"Hello World!" is a string - a sequence of characters surrounded by enclosing quotation marks.
print("Hello World!")
2 + 2 # Addition
10 - 3 # Subtraction
6 * 7 # Multiplication
5/2 # Division
1 # 1 is recognized by Python as an integer
1. # 1. is recognized by Python as a float (floating point number)
1 + 1 # The sum of two integers is an integer
1 + 1. # The sum of an integer and a float is a float
2**5 # The ** operator is used for powers
26 % 5 # The % operator is used to find the modulus (remainder after division)
abs(-88) # Absolute value
g = 9.81 # Assigning the value 9.81 to the variable g
g # g takes on the value 9.81, which is substituted in whenever g is used
g*2
x = 1
y = 2
z = x + y # So z = 1 + 2 = 3
print(z)
print(x, y, z) #
a, b = 3, 4
print(a)
print(b)
The entire right hand side is evaluated before assigning values to the variables on the left
a, b = b, a
print(a, b)
print(pi) # pi does not yet exist
import math # Make the functions and variables in the math module available with the prefix .math
math.pi # pi is now available with the .math prefix
math.sin(1) # sin is a function in the math module
math.cos(0) # cos is also a function in the math module
from math import pi # import the variable pi
pi # pi can now be used without the math. prefix
from math import * # import all variables and functions from the math module
atan(1.0) # atan returns the arctan
atan2(-1, 1) # atan2 takes the (x, y) coordinates of a point
floor(4.3) # floor rounds down to the nearest integer
ceil(4.3) # ceil rounds up to the nearest integer
print(dir(math)) # dir returns a list of all functions and variables in a module
? degrees # Get help on a function
help(degrees) # Another way to get help.
degrees(pi) # degrees converts radians to degrees
Python has a built-in "boolean" data type that can take the values True or False.
A "boolean expression" is a Python statement that evaluates to True or False
3 == 2
3 == 3
3 != 2
3 != 3
3 > 3
3 >= 3
2 < 1
2 <= 1
If # Variables, function names and keywords are all case-sensitive in Python. So "If" and "if" are not the same.
These have the syntax:
if <boolean expression>:
<code to execute if boolean expression is True>
<following code always gets executed>
if 3 > 2:
print("3 is greater than 2")
3 > 2
if 3 < 2:
print("3 is less than 2")
3 < 2
These have the syntax:
if <boolean expression>:
<code to execute if boolean expression is True>
else:
<code to execute if boolean expression is False>
if 3 > 2:
print("3 is greater than 2")
else:
print("3 is less than or equal to 2")
if 3 < 2:
print("3 is less than 2")
else:
print("3 is greater than or equal to 2")
if b < a:
a = a - b
else:
b = b - a
These have the syntax:
if <boolean expression 1>:
<code to execute if boolean expression 1 is True>
elif <boolean expression 2>:
<code to execute if boolean expression 2 is True>
...
else:
<code to execute if none of the boolean expressions is True>
color = 'r'
if color == 'g':
print('green')
elif color == 'r':
print('red')
else:
print('blue')
These have the syntax:
while <boolean expression>:
<code to execute while boolean expression is True>
n = 10
while n > 0:
print(n)
n -= 1
print("Blast off!")
n # n has the final value assigned to it before the while loop terminated
Anything after a # on the same line is ignored
# This is a comment
print(1)
# comment is ignored
print(2)
The syntax is:
def <function name>(<parameter list>):
"Optional documentation string"
<function code>
The "return" statement inside a function terminates the function, and returns the given value
def add(x, y):
"Adds x and y"
return x + y
add(10, 20) # Calling the "add" function with x = 10, y = 20
z = add(10, 20) # z gets assigned the value returned from "add"
z
def add2(x, y):
"Adds x and y"
print(x + y) # "print" prints a value to the screen, but doesn't return it
add2(10, 20)
w = add2(10, 20)
w # "add2" doesn't return a value, so w doesn't get assigned a value
print(w)
not 10 == 20
6 < 5 and 2 < 3
6 < 5 or 2 < 3
6 != 5 or 5 > 4 and 4 == 3
not 3 < 2 or 3 > 2
not (3 < 2 or 3 > 2)
x = 2
1 < x < 3
1 < x and x < 3 # This is equivalent to 1 < x < 3
PEMDAS, just like high school:
3 + 6**2 # Exponents get evaluated first
(3 + 6)**2 # Use parentheses to manually control the order of operation