Session 16 – Python: Statements and Expressions

16.1 What are statements?

A statement in Python is a line of code that can be read and executed. There are two types of statements that we will cover in python: an expression and an assignment statement. You can think of an expression as a mathematical formula or direction, whereas, an assignment statement is the part of the text that defines what each of the variables are.

16.2 Expressions in Python

An expression always contains a logical sequence of either numbers, strings, operators, or objects.

#Arithmetic Expression: Calculate an average test score...
(95+55+45)/3

#Does not have to contain all numbers: 2 to the 4th power...
pow(2,4)

Again, Expressions do not have to simply be arithmetic but can also deal with strings, operators, and objects.

16.3 Simple Assignment Statements in Python

This is something that should look very familiar as we just identifiers. In an assignment statement we list our identifier along with its definition.

# You can think of this as a variable or identifier will equal an expression

wills_in_store = "10 people named Will in store"
# variable = expression

#use id() to see where your expression lies in your computers' memory
id(wills_in_store)

#this may seem like a tangent but it is important to note what your computer does with information and where it is stored. 
#we discussed that Python will overwrite the definition of a identifier if reassigned, but what will happen if you assign the same content to two different identifiers?

#scenario 1:
my_data = "data"
id(my_data)

my_data2 = "data"
id(my_data2)

#scenario 2:
my_data = "data here"
id(my_data)

my_data2 = "data here"
id(my_data2)

#scenario 3:
my_data = 20
id(my_data)

my_data2 = 20
id(my_data2)

#scenario 4:
my_data = 2000
id(my_data)

my_data2 = 2000
id(my_data2)

The general rules for data allocation in memory, in respect to duplicate expressions is:

  1. String duplicates under 20 characters that do not contain whitespaces will share memory locations

  2. Number duplicates between -5 and 255 will share memory locations

These rules are essential for ‘interning’, or the process of saving memory.

16.4 Augmented Assignment Statements in Python

Assignment statements can be more complex, adding additional commands along with a simple assignment, thus labeled an augmented assignment.

Below are examples of augmented assignments:

#simple assignments
x = 1
y = 2

#augmented assignments
x += y
#or
x = x + y

#simple tuple
tuple_1 = (1, 2, 3)
print(tuple_1)

#augment tuple
tuple_1 += (4, 5)
print(tuple_1)

#simple tuple 2
tuple_2 = ('apple', 'bannana', 'pear')
print(tuple_2)

#augment tuple 2
tuple_2 += ('grape', 'tomato')
print(tuple_2)

16.5 Explicit and Implicit Line Continuation

In most programming languages, programmers try to not make lines of code too long. This is turn makes the code more readable and easier to debug. Long lines fatigue the reader and some text editors make it difficult to view these lines. There are two methods of splitting long lines of code into numerous lines: explicit and implicit line continuation methods.

#explicit line continuation: use backslash
list_1 = [1, \
2, 3, \
4 \
]
print(list_1)

#implicit line continuation: use open parentheses (), braces {}, and brackets []
list_1 = [1,
2, 3,
4
]
print(list_1)

16.6 Indentation in Python is Crucial to Proper Code function

Python constructs blocks of code via an indentation structure. Other programming langauges vary in the method in which they create blocks of code. Most commonly braces are used to do this outside of Python.

In Python the first line of a code block is written normally. The subsequent lines of code, if they are in the same code block, must be indented accordingly. If you recieve any error in Python while learning, it will mostly be indentation errors as it is a strange concept to pickup and learn.

Indentation is not needed for simple statements, but compound expressions require indents. Let us practice with some of the examples below:

#lets define a function with proper indentation
def even_or_odd(num):
    print("Starting Analysis of Number")
    if num % 2 == 0:
        return True
    else:
        return False 

num = 7
if even_or_odd(num) is True:
    print(num, 'is an even number')
else:
    print(num, 'is an odd number')

16.7 Comments

Be sure to annotate your code with hashtags followed by the code description. This is similar to R so there is not much new ground to cover. As you build more complex programs it will be essential to accurately recall what you wrote and why.

In addition to traditional hashtags for comments you may use docstrings for large notes:

#lets introduce docstrings to our previous code
def even_or_odd(num):
    print("Starting Analysis of Number")
    if num % 2 == 0:
        return True
    else:
        return False 


'''
These lines will not count
They are between the triple quotes
So they will not be used as code, but instead a comment
'''

num = 7
if even_or_odd(num) is True:
    print(num, 'is an even number')
else:
    print(num, 'is an odd number')