Python Flashcards

1
Q

#

A

Python interprets anything after a # as a comment

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

print( “ “ )

A

Function used to print whatever is put in the quotes

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

Strings

A

Blocks of text, can be within “ “ or ‘ ‘

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

Variables

A

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

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

Two common errors in python

A

SyntaxError

NameError

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

SyntaxError

A

Means there is something wrong with the way your program is written - punctuation, command in wrong spot, missing parenthesis

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

NameError

A

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

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

Integer

A

Data type that is a whole number

int

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

Floating point number

A

A data type that is a decimal number

float

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

Literal

A

A number that is actually a number and not a variable

print(variable + 3)

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

Changing numbers

A

Variables will not be changed when performing arithmetic

Variables will only update with =

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

Modulo

A

% 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

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

Concatenation

A

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

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

+=

A

Shorthand operator to add to the current value of a variable

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

“ “ “

A

Allows spacing of strings to multiple lines or including quote marks within your strings

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

Functions

A

Allow to group code into reusable blocks. It is a sequence of steps that can be performed repeatedly without having to rewrite the code

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

Defining a function

A

def function_name( parameter1, parameter2, parameter3, etc ):
print( … )
print( … )

Remove the indent of the line to end the function

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

Parameter vs argument

A

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

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

Types of arguments

A

Positional arguments

Keyword arguments

Default arguments

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

Positional argument

A

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

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

Keyword argument

A

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)

22
Q

Default argument

A

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)

23
Q

Variable scope

A

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

24
Q

Returns

A

def calculation(x, y)
return x * y

Functions can return a value to the program so that it can be modified or used later

25
Q

Returned function value

A

A result from a function that is stored in a variable

26
Q

Boolean expression

A

A statement that can either be true or false

It will be returned as True and not “True”. I.e. a Boolean value and not a string

It is a bool data type

Variables with a bool type are Boolean variables

27
Q

Relational operators / comparators

A

Operators that compare two items and return either true or false

Equals ==
Not equals !=

String != integer

> =
<
<=

28
Q

Conditional statement syntax

A

if is_raining:
print(“bring an umbrella”)

The : represents the Then part of the if and what comes after the colon will be executed if it is true

29
Q

Boolean operators

A

And - all conditions must be met in order to be True

Or - only one condition must be met in order to be True

Not - reverses a Boolean value
not True == False
if not x > 5:
print(“ “)

30
Q

Else statements

A

Else statements allow us to elegantly describe what we want our code to do when conditions are not met rather than using not

Else statements always appear with if

if weekday:
print(“wake up at 6:30”)
else:
print(“sleep in”)

31
Q

Else if statements

A

elif

Allow us to check multiple ifs

Will check in the order it is written

32
Q

List

A

Allows us to work with a collection of data in sequential order

Heights = [61, 70, 67, 64]

Lists can contain any data type together

It is not required for a list to have values

list = []

33
Q

List methods

A

Append - adds a single element to the end of the list - example_list.append(5)

Remove - remove an element from the list - example_list.append(5)

Add multiple - updated list = list1 + [“ex1”, “ex2”]

34
Q

List index

A

Lists are indexed and the starting index is 0

print(list[0])

We can select the last element of the list using -1 even if we don’t know the length of the list

35
Q

Replacing a value in a list

A

We can directly replace a value in a list by calling the index

List[5] = ‘strawberries’

36
Q

Zip()

A

Used to combine two lists

new_list = zip(list1, list2)

In order to print the list you need to use the list() function

print_new_list = list(new_list)

37
Q

.count()

A

A list method to count the number of occurrences of an element in a list

38
Q

.insert()

A

A list method to insert an element into a specific index of a list

39
Q

.pop()

A

A list method to remove an element from a specific index or from the end of a list

40
Q

range()

A

A built in python function to create a sequence of integers

my_range = range(2, 9, 2)

Creates a range starting at 2 up to 8 counting by 2s

41
Q

len()

A

A built in python function to get the length of a list

42
Q

.sort() / sorted()

A

A method and a built in function to sort a list

.sort will modify the existing list

sorted() will create a new list

43
Q

Slicing

A

sliced_list = list[first index : index+1 of the last item we want to include]

Slice section from beginning up to n
list[:n]

Slice section from end up to n
list[-n:]

Slice section but leave remainder n
list[:-n]

44
Q

Indefinite iteration

A

The number of times the loop is executed depends on how many times a condition is met

45
Q

Definite iteration

A

The number of times the loop will be executed is defined in advance

46
Q

for loop

A

A definite iteration

for <temporary> in <collection>:</collection></temporary>

<action>
</action>

47
Q

Using range() with for loop

A

Allows to define a certain number of iterations without caring what is in the list

for temp in range(6):
print(“Learning Loops!”)

48
Q

while loop

A

An indefinite iteration

Will perform a set of instructions as long as a condition is true

while <conditional>:</conditional>

<action>
</action>

49
Q

Loop break

A

We can break a loop when a condition is met

for item in items_on_sale:
print(item)
if item == “knit dress”:
break

50
Q

continue

A

Used in loop to skip

for i in big_number_list:
if i <= 0:
continue
print(i)

this will skip negative numbers and only print positive numbers in the number list

51
Q

List comprehensions

A

We can write loops in a cleaner way in python

numbers = [ … , … , ]
doubled = [num*2 for num in numbers]
print(doubled)

We can also add if, in this case to only double negative numbers

numbers = [ … , … , ]
negative_doubled = [num*2 for num in numbers if num < 0]
print(negative_doubled)