Python References
CLick on the button below for a quick reference to common python programming structures.
Keyboard Input
Use the input() function to read text from the keyboard. When the Enter key is hit, all characters typed will be returned as a string. You will need to store them in a variable. If you are reading in numeric data you will need to convert it using float() or int().
Make sure to provide the user with a prompt before asking for data. You can either provide a prompt using a print() statement on the line above the input() function or passing a string as an argument to the input() function (as shown in the examples below).
Ex. Reading in a string:
name = input("Please enter your name: ")
You may want to prevent the user from putting incorrect input by automatically converting case for them. This example below ensures that even if the user types a 'q', it will be stored as a 'Q'.
choice = input("Please enter Q to quit").upper()
Ex. Reading in an integer:
age = int(input("Please enter your age: ")) #You must convert the string to an integer
Ex. Reading in a decimal number:
price = float(input("Please enter the price: ")) #You must convert the string to an float
If Statements and Boolean Operators
See this link for a detailed explanation or search it on your own:
https://www.w3schools.com/python/python_conditions.asp
The condition of an if statement must be either a boolean statement, or something that the computer can evaluate to true or false. Only indented code will be treated as in the body of the if statement.
Here are the equality operators:
== equals (notice it is two equals signs)
!= does not equal
< , <= less than, less than or equal to
>, >= greater than, greater than or equal to
Here are the boolean operators:
not : This will make a true be false and a false be true.
Example:
name = "Barney"
print(name == "barney") # Prints false because string comparison is case sensitive
print(not(name == "barney")) # Prints true
and : The entire boolean statement will be true only if BOTH conditions on either side of the and operator are true.
Example:
age = 19
name = "Gary"
print(name == "Gary" and age == 19) # Prints true
print(name == "Gary" and age < 19) # Prints false
or : The entire boolean statement will be true if AT LEAST ONE conditions on either side of the or operator is true.
age = 19
name = "Gary"
print(name == "Gary" or age == 19) # Prints true
print(name == "Gary" or age < 19) # Prints true
print(name == "gary" or age < 19) # Prints false
When comparing strings, use upper() or lower() to convert to a constant case to make comparison easier.
Example:
color = input("Please enter your favorite color: ").lower()
if (color == "blue"):
print("color is either Blue, blue, BLUE or any other permutation of case")
else:
print("name is not Steve")
For more details see:
Loops
Counted (for) Loops
https://www.w3schools.com/python/python_for_loops.asp
For loops always end BEFORE the end condition.
for i in range(10):
print(i, end = "") # Prints: 0 1 2 3 4 5 6 7 8 9
for i in range (1, 10):
print(i) # Prints: 1 2 3 4 5 6 7 8 9
for i in range(1, 10, 2):
print(i) # Prints: 1 3 5 7 9
Looping through a list
Method 1 - By Index:
for i in range(len(names)):
print(names[i])
Method 2 - By Item:
for name in names:
print(name)
Conditional (while) Loops
https://www.w3schools.com/python/python_conditions.asp
while (condition):
# Indented code will loop as long as condition is true.
# 'condition' can be a boolean value/variable or a boolean expression.
Lists
https://www.w3schools.com/python/python_lists.asp
Create a new empty list:
names = []
Add to the List:
names.append("Toopy")
names.append("Beanoo")
Get the number of items in the list:
len(names)
Access a name in the list:
names[0] # Access the first item in the list
names[0] = "Gary" # Changes the item at index 0 in the list
print(names[0]) # Prints the first item in the list
print(names[len(names) - 1] # Prints the last item in the list
Remove a name in the list:
names.pop(1) # Removes the item at index 1
names.remove("Gary") # Removes 'Gary' if it is in the list
Loop through a List
Method 1 - By Index:
for i in range(len(names)):
print(names[i])
Method 2 - By Item:
for name in names:
print(name)
You cannot change an item in the list using this method, however, you can change its attributes if it is an object.