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!

../../../_images/programming2.jpg


Photo by Christina Morillo from Pexels
September 29, 2021
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

int

positive/negative whole numbers

42, -42, -10000

floating point number

float

real number in decimal form

3.14159

boolean

bool

true or false

True

string

str

text

"I Can Has Cheezburger?"

list

list

a collection of objects - mutable & ordered

["Ali","Xinyi","Miriam"]

tuple

tuple

a collection of objects - immutable & ordered

("Thursday",6,9,2018)

dictionary

dict

mapping of key-value pairs

{'name':'DSCI','code':511,'credits':2}

none

NoneType

represents no value

None

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 and False.

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

x == y

is x equal to y?

x != y

is x not equal to y?

x > y

is x greater than y?

x >= y

is x greater than or equal to y?

x < y

is x less than y?

x <= y

is x less than or equal to y?

x is y

is x the same object as y?

2 < 3
True
"Data Science" != "Deep Learning"
True
2 == "2"
False
2 == 2.00000000000000005
True

Operators on Boolean values.

Operator

Description

x and y

are x and y both true?

x or y

is at least one of x or y true?

not x

is x false?

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