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
Why Python?
- 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
What data types are there?
- integer (numbers)
- string (text)
- float (decimals)
- boolean (true or false value)
note : data types can only be compared with the same data type
What are variables & how are they stored?
- 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.
What does type() do?
- This will return the type of data of the selected object
What does id() do?
- This will return the address of where the selected element is stored
What does input() do?
What does print() do?
- Input() will prompt the user for input
- Print() will display the selected values
What does if, elif and else do?
- 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
What is the difference between a while loop and a for loop?
- 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
What does break and continue do?
- 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
What will the value of i be on the 2nd, 3rd and 5th loop?
for i in range(0,10,3) :
print(i)
- 2nd loop will be 3
- 3rd loop will be 6
- There will be no 5th loop :)
What value will return from…
print(len(‘Python’))
- The len() function counts the length of the element/list and returns that as a value.
- print(len(‘Python’)) will return 6
What are the functions of round() and abs()?
- 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
What syntax is used to create a reusable function and why use them?
Extra point, what is a default parameter?
- 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
Where is return used?
- 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 do you create a list?
- list_name = [10, 32, 12, 55, 76]
Can a single list store multiple data types ?
- 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
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])
- 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!
What do the following codes do?
- list.append()
- list.insert()
- list.extend()
- list.remove()
- list.pop()
- 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
Immutable or mutable?
- Strings
- Lists
- Dictionaries
- Tuples
- Strings are immutable
- Lists are mutable
- Dictionaries are mutable
- Tuples are immutable
What is binary searching and how does it work?
- 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