Programming languages are literal, unambiguous, and concise
Values and Types
Value: a basic thing a program works with (letters, numbers, etc.)
Type: a category of values (integers, strings, floats, etc.)
Python is dynamically typed. It determines the type of a variable based on its value
You don’t have to explicitly declare variable types
Variables and Expressions
Variables store values
Use underscores for multi-word variable names
Can’t start a variable with a number, include an illegal character (Ex: “@”), or name it after a Python keyword (Ex: “return”)
Expressions follow an order of operations (the usual PEMDAS for math operations)
Python Math operators
*, / Multiply and Divide
+, - Add and Subtract
** Exponentiate
%, // Modulus and Floor Division
Built-in Data Structures
List
Collection of any types of objects, uses []
Mutable
Dictionary
Key-value pairs, uses {}
Access values with keys, keys are immutable
Tuple
Immutable version of lists, use ()
Set
Unordered collection of unique objects, uses {}
Mutable, but can be made immutable with frozenset()
Supports common operations on sets, such as intersection and union
Built-in Data Structures - List
Lists are 0-indexed. We can access the elements of a list with square braces and the index.
Built-in Data Structures - Dictionary
We can access the paired values by dictionary[key]
Built-in Data Structures - Tuple
Tuples are 0-indexed. We can access (but not modify!) the elements of a tuple with square braces and the index.
Built-in Data Structures - Set
The least used (and often overlooked!) built-in. Useful operations include the union (|) and intersection (&).
Functions
Create a Python function with the following boilerplate
deffunction_name(positional_args, keyword_args=default_val):
"""This is a docstring. ALWAYS write docstrings for your functions. """my_val = <some computations using the inputs>
return my_val
Some text editors will handle the tab \(\to\) spaces conversion for you
Control Structures (if, for, while, etc.)
Three types of control structures
Sequential
The normal flow of code
Selection/Decision
if, elif, else
Repetition
while, for
Control Structures - Sequential
x = 5
y = 15
print("The difference is", y - x)
The difference is 10
Control Structures - Selection/Decision
Control the flow of code based on Boolean (True/False) values/expressions.
x = 5
y = 15
if x == y:
print("The numbers are the same!")
else:
print("The numbers are not the same!")
The numbers are not the same!
Control Structures - Repetition
Repeat based on conditionals or iterables
x = 12
y = 15
diff = y - x
my_list = [5, 10, 15]
while x < y:
print("Difference is", diff)
x += 1
for z inrange(2):
print("The value of z is", z)
for t in my_list:
print("The value of t is", t)
Difference is 3
Difference is 2
Difference is 1
The value of z is 0
The value of z is 1
The value of t is 5
The value of t is 10
The value of t is 15
Script vs Interactive
So far, we’ve been working in the Python Interactive Shell. We can also bundle up our commands and run them from a .py script. The shebang#!/usr/bin/env python lets our computer know how to run the script if we don’t invoke it with python my_script.
Python Demos
Visit the link below to get an online instance of a Jupyter Notebook with some demos.