Python Basics Flashcards

Covers the basics of programming: - Basics of programming - Flow Charts - Variables and data types - Conditional statements - Loops and functions - Patterns - Functions - Lists - Searching and sorting lists - Strings - Tuples - 2D arrays - Dictionaries

1
Q

Why Python?

A
  • It is a great starting language, easy to use and understand
  • It is very versatile as a general purpose language, it can be used for various type development and data purposes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What data types are there?

A
  • integer (numbers)
  • string (text)
  • float (decimals)
  • boolean (true or false value)

note : data types can only be compared with the same data type

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

What are variables & how are they stored?

A
  • A storing mechanism for values
  • Python stores these variables in containers and points to the relevant container using ‘addresses’. The values -5 to 256 are the most commonly used. Therefore, for memory optimisation they always have the same address to avoid memory duplication. When the value is changed, the address will change to point towards the new container.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does type() do?

A
  • This will return the type of data of the selected object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does id() do?

A
  • This will return the address of where the selected element is stored
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does input() do?
What does print() do?

A
  • Input() will prompt the user for input
  • Print() will display the selected values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does if, elif and else do?

A
  • If will enter into a certain piece of code if a condition is met
  • elif is used after if statements to see if the previous condition wasn’t met, then check if a new condition was met, if yes then enter into a certain code
  • else if none of the other statements are met, execute this code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between a while loop and a for loop?

A
  • A while loop will keep going through a code until a certain condition is met, then it will stop.
  • A for loop will traverse through a selected range of values, from a specified start and end point, and is instructed at what ‘step rate’ to travel through the range
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does break and continue do?

A
  • Break allows you to exit a loop when an external condition is met. Normal program execution resumes at the next statement
  • Continue is used to end the current iteration in a loop and continues to the next iteration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will the value of i be on the 2nd, 3rd and 5th loop?

for i in range(0,10,3) :
print(i)

A
  • 2nd loop will be 3
  • 3rd loop will be 6
  • There will be no 5th loop :)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What value will return from…

print(len(‘Python’))

A
  • The len() function counts the length of the element/list and returns that as a value.
  • print(len(‘Python’)) will return 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the functions of round() and abs()?

A
  • round() will round the number to the nearest whole number
  • abs() will turn the value into the absolute number, i.e -2.9 will become 2.9
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What syntax is used to create a reusable function and why use them?
Extra point, what is a default parameter?

A
  • def function name(variables used in function)
  • functions are used to avoid repetition of code, have better
    readability and testability, making it easier to problem solve
  • An example may look like, def taxCalculator(a, b, c=0)
    ^where c is a default parameter, meaning that if no value is
    provided, it will default to that value, otherwise there would be an
    error if no value provided
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Where is return used?

A
  • Return is used in functions to extract a value from it and assign it to
    a variable, other wise the value stays stored inside of the function
    as a local variable.
  • It will also act as a break, once return is hit it will move to the next code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create a list?

A
  • list_name = [10, 32, 12, 55, 76]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can a single list store multiple data types ?

A
  • Yes, a list can store multiple data types in a single list. It stores them
    by ‘indexing’. Assigning a value for each element, starting at 0
    incrementing by 1 each time. The index then acts as a guide for
    which address it should point to when retrieving the data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What will happen in the following code:

age = [10, 32, 12, 55, 76]

print(age[2:3])
print(age[0:4:2])
print(age[2::])
print(age[1:-1])

A
  • it will print in this order:
  • [12]
  • [10, 12]
  • [12, 55, 76]
  • [32, 12, 55]

This is called slicing and the syntax is:
[starting index: ending index: step rate]
note, the ending index is never included, it stops there and doesn’t print it. You can also slice strings too!

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

What do the following codes do?

  1. list.append()
  2. list.insert()
  3. list.extend()
  4. list.remove()
  5. list.pop()
A
  • list.append() adds the element to the end of the list
  • list.insert(index, value) adds the element to a specific index in a list
  • list.extend() adds multiple values to a list
  • list.remove() will remove the first occurrence of the element in a list
  • list.pop() will remove the element at the end of the list, unless the index has been specified
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Immutable or mutable?

  1. Strings
  2. Lists
  3. Dictionaries
  4. Tuples
A
  • Strings are immutable
  • Lists are mutable
  • Dictionaries are mutable
  • Tuples are immutable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is binary searching and how does it work?

A
  • Binary searching is a searching method, on the basis of divide an conquer, it works on numerically sorted lists
  • It finds the middle value and cuts the list in half, creating new upper and lower bounds of the lists. It then compares the new middle value to see if it equals to, or is higher or lower then the element we are searching for. Then it can decide which side of the list the number would sit in, and repeat the process until it is confirmed if the value is in the list
  • The formula to find the middle value is (LowerBoundIndex + UpperBoundIndex) / 2
  • This process saves computational power
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Explain the process of selection sort

A
  • Selection sort is used to sort a list.
  • It breaks down the list into two parts, sorted and unsorted. The first value is put into the ‘sorted’ part and assumed the minimum value for now
  • The assumed minimum value is then compared one at a time against the rest of the list in index order, and if the comparison shows the new value is smaller, it’s added to the front of the sorted section and becomes the new assumed minimum value. Then repeats this process
  • Once the minimum value is compared with all numbers and confirmed to be the minimum value, the comparison process begins again with the next index to find the 2nd smallest value, and again with the 3rd, 4th, 5th….until the whole list is sorted
22
Q

What is bubble sorting and how does it work?

A
  • Bubble sort is a sorting method
  • It looks into pairs of adjacent elements (index next to each other), and compares them one at a time, it will then swap the elements if the comparison shows the first element is larger
  • It repeats this cycle until the list is sorted, based on the length of the list
23
Q

What are triple quotes for? ‘’’ ‘’’

A
  • Triple quotes are use for creating multi-line strings
23
Q

What are unicodes?

A
  • Unicodes are unique codes and the system that python uses to store and reference strings, for a random example : 100011010110101001010
24
Q

How would you find out if a string starts with a specific letter/letters?

A
  • To find if a string starts with a letter, you use the startswith() in the syntax value.startswith(‘s’)
  • It will return a True or False boolean value
  • You can also use it to check certain indexes of strings
25
Q

What do the .lower() and .upper() functions do?

A
  • .lower() will convert the string into lower case
  • .upper() will convert the string into upper case
  • value.lower()
26
Q

What happens on the backend when you compare 2 strings?

A
  • When comparing 2 strings, every character has a value, know as ascii values. Even lower and upper case have different values. It will then compare them both and return either True or False
27
Q

What will happen in the following code?

name = ‘don said my name is don’
name2= name.replace(‘don’, ‘Jamie’)

A
  • It will replace all instances of ‘don’ with ‘Jamie’, so ‘Jamie said my name is Jamie’
    -You can chose to only replace a set number of instances by adding the number to the end, ie name2= name.replace(‘don’, ‘Jamie’, 3) would replace the first 3 instances.
28
Q

Describe what the .find function does in Python

A
  • It is used to find an index of a substring
  • value.find(‘core’,1,7) - this will search the value between indexes 1 and 7 to see if ‘core’ is found there. Removing the numbers will search the whole string
29
Q

How do you do a quick check for a substring in a string and print yes or no?

A
  • if ‘ell’ in ‘hello’ :
    print(‘yes’)
    else:
    print(‘no’)
30
Q

Whats the difference between character wise iteration and index wise iteration?

A
  • Character wise iteration is iterating along a string
  • Index wise iteration is iterating using the index
31
Q

What is a 2D list?

A
  • A 2D list is a list inside of a list
  • Each list has a specific index and the index inside of each list start at 0
32
Q

How would you make a change to a 2D list?

A
  • listname[list_Index][element_Index] = new value
33
Q

What is a jagged list?

A
  • Jagged list is a term used when lists in a 2D lists have a different number of elements inside of them
34
Q

Describe list comprehension

A
  • List comprehension is a shortcut to build new lists, reducing the amount of code needed
  • They operate under the syntax : name = [output loop condition]
  • e.g new = [ele for x in list if ele ==x] this will make a new list with all elements where ele == x is true
35
Q

How do you do list comprehension for 2D lists?

A
  • names = [‘jamie’, ‘harshit’]
    new = [[ s for s in ele] for ele in li]
  • This example will produce : [[‘j’, ‘a’, ‘m’, ‘i’, ‘e’], [‘h’, ‘a’, ‘r’, ‘s’, ‘h’, ‘i’, ‘t’]]
  • It begins traversing through the list, gets the first value (‘jamie’) and iterates across that to create the first list, it will then go back into the for loop, to the next index (‘harshit’) and do the same again until gone through the whole list
36
Q

How would you create a 2D list based on inputs from a user?

A
  • n = int(input())
    li = [[int(j) for j in input.split()] for i in range n]
  • n acts as a range for the number of lists to produce, it then cycles through the loop splitting each input into it’s own index and creating a list
  • Note this is more advance Python
37
Q

What will ‘ab’.join(‘123’) produce?

A
  • 1ab2ab3
  • It joins the strings one character at a time and cycles back for the next character, until it reaches the final character and adds it alone.
    -1ab id added, 2ab is added, 3 is added
38
Q

What is a tuple and it’s characteristics?

A
  • Tuples are very similar to lists
  • Tuples use () instead of []
  • If you write li = 3, 4, 5 it will form a tuple (3, 4, 5)
  • To access elements in a tuple you still use [] and indexing
  • Tuples are immutable
  • You can still use [start_index:end_index:steps} for slicing tuples
39
Q

What will be produced from the following code?

a = (1, 2 ,3)
b = (4, 5, 6)

print(a+b)
print(max(a)
print(a,b)

A
  • print(a+b) will produce : (1, 2, 3, 4, 5, 6) in a single tuple
  • print(max(a)) will produce : 3
  • print a,b will produce : (1, 2, 3) (4, 5, 6) in separate tuples
  • The max function in tuples will return the largest value in the tuple
  • Tuples and maths works :)
40
Q

What is the function of ‘*more’ in the following code?

def calculator(a,b, *more)

A
  • This is called variable length input, it’s used for when you need to be able to add more inputs into a function, and it may not be set in stone how many there are
  • You can add as many values you like into the *more function and they will be put into a tuple that can be used
41
Q

How do you return multiple values from a function?

A
  • Simply on the return enter the values you wish to return separated by a comma, ie return a, b c
  • When calling the function assign a variable in order for the values, example d, e, g = function(values). This will assign a to d, b to e and c to g.
42
Q

What is a dictionary and it’s charateristics?

A
  • A collection of elements
  • They are mutable
  • They store data in key value pairs, instead of indexing. It looks like { ‘the’ : ‘jamie’} - where ‘the’ is the key and ‘jamie’ is the value. It can be a mixture of data types in the same dictionary
43
Q

What is variable a an example of?

a = {
‘the’ : ‘jamie’,
100 : ‘harshit’,
‘now’ : 450
}

A
  • Variable a is holding a dictionary
44
Q

What will y look like?

x = {
‘times’ : 10,
100 : ‘flip’,
25 : 450
}
y = x.copy()

A
  • y will be a copy of x, the command .copy() creates an entire copy of the original dictionary
45
Q

What is the purpose of this code? What will c become?

c = dict( [ (350, ‘yes’), ‘a’, ‘pear’), (122, 250) ] )

A
  • The purpose of this code is to make a dictionary using a list. c will become a dictionary from the list.`
46
Q

What do these functions do?

name.keys()
name.value()
name.item()

A
  • .keys() returns all of the keys in a dictionary
  • .value() returns all of the value in a dictionary
  • .item() returns everything in the form of tuples
47
Q

What do these functions do?

name.fromkeys()
name.get()

A
  • .fromkeys() creates a dictionary from keys entered, they can also be assigned a value but if they are not, it will have the keys there and the value will be ‘none’
  • .get() you enter a key and it will return the value in the pair .get(‘li’,0), if the key ‘li’ is not found, it will give the value 0 instead of an error. Good for if you’re unsure if the key exists
48
Q

How do you change the value or add an item to a dictionary ?

A
  • To add or change a value in a dictionary you use the syntax name[key] = value
  • It will add a new key and value to the dictionary, else if the key exists it will replace the old value with the new
49
Q

How do you swap values of variables?

A
  • Simply by getting the variables you wish to swap, and writing a, b = b, a