Symbol Review Flashcards

1
Q

What does the and operator do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the with … as keyword do ?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the assert keyword do?

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does the break keyword do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does the class keyword do?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does the continue keyword do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what does the del keyword do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what does the else keyword do?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does the elif keyword do?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does the def keyword do?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what does the except keyword do

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the exec keyword do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the for keyword do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the finally keyword do?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the from keyword do?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the global keyword do?

A

The global keyword is used to create global variables from a non-global scope, e.g. inside a function.

#create a function:
def myfunction():
 global x
 x = "hello"
#execute the function:
myfunction()
#x should now be global, and accessible in the global scope.
print(x)
17
Q

What does the if keyword do?

A

The if keyword is used to create conditional statements (if statements), and allows you to execute a block of code only if a condition is True.

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”)

18
Q

What does the import keyword do?

A

The import keyword is used to import modules.

Example

Import the datetime module and display the current date and time:

import datetime

x = datetime.datetime.now()
print(x)

19
Q

What does the raise keyword do?

A
20
Q

What does the return keyword do?

A
21
Q

What does the print keyword do?

A
22
Q

What does the pass keyword do?

A
23
Q

What does the or keyword do?

A
24
Q

What does the not keyword do?

A
25
Q

What does the lambda keyword do?

A
26
Q

What does the is keyword do?

A
27
Q

What does the in keyword do?

A
28
Q

What does the try keyword do?

A
29
Q

What does the while keyword do?

A
30
Q

What does the with keyword do?

A
31
Q

What does the yield keyword do?

A
32
Q

what are the built in datatypes for binary?

A

Binary Types:

bytes, bytearray, memoryview

33
Q

what are the built in datatypes for booleans?

A

Boolean Type:

bool

34
Q

what are the built in datatypes for sets?

A

Set Types:

set, frozenset

35
Q

what are the built in datatypes for mapping?

A

Mapping Type:

dict

36
Q

what are the built in datatypes for sequences?

A

Sequence Types:

list, tuple, range

37
Q

what are the built in datatypes for numerics?

A

Numeric Types:

int, float, complex

38
Q

what are the built in datatypes for text?

A

Text Types:

str