Python Flashcards
#
Python interprets anything after a # as a comment
print( “ “ )
Function used to print whatever is put in the quotes
Strings
Blocks of text, can be within “ “ or ‘ ‘
Variables
We can store data for reuse, variables are assigned by using =
Variables cannot have spaces or symbols in their names, only underscore can be used as a space
Two common errors in python
SyntaxError
NameError
SyntaxError
Means there is something wrong with the way your program is written - punctuation, command in wrong spot, missing parenthesis
NameError
Occurs when the Python interpreter sees a word it does not recognize
Code that contains something that looks like a variable but was never defined
Integer
Data type that is a whole number
int
Floating point number
A data type that is a decimal number
float
Literal
A number that is actually a number and not a variable
print(variable + 3)
Changing numbers
Variables will not be changed when performing arithmetic
Variables will only update with =
Modulo
% operator
Gives the remainder of a division calculation
If the two numbers are divisible then the result will be 0
This is good in programming for use if we want to perform an action every nth time
Concatenation
The + operator can be used to combine variables that are strings together
If we want to combine a number and string together and store it as a variable then we need to convert the number variable to a string using str()
If we just want to print the variables we do not need to convert the number variable to a string. We can simply use commas in the print operator - print( , , )
+=
Shorthand operator to add to the current value of a variable
“ “ “
Allows spacing of strings to multiple lines or including quote marks within your strings
Functions
Allow to group code into reusable blocks. It is a sequence of steps that can be performed repeatedly without having to rewrite the code
Defining a function
def function_name( parameter1, parameter2, parameter3, etc ):
print( … )
print( … )
Remove the indent of the line to end the function
Parameter vs argument
A parameter is defined with the function and is treated as a variable
The argument is the data that is passed into the function in the form of that variable when the function is called using the argument
Types of arguments
Positional arguments
Keyword arguments
Default arguments
Positional argument
Standard way of definition. The order in which you enter your arguments will be assigned according to the order in which the parameters were defined
Keyword argument
When entering your argument, you bypass the positional definition of the parameters and simply call out the definition directly in the function call
def function(x, y, z)
function(y=5, z=3, x=1)
Default argument
When defining the function we can assign a default value to the parameter
When calling the function we can enter no argument at that position to keep the default or enter a value to replace the default
def function(x, y=10)
function(x)
Variable scope
If a variable is defined outside of a function then it can be used anytime
If it is only defined within a function then it can only be used when calling the function
Returns
def calculation(x, y)
return x * y
Functions can return a value to the program so that it can be modified or used later