Basics of Python

Distinctive Features of Python.

  1. Python statements do not need to end with a special character (e.g., comma, semicolon). Python interpreter knows that you are done with a statement when you start a newline. What if I have a statement spanning more than one line? Use a backslash\ to explicitly tell Python this is a continuation of a statement. E.g.,
    a = '1'   \
     + '2' \
     + '3' \
     - '4'
    
  2. Python uses indentation (leading whitespaces) to define code block. Most of the programming languages (like C, C++, Java) however, use curly brackets {}. It is noted that the number of leading whitespaces does not matter, but has to be consistent. In other words, if two statements belong to the same group level, they should have the same amount of whitespaces. All Python IDEs (integrated development environments) can manage indentation automatically. The Python developers believes that the indentation helps readability, however other programmers believe this is a evil feature . This is probably the most distinctive feature of Python.

Glossary:
Code block: is a grouping of two or more statements. In many cases, it serves as a way to define logical level and limit the scope of variables and functions. It makes more sense when we talk about the scope of variables.

Your first Python program

In [1]:
a = "Hello World!"
print(a)
Hello World!

Variable

Variable is a reserved memory location to store values which we will refer to later in the code. Python is dynamically-typed language, which means you do not need to claim or predefine the data type of a variable.

I/O: Input and Output

Input

The input can be from an end user or an external file (e.g., a txt file). Let's read something from users' input.

In [2]:
a = input("please enter a number:")
print("You just entered {}, correct?".format(a))# this is called string formatting
please enter a number:23
You just entered 23, correct?

Syntax: # is a symbol indicating the following phrases are just comments that have nothing to do with code. You can always use # to annotate your code.

Note: In most cases, we do not use the built-in input or file-reading functions from Python. Instead, we turn to Pandas or other packages. We will learn to use Pandas to read csv files in the third session on Nov. 6.

Output

In [16]:
print("Hello Python!")
Hello Python!
In [5]:
a = 5
a
Out[5]:
5
In [3]:
a = "I love python"
a
Out[3]:
'I love python'
In [4]:
print(a)
I love python

Note: If the last line of a cell is a variable, Jupyter Notebook automatically prints it out using a Ipython output function--display. This is a Ipython trick, which you cannot observe while working with a regular Python IDE.

In [20]:
a = 2
a
Out[20]:
2
In [4]:
b = '2'
b
Out[4]:
'2'

Question: Does a '2' (enclosed by double quotes) equal b 2?

Data Types

  • Primitive Types:
    1. Numeric types:
      • Integer (any whole numbers, e.g., 1.2.3)
      • Floating point number (any decimal numbers, 1.5, 1.6)
      • Complex numbers (not covered)
    2. String type (has to been enclosed by single or double quotes.)
    3. Boolean type (True or False)
  • Collection types:
    1. List (a collection of numbers, mutable, wrapped by square brackets []. Elements in a list do not need to be of the same type.)
    2. Tuple (a collection of numbers, immutable, wrapped by parentheses () )
    3. Dictionary (a collection of key-value pairs, mutable, wrapped by curly brackets {})
    4. Set (a specialized list, with no duplicates. mutable, also enclosed by curly brackets {}. You can think of a set as a dictionary with only keys.)
In [5]:
a = 1
type(a) # type() is a built-in function of Python, which checks the type of the value/varialbe passed in
Out[5]:
int
In [6]:
type(-1) #also an integer
Out[6]:
int
In [7]:
a = 1.3 # a decimal number
type(a)
Out[7]:
float
In [8]:
"Brown University" # a string wrapped by double quotes
Out[8]:
'Brown University'
In [9]:
'Brown University' # a string wrapped by single quotes.
Out[9]:
'Brown University'

Note: In most cases, single quotes and double quotes are interchangeable. What if a string happen to have quotation marks inside itself? The rules are:

  1. If a string has single quote inside, you need to use double quotes to wrap it. E.g., "I've done it!"
  2. If a string has double quotes inside, you need to use single quotes to wrap it. e.g., ' "this is quoted sentence!" '
  3. if a string has mixed types of quotes, or you just want to memorize rule 1 & 2, use triple single/double quotes. e.g., """ I have a single quote ' here, also a double quote here " Haha! """
