Symbol Review Flashcards
What does the and operator do?
Returns True if both statements are true
x = 5
print(x > 3 and x < 10)
returns True because 5 is greater than 3 AND 5 is less than 10
What does the with … as keyword do ?
The with … as keyword provides context management around the block of code that follows.
A common example is file handling where after the with code block finishes Python will close the file automatically for you.
e.g.
with open("MyTextFile.txt",'r') as myfile: for line in myfile: print(line)
What does the assert keyword do?
The assert keyword is used when debugging code. It asserts / assures that something is true.
The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.
You can write a message to be written if the code returns False, check the example below.
x = “hello”
#if condition returns True, then nothing happens: assert x == "hello"
#if condition returns False, AssertionError is raised: assert x == "goodbye"
what does the break keyword do?
break exits a loop - stop / exit this loop right now
for i in range(9): if i \> 3: break print(i)
0
1
2
3
what does the class keyword do?
The class keyword defines a class. Below is an example of a class that uses the BIF _init_ and defines another method / function
class Person: def \_\_init\_\_(mysillyobject, name, age): mysillyobject.name = name mysillyobject.age = age
def myfunc(abc): print("Hello my name is " + abc.name)
p1 = Person(“John”, 36)
p1.myfunc()
what does the continue keyword do?
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.
for i in range(5): if i == 3: continue print(i)
0
1
2
4
what does the del keyword do?
The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.
Delete the first item in a list:
x = [“apple”, “banana”, “cherry”]
del x[0]
Delete a class:
class MyClass: name = "John"
del MyClass
delete a variable:
x = “hello”
del x
what does the else keyword do?
The else keyword catches anything which isn’t caught by the preceding conditions.
if: X
elif: Y
else : Z
a = 200
b = 33
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)
else:
print(“a is greater than b”)
what does the elif keyword do?
it’s an else if construct
if: X
elif: Y
else : Z
a = 33
b = 33
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)
what does the def keyword do?
def defines a function
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
In Python a function is defined using the def keyword. To call a function, use the function name followed by parenthesis:
def my\_function(): print("Hello from a function")
my_function()
what does the except keyword do
If an exception happens do this.
The except keyword is used in try…except blocks. It defines a block of code to run if the try block raises an error.
You can define different blocks for different error types, and blocks to execute if nothing went wrong, see examples below.
(x > 3) will raise an error because x is a string and 3 is a number, and cannot be compared using a ‘>’:
x = “hello”
try:
x > 3
except NameError:
print(“You have a variable that is not defined.”)
except TypeError:
print(“You are comparing values of different type”)
What does the exec keyword do?
The exec() function executes the specified Python code.
e.g.
x = ‘name = “John”\nprint(name)’
exec(x)
will display
John
when the exec function runs. The exec() function accepts large blocks of code.
What does the for keyword do?
The for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Examples
Print each fruit in a fruit list:
fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x)
Loop through the letters in the word “banana”:
for x in “banana”:
print(x)
What does the finally keyword do?
The finally keyword is used in try…except blocks. It defines a block of code to run when the try…except…else block is final.
The finally block will be executed no matter if the try block raises an error or not.
This can be useful to close objects and clean up resources.
e.g.
try:
x > 3
except:
print(“Something went wrong”)
else:
print(“Nothing went wrong”)
finally:
print(“The try…except block is finished”)
What does the from keyword do?
The from keyword is used to import only a specified section from a module.
Example
Import only the time section from the datetime module, and print the time as if it was 15:00:
from datetime import time
x = time(hour=15)
print(x)