Introduction to Programming
Introduction videos
Corey Schafer:
Python Tutorial for Beginners 1: Install and Setup for Mac and Windows
Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data
Integers and Floats
type()
Order of operations
Storiing a number in a variable
Incrementing a number
abs()
round()
Assignment operators
int()
Mr. Gallo:
Installing Python
Before you can start programming in Python, you will need the Python interpreter. The Python interpreter helps translate Python into machine code.
Please follow Corey Schafer’s video Python Tutorial for Beginners 1: Install and Setup for Mac and Windows. He will forward you to the Python Download page and show you what to do. You may also want to follow his other Python videos. They are all well done.
An Integrated Development Environment (IDE)
When you watch the previously mentioned video, Corey will recommend some IDEs like PyCharm and Atom. I suggest you get VS Code and its Python Extension. It’s currently the most popular IDE. Microsoft also has a Getting Started with Python in VS Code tutorial (optional).
Corey Schafer also has a couple videos about VS Code specifically (optional viewing):
Create and run a Python file
Output a message
print("Hello, World!")
print("foo!")
print("bar!")
print("foo2", end="") # keep next print on same line
print("bar2")
Mathematical operations
Operation |
Operator |
Example |
Result |
---|---|---|---|
Addition |
|
|
|
Subtraction |
|
|
|
Multiplication |
|
|
|
Division |
|
|
|
Floor Division |
|
|
|
Modulus (remainder) |
|
|
|
Exponent |
|
|
|
Order of operations
Parentheses (including function calls)
Exponents
Multiplication
Division
Addition
Subtraction
4 + 5 * 2 # 14
(4 + 5) * 2 # 18
round(4.25) * 3 # 12
2**8 * 2 # 512
Common Math Functions
Built-in math functions
# Absolute value
abs(-5) # 5
# General round
round(5.55) # 6
round(5.45) # 5
round(3.141592653589793, 3) # 3.142
round(3.141592653589793, 2) # 3.14
From the math library
import math
# square root
math.sqrt(49) # 7
# Floor (round down)
math.floor(5.99) # 5
# Ceiling (round up)
math.ceil(99.1) # 100
Storing data in variables
Video: Introduction - Storing Data in Variables
1length = 5
2width = 10
3
4area = length * width
5
6print(area) # 50
Assignment Operators
The following operators will re-assign a variable value after performing a modification to the initial value.
>>> num = 3
>>> num += 2
>>> num
5
>>> num *= 5
>>> num
25
>>> num -= 5
>>> num
20
>>> num /= 4
>>> num
5.0
Note
The special assignment operators are just short-hand for a mathematical operation and a re-assignment.
num += 2
# is the same as
num = num + 2
Get input from the user
Video: Introduction - Get Input from the User
name = input("Please enter a name: ")
print("Hello", name)
Convert strings to numbers
1my_str = "hello" # obviously a string
2age_str = "50" # not-so-obviously a string.
3
4age_next_year = age_str + 1 # will NOT work. Cannot do math on a string.
5
6# we can convert strings that contain numbers into integers
7age_int = int(age_str)
8print(age_int + 1)
9
10# we can also convert into float (decimal)
11age_float = float(age_str)
12print(age_float + 1)
13
14# if receiving a number from user, convert it before storing it in a variable
15some_number = int(input("Please enter a number: "))
Format output text
There are a number of ways to format text by placing variable values into strings. All have their advantages and disadvantages. You may prefer to use different approaches in different situations.
1name = "John"
2age = 37
3
4# f-strings. Python 3.6+
5message_fstring = f"Hello, {name}. You are {age} years old."
6
7# .format()
8message_dot_format = "Hello, {}. You are {} years old.".format(name, age)
9
10# concatenation
11message_concat = "Hello, " + name + ". You are " + str(age) + " years old."
12
13print(message_fstring)
14print(message_dot_format)
15print(message_concat)
Hello, John. You are 37 years old.
Hello, John. You are 37 years old.
Hello, John. You are 37 years old.