In [50]:
a = """ This sentence has a single quote ' here and also a double quote " here! """
print(a)
 This sentence has a single quote ' here and also a double quote " here! 

string formatting: Let's look at a very powerful method (we will talk about what are methods and their difference from functions), format(), which allows you to do string substitutions. This method allow you to concatenate elements through positional formatting.

In [10]:
x = 'language'
b = "Python is a {}".format(x)
b
Out[10]:
'Python is a language'

What if I want to replace more than 1 elements?

In [15]:
a = 'Python'
b = 'language'
sentence = '{} is a {}'.format(a,b)
sentence
Out[15]:
'Python is a language'

Or you could put indexes inside the curly brackets to explicitly get the values of positional arguments.

In [17]:
a = 'Python'
b = 'language'
sentence = "{1} is a {0}".format(b,a)
sentence
Out[17]:
'Python is a language'

f string: a new syntax since Python 3.6 for string formatting. The code within the {} are evaluated.

In [13]:
a = 'Rhode Island'
x = f'Where is {a}'
x
Out[13]:
'Where is Rhode Island'
In [14]:
a = 4
b = 5
x = f"4+5={a+b}"
x
Out[14]:
'4+5=9'

Boolean Data Type

In [15]:
a = True # a is boolean varialbe. True is a reserved word to represent the logic true value. 
print(a)
True
In [16]:
b = False # False is a reserved word to represent the logic false value. 
b
Out[16]:
False

List

In [9]:
[2,4,'t',True] # This is a list. As you can tell, the elements in a list do not need to be of the same type. 
Out[9]:
[2, 4, 't', True]
In [10]:
(2, 3,True) # This is a tuple. 
Out[10]:
(2, 3, True)
In [65]:
{'First-name:':'Bill',"Last-name":"Gates"} # this is a dictionary, 'first-name' and 'last-names' are called keys. 
Out[65]:
{'First-name:': 'Bill', 'Last-name': 'Gates'}
In [61]:
{2,3,4} # this is a set 
Out[61]:
{2, 3, 4}

Data Type Conversion

Yes, you can convert a variable from one type to another compatible type. E.g., you have a string variable '4'. You might want to use its corresponding numeric value 4 to do some arithmetic operations.

  • turn a variable to a string: str()
  • turn a variable to a integer: int()
  • turn a variable to a floating point number: float()
  • turn a variable to a boolean value: bool()
In [17]:
a = "4"
b = a + 8
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-4bbac2df7991> in <module>
      1 a = "4"
----> 2 b = a + 8

TypeError: can only concatenate str (not "int") to str
In [18]:
a = "4"
b = int(a)
print (b+8)
12
In [21]:
a = 4.3
b = int(a)
b
Out[21]:
4
In [22]:
a = 4 
b = str(4)
b
Out[22]:
'4'
In [23]:
a = 4
b = float(4)
b
Out[23]:
4.0
In [24]:
a = "time"
b = int(a)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-33927b2ae7ba> in <module>
      1 a = "time"
----> 2 b = int(a)

ValueError: invalid literal for int() with base 10: 'time'
In [25]:
a = 1
bool(a)
Out[25]:
True
In [26]:
a = 2
bool(a)
Out[26]:
True
In [27]:
a = 0 
bool(a)
Out[27]:
False
In [28]:
a = 'time'
bool(a)
Out[28]:
True
In [29]:
a = ''
bool(a)
Out[29]:
False

Common Operations\Operators

Operators are constructs to manipulate the values of operands. Look at this expression, 4 + 5 = 9. 4 and 5 are called operands, and + is called operators. It is noted that the same operator might lead to a different operation when it is applied to different data types.

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Bitwise operators (not covered)
  • Membership operators
  • Identity operators

Arithmetic operators

