$$\require{cancel}$$

# 28.1: A quick intro to programming

In Python, as in other programming languages, the equal sign is called the assignment operator. Its role is to assign the value on its right to the variable on its left. The following code does the following:

• assigns the value of 2 to the variable a
• assigns the values of 2*a to the variable b
• prints out the value of the variable b

python code $$\PageIndex{1}$$

Declaring variables in Python

#This is a comment, and is ignored by Python
a = 2
b = 2*a
print(b)

Output

4

Note that any text that follows a pound sign (#) is intended as a comment and will be ignored by Python. Inserting comments in your code is very important for being able to understand your computer program in the future or if you are sharing your code with someone who would like to understand it. In the above example, we called the print() function and passed to it the variable b as an argument; this allowed us to print (display) the value of the variable b and verify that it was indeed equal to the number $$4$$.

In Python, if you want to have access to “functions”, which are a more complex series of operations, then you typically need to load the module that defines those operations.

A large number of functions are provided in Python. Most of these functions need to be “imported” from “modules”. For example, if you want to be able to take the square root of a number, then you need to load (import) the “math module” which contains the square root function, as in the following example:

python code $$\PageIndex{1}$$

Using functions from modules

#First, we load (import) the math module
import math as m
a = 9
b = m.sqrt(a)
print (b)


Output

3

In the above code, we loaded the math module (and renamed it m); this then allows us to use the functions that are part of that module, including the square root function (m.sqrt()).