Class 4B: Introduction to Programming in Python
Contents
Class 4B: Introduction to Programming in Python¶
We will begin at 2:00 PM! Until then, feel free to use the chat to socialize, and enjoy the music!

Firas Moosvi
Class Outline:¶
Digging Deeper into Python syntax
Announcements (5 min)
Thursday labs will be cancelled this week (Nat’l day of truth and reconciliation)
Project 1 Milestone Feedback has now been released
Project Details are also released
Test details
Test 1 will be from Friday 6 PM - Sunday 6 PM (48 hour window)
You can start the test at any time during this window
You will have 1 hour to do the test (Canvas Quiz)
Content: Terminal & Git commands & Markdown syntax
Basic datatypes (15 min)
Lists and tuples (15 min)
String methods (5 min)
Dictionaries (10 min)
Conditionals (10 min)
Basic datatypes¶
A value is a piece of data that a computer program works with such as a number or text.
There are different types of values:
42
is an integer and"Hello!"
is a string.A variable is a name that refers to a value.
In mathematics and statistics, we usually use variables names like \(x\) and \(y\).
In Python, we can use any word as a variable name (as long as it starts with a letter and is not a reserved word in Python such as
for
,while
,class
,lambda
,import
, if, else, etc.).
And we use the assignment operator
=
to assign a value to a variable.
See the Python 3 documentation for a summary of the standard built-in Python datatypes. See Think Python (Chapter 2) for a discussion of variables, expressions and statements in Python.
Common built-in Python data types¶
English name |
Type name |
Description |
Example |
---|---|---|---|
integer |
|
positive/negative whole numbers |
|
floating point number |
|
real number in decimal form |
|
boolean |
|
true or false |
|
string |
|
text |
|
list |
|
a collection of objects - mutable & ordered |
|
tuple |
|
a collection of objects - immutable & ordered |
|
dictionary |
|
mapping of key-value pairs |
|
none |
|
represents no value |
|
Numeric Types¶
x = 45
print(x)
45
type(x)
int
print(x)
45
x # in Jupyter we don't need to explicitly print for the last line of a cell
45
pi = 3.14159
print(pi)
3.14159
type(pi)
float
Arithmetic Operators¶
The syntax for the arithmetic operators are:
Operator |
Description |
---|---|
|
addition |
|
subtraction |
|
multiplication |
|
division |
|
exponentiation |
|
integer division |
|
modulo |
Let’s apply these operators to numeric types and observe the results.
1 + 2 + 3 + 4 + 5
15
0.1 + 0.2
0.30000000000000004
Tip
Note From Firas: This is floating point arithmetic. For an explanation of what’s going on, see this tutorial.
2 * 3.14159
6.28318
2**10
1024
type(2**10)
int
2.0**10
1024.0
int_2 = 2
float_2 = 2.0
float_2_again = 2.
101 / 2
50.5
101 // 2 # "integer division" - always rounds down
50
101 % 2 # "101 mod 2", or the remainder when 101 is divided by 2
1
None¶
NoneType
is its own type in Python.It only has one possible value,
None
x = None
print(x)
None
type(x)
NoneType
You may have seen similar things in other languages, like null
in Java, etc.
Strings¶
Text is stored as a datatype called a string.
We think of a string as a sequence of characters.
We write strings as characters enclosed with either:
single quotes, e.g.,
'Hello'
double quotes, e.g.,
"Goodbye"
triple single quotes, e.g.,
'''Yesterday'''
triple double quotes, e.g.,
"""Tomorrow"""
my_name = "Firas Moosvi"
print(my_name)
Firas Moosvi
type(my_name)
str
course = "DATA 301"
print(my_name,course)
Firas Moosvi DATA 301
print(my_name+" --------------> "+course)
Firas Moosvi --------------> DATA 301
type(course)
str
If the string contains a quotation or apostrophe, we can use double quotes or triple quotes to define the string.
"It's a rainy day cars'."
"It's a rainy day cars'."
sentence = "It's a rainy day."
print(sentence)
It's a rainy day.
type(sentence)
str
saying = '''They say:
"It's a rainy day!"'''
print(saying)
They say:
"It's a rainy day!"
Boolean¶
The Boolean (
bool
) type has two values:True
andFalse
.
the_truth = True
print(the_truth)
True
type(the_truth)
bool
lies = False
print(lies)
False
type(lies)
bool
Comparison Operators¶
Compare objects using comparison operators. The result is a Boolean value.
Operator |
Description |
---|---|
|
is |
|
is |
|
is |
|
is |
|
is |
|
is |
|
is |
2 < 3
True
"Data Science" != "Deep Learning"
True
2 == "2"
False
2 == 2.00000000000000005
True
Operators on Boolean values.
Operator |
Description |
---|---|
|
are |
|
is at least one of |
|
is |
True and True
True
True and False
False
False or False
False
# True and True
("Python 2" != "Python 3") and (2 <= 3)
True
not True
False
not not not not True
True
Casting¶
Sometimes (but rarely) we need to explicitly cast a value from one type to another.
Python tries to do something reasonable, or throws an error if it has no ideas.
x = int(5.0)
x
5
type(x)
int
x = str(5.0)
x
'5.0'
type(x)
str
str(5.0) == 5.0
False
int(5.3)
5