Operation Operator Example
Addition + a + b
Subtraction - a - b
Division / a / b
Floored Division // a // b
Multiplication * a * b
Remainder % a % b
Exponentiation ** a ** b
Unary negation - -a
In [66]:
4+3
Out[66]:
7
In [67]:
4-3
Out[67]:
1
In [68]:
4/3
Out[68]:
1.3333333333333333
In [69]:
4//3
Out[69]:
1
In [75]:
5%3
Out[75]:
2
In [78]:
4**2 #raise 4 to the power 2 = 16
Out[78]:
16
In [80]:
-(-5) # the negation of -5
Out[80]:
5
In [30]:
'Brown' + ' University' #concatenate two strings
Out[30]:
'Brown University'

Note: What about square root operation, rounding operation and so on? These operations can be found in math package.

import math
math.sqrt(x)
math.floor(x)
math.ceil(x)

Comparison operators

compare values of operands and return either True or False.

Operation Operator Example
Equal to == 4==3
Not equal to != 3!=4
Less than < 3 < 4
Larger than > 4 > 4
Less than or equal to <= 3 <=4
Larger than or equal to >= 4 >=3
In [81]:
4==3
Out[81]:
False
In [82]:
3!=4
Out[82]:
True
In [83]:
3<4
Out[83]:
True
In [84]:
4>4
Out[84]:
False
In [85]:
3<=4
Out[85]:
True
In [86]:
4>=3
Out[86]:
True
In [87]:
"Tom" == "Jerry"
Out[87]:
False
In [88]:
4 == '4'
Out[88]:
False
In [31]:
1 == True
Out[31]:
True

Assignment Operators

Assigns the value on the right end to its left variable.

Operation Operator Example Explanation
Assignment = x =5 x = 5
Addition assignment += x += y x = x+y
Subtraction assignment -= x -= y x = x-y
Multiplication assignment *= x *= y x = x*y
Division assignment /= x /= y x = x/y
Remainder assignment %= x %= y x = x%y
In [32]:
x = 5 # assing the value of 5 to x.
x
Out[32]:
5
In [33]:
y = 4
y
Out[33]:
4
In [34]:
x += y # x = x + y; we add y to x and save the result to an intermediate memory and then assign it back to X. 
x
Out[34]:
9
In [99]:
x = 5
y = 4
x -= y # x = x-y = 5-4 = 1
x
Out[99]:
1
In [100]:
x = 5
y = 4
x *= y # x = x*y = 5*4 = 20
x
Out[100]:
20
In [101]:
x = 5
y = 4
x /= y # x = x/y = 5/4=1.25
x
Out[101]:
1.25
In [103]:
x = 5
y = 4
x %= y # x = x%y = 5%4 = 1
x
Out[103]:
1

Logical operators:

There are three logical operators in Python: and, or, not. They are designed to be applied to boolean values or boolean-compatible values. Mainly used in if-else statement (introduced shortly)

  • and: used to combine two logical expressions, returns True only if both sides are true.
  • or: used to combine two logical expressions, returns True if either side is true.
  • not: return True if the following logic expression is False.
In [35]:
True and False
Out[35]:
False
In [37]:
(5 > 4) and (-1 > 0)
Out[37]:
False
In [104]:
not True
Out[104]:
False
In [38]:
not (4 < 6)
Out[38]:
False
In [39]:
not '2' # '2' is evaluted to be True, so not True leads to a False value.
Out[39]:
False
In [40]:
not 0 # 0 is evaluated to be False, not False leads to a True value. 
Out[40]:
True

Membership operators:

Checks if an element is part of a collection (e.g., list, string). Returns True if the collection contains that element, otherwise False.

  • in: is the specified element inside a collection?
  • not in: is the specified element not inside a collection?
In [44]:
5 in [3,4,5] # list
Out[44]:
True
In [43]:
1 in (2,3,4) #tuple 
Out[43]:
False
In [45]:
'B' in 'Brown University'
Out[45]:
True
In [46]:
'rown' in "Brown University"
Out[46]:
True
In [47]:
'T' not in "PSTC"
Out[47]:
False

Exercise: Data types

Flow Control

In the previous tutorials, we've learned how to use Python to do simple arithmetic calculations. In real world, a code script can become way more complicated than that.

This section introduces you three flow-control statements in Python

  • if statement (condition)
  • for statement (loop)
  • while statement (loop)

The If statement

The if statement is used to check whether a condition is met. If the condition is True, the code block under the if statement will be executed. Otherwise the code block under the else statement is executed. It is noted that the else statement is optional.

The syntax is:

  • If statement
if condition:
    do something
  • if-else statement:
    if condition: 
      do something
    else:
      do something-else
    
  • if-elif-else statement: (It is noted that you may end up having more than 1 elif)
    if condition:
      do something
    elif condition2:
      do something else
    else:
      balabala
    

if-statement

In [48]:
a = 'p'
b = 'pstc'
if a in b: # here a in b is logical expression
    print("a is part of b!") # pay attention to the intentation here. 
a is part of b!

if-else statement

In [17]:
a = 4
b = 5
if a == b:
    print("a equals b!")
else: 
    print('a does not equal b!')
    
a does not equal b!

if-elif-else statement

In [49]:
a = 4
b = 4 
if a > b: #this condition fails
    print('a > b')
elif a < b: # this confition fails
    print('a < b')
else: 
    print('Well, then a must equal b!')
Well, then a must equal b!

The for-loop

The for...in statement is a looping statement, which iterates over an iterable (e.g., list, str,tuple,dict and file objects).

syntax:

for item in iterable: 
    do something

Note that the item variable is a "temporary" variable which is updated on-the-fly to refer to the element during each iteration.

In [50]:
a = [1,2,3,4,5] # list is an iterable.
for item in a: 
    print(f"the current value is {item}")
the current value is 1
the current value is 2
the current value is 3
the current value is 4
the current value is 5

Nested for-loop: where you can have all the possible combinations Syntax:

for item_1 in iterable_1:
    for item_2 in iterable_2:
        do something
In [51]:
cities = ['Providence','Pawtucket','Barrington']
supermarkets = ['Wholefoods', 'Walmart']
for city in cities: 
    for supermarket in supermarkets: 
        print(f'{city}-{supermarket}')
    
Providence-Wholefoods
Providence-Walmart
Pawtucket-Wholefoods
Pawtucket-Walmart
Barrington-Wholefoods
Barrington-Walmart

The while-loop

The while statement allows you to repeat an action until the condition specified fails. The syntax:

while condition:
    do something
    also change the condition

Note that remember to change the variable(s) involved in the condition during the looping. If not, the code will never stops until your computer memory runs out.

In [21]:
a = 5
while a<10:
    print(a)
    a += 1 #still remember the addition assignment? a = a+1
    print("moving to the next iteration")
5
moving to the next iteration
6
moving to the next iteration
7
moving to the next iteration
8
moving to the next iteration
9
moving to the next iteration

More Controls in Loops: break and continue

Break keyword

Assuming we have a variable with a long list of names. We want to test whether 'Tom' is in this list.

In [52]:
%%time
names_list =['Tom']+['Jerry']*100000 # This is a list of names containing one Tom(the first element) and 100,000 of 'Jerry' 
for name in names_list:
    if name == 'Tom':
        print("I found Tom!")
I found Tom!
Wall time: 7.98 ms

%%time is a Jupyter magic command that records the running time of a cell.

What is the problem with the code above? This for-loop runs over all the elements in the name list. However, we expected the script to stop as soon as it found "Tom". The rest of searching is a waste of time and computing resources. Let's introduce the new keyword "break" to terminate (jump out of) a loop.

In [54]:
%%time
names_list =['Tom']+['Jerry']*100000
for name in names_list:
    if name == 'Tom':
        print("I found Tom!")
        break 
I found Tom!
Wall time: 997 µs

The code with a break statement is 13 times faster than the one without a break on my laptop

Continue Keyword

Skips over all the lines below the continue keyword within a loop and move directly to the next iteration. It is very helpful when you have multiple conditions inside a loop.

In [55]:
names_list =['Tom']+['Jerry']*100000
for name in names_list:
    if name == 'Tom':
        continue
        print("I found Tom!")

Why is there no output? During a loop, if the script sees continue keyword, it automatically skips over the rest of code and move forward to the next element.

Exercise: Flow Control

In [ ]: