Print is pretty much the same as
echo in Bash and PHP and
puts in Ruby
Function to get information from a user. Always gets a string, even if the content is a number.
Variables in Python can not start with a number
Save input as variable
Conditionals (if, else)
Your if statement will work with or without the parentheses
( ) but since 2010, the style guide recommends using them. So
if (name == "Aamnah"): is preferred over
if name == "Aamnah":
Combining multiple conditions with
Here is an example
will output ‘Of course you are! You’re very honest.’ if you answer with y or yes. If you answer with anything else, it’ll say Lier Lier!
String Formatting / Replacement
When you divide two numeric values, you always get a float as result. Floats work mostly how they are supposed to but occasionally you’ll find some odd problems. For example
should result 0 but gives 5.551115123125783e-17 instead.
Make numbers from strings
When you take input in, it always takes it as a string. So you’d have to convert it to an int or float first in order to do any kind of calculation on it.
Convert floats to integers and vice versa
Round floats to whole numbers
round() rounds a float to the nearest whole number.
round(2.4) will become 2 and
round(3.9) will become 4.
Check if something is in or not in something else. For example,
'a' in 'Aamnah' would return
'b' not in 'Aamnah' would return
'x' in 'Aamnah' would return
Check List’s length
Make Lists from String
Check if value in List
Append to list
We can only add lists to other lists.
.append() add to the end of a list.
.split() on a string breaks the string up on whitespaces. If we had Returns or Tabs, they’d also break.
You can determine what is used to join the list. In the example above, we used spaces. We can also use
- or something else.
Everything we are joining has to be a string. We can’t join numbers.
See More examples
While vs. For
for loop can only iterate (loop) “over” collections of things. A
while loop can do any kind of iteration (looping) you want. However,
while loops are harder to get right and you normally can get many things done with
else in Loops
elsestatement is used with a
forloop, the else statement is executed when the loop has exhausted iterating the list.
When the above code is executed, it produces the following result:
0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5
break makes Python stop whatever loop it is in, which works really well with infinite loops.
continue let’s us move on to the next step in the loop.
open() - Opens a file in Python. This won’t contain the content of the file, it just points to it in memory.
You can also specify encoding
For ease of use, save the file in a varibale
open() you don’t get the actual contents of the file, you just get a pointer to the file. To get the contents, you would use
read() gets the contents of a file for you. For ease of use, you can save the content in a variable.
Once you have opened the file and read it, you should close it.
Closing the file prevents it from taking up memory.
Modules (aka Libraries) and Packages
There are loads of built-in modules available for Python. You can find a list here. You use the keyword
import to bring outside libraries into your code.
You load a library/module using
You can specify multiple modules in one import statement
Functions follow the same naming rules as variables. You can’t start a function name with a number and you can’t put any hyphens or special characters in the name. We use the keyword
def for defining every function, like so
Taking arguments is also easy
Getting back data using
return for when you want to get data back from a function and not just print it.
Here is an example of a shopping list with a function to add list items, another function to show help, and a function to show list.
Collections are variable types that collect different types of data together. They are also called
iterables because you iterate or loop through them.