Python for Beginners

What is Python?

Description

Python is an interpreted programming language. It was created by Guido van Rossum and first released in 1991. Python is known for its simplicity and readability, which makes it a great language for beginners.

Python is one of the most popular languages in the world. It is often the preferred language when automating tasks via scripts. It is also widely used in scientific computing, data science, machine learning, and backend web development.

This course will teach you the core concepts of Python. But if you're a beginner, the most important thing you will learn is how to think like a programmer.

Challenge

You can click the Submit button to execute the code on the right. Don't worry if you don't understand any of it.

You will notice that the output is incorrect. We want to calculate the first 20 digits of pi, but right now our program is only printing the first 19 digits. With the power of programming we can fix this by changing a single line of code.

To fix this, find the following line of code:

n = 19

and change the number to 20. Then click the Submit button again.

Hint: If you don't want to read through the code, click the editor and press Ctrl + F. You can then type "n = 19" to find the line of code you need to change.


What is an interpreted programming language?

An interpreted programming language is different from a compiled language. In a compiled language, the code is translated into machine code before it is run. In an interpreted language, the code is executed line by line by an interpreter, usually written in another language. This makes it easier to write and test code, but it can be slower than compiled languages.

It's okay if you don't understand all of this yet. If you're a beginner learning Python as your first language, the distinction between interpreted and compiled languages is not important for now.


What is a script?

A script is a file that contains a series of commands that are executed by a computer. Scripts are often used to automate repetitive tasks, such as renaming files, moving files, or processing data. Python is often used to write scripts because of its simplicity and readability.

Technically, any program that is written in a scripting language is a script. However, the term is often used to refer to small programs that automate tasks on a computer. The terms "scripting language" and "interpreted language" are often used interchangeably. They are technically different, but the distinction is rarely important.

Starter Code

from decimal import Decimal, getcontext
 
def calculate_pi(n):
    getcontext().prec = n + 2  # Set precision higher than needed for accuracy
    
    C = 426880 * Decimal(10005).sqrt()
    K = 6
    M = 1
    X = 1
    L = 13591409
    S = L
    
    for i in range(1, n):
        M = (K ** 3 - 16 * K) * M // i ** 3
        L += 545140134
        X *= -262537412640768000
        S += Decimal(M * L) / X
        K += 12
    
    pi = C / S
    return str(pi)[:n + 2]  # Return first n digits plus the '3.'
 
n = 19
pi_digits = calculate_pi(n)
print(pi_digits)

Video Explanation

Click here to jump to the What is Python? section of the video.

Solution Code

from decimal import Decimal, getcontext
 
def calculate_pi(n):
    getcontext().prec = n + 2  # Set precision higher than needed for accuracy
    
    C = 426880 * Decimal(10005).sqrt()
    K = 6
    M = 1
    X = 1
    L = 13591409
    S = L
    
    for i in range(1, n):
        M = (K ** 3 - 16 * K) * M // i ** 3
        L += 545140134
        X *= -262537412640768000
        S += Decimal(M * L) / X
        K += 12
    
    pi = C / S
    return str(pi)[:n + 2]  # Return first n digits plus the '3.'
 
n = 20
pi_digits = calculate_pi(n)
print(pi_digits)

On this page