python coding notes Flashcards

1
Q

how to find where a character is

A

.index(character)

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

how to remove something from a list

A

list.pop(position of char)

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

different ways to show range

A
  • for i in range
  • num for num
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

different ways to do loops

A
  • for i in
  • char for char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to find the number of time something occurs in a list

A

.occur()

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

how to sort list

A
  • sorted(list)
  • list.sort()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to find length of something

A

len()

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

how to insert an element into a specific index of a list

A

list.insert()

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

how to skip numbers when doing range

A
  • add a third input, eg range(2,9,2) would output 2,4,6,8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to slice lists

A

list[start:end]

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

how to see how many times an element occurs in a string or list

A

.count

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

how to remove duplicates in a list

A

use list(set())

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

what is a tuple

A

a data structure that allows us to store multiple pieces of data inside of it
- the elements, the order of the elements and how many elements there are cannot be changed
- they are immutable, can’t be edited

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

what does zip() do

A

takes two or more list as inputs and returns an object that contains a list of pairs. each pair contains one element from each of the inputs

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

what is indefinite iteration

A

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

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

what is definite iteration

A

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

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

what does a while loop do

A

performs a set of instructions as long as a given condition is true

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

what does break do

A

stops an iteration

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

how to carry on an iteration

A

continue

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

how to iterate using a list

A

[(what u wanna append to the list) for i in list]

21
Q

string in built functions

A

.strip() - removes characters from string
- .find() - finds the position of a character in a string

22
Q

how to reverse a list

A
  • list.reverse()
  • Reverse = True
  • [::-1]
23
Q

what does enumerate do()

A

generates tuples which represent the indices and values of the elements in the list

24
Q

what does while True do

A

loops forever

25
what does try do
lets you test a block of code for errors.
26
how to check if something is a digit
.isdigit()
27
how to repeat a loop, for example in a game
use While True
28
difference between pop and remove
remove removes using values pop removes using indexes
29
if string.strip() what does this mean
checks if the input string is not empty after removing leading and trailing whitespace
30
other ways to remove white space in sentence
- “”.join(sentence.split()) - sentence.replace(“ “, “”)
31
what else does list()
- converts a string into a list, where each character in the string is a separate element in the list - eg hello is [“h”, “e”, “l”, “l”,”o”]
32
what does zip do
used to combine two or more iterables, eg an integer and string, or string and a list - eg - for i,j in zip(str(n)), range(1,len(str(n))+1) , this combines a string and an integer
33
how to do square root
math.sqrt() but import math before u do this
34
how to check if a number is whole
mod it by 1
35
how to make a number whole
round()
36
what does abs() do
gives the absolute value of a number, - turns negative numbers positive, eg -5 to 5 - leaves positive numbers unchanged
37
what does set() do
A set automatically removes duplicate elements. Each element in a set must be unique. Sets do not maintain any specific order of elements. you can add or remove elements after the set has been created.
38
how to find the position of a character in a string/list
use variablename.index(character) - eg. if theres a string called letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and you want to find where X is: for char in userInput: if char in letters: current_index = letters.index(char)
39
when to use try and except
The try and except block is used to handle situations where the user inputs something invalid (like entering letters instead of a number) 1. Handling User Input Errors When converting user input into a specific data type, errors may occur if the input is invalid. eg. try: age = int(input("Enter your age: ")) except ValueError: print("Invalid input! Please enter a number.")
40
when to use a for loop
You Want to Continuously Prompt the User Until Valid Input Is Provided: For example, asking a user for a valid number or email address until they provide one. Loops make it easy to repeat the request. You Need Custom Validation Logic: For example: - Checking if a string contains only specific characters. - Ensuring input meets specific constraints (e.g., a number in a range). - A loop allows you to write your own logic instead of relying solely on Python’s built-in exception system. Errors Are Not Easily Capturable by Exceptions: - For example, checking if a string contains only letters is not something that throws an exception—you need to check this explicitly using string methods. You Want More Granular Feedback: - If you want to inform the user why their input is invalid (e.g., "Value is too large" or "Must be a number"), loops can provide more flexibility.
41
how to do a factorial recursion function
def factorial(x): if x == 1: return 1 else: return x * factorial(x -1)
42
how to check if any character in an input is a digit
if not any(char.isdigit() for char in userInput):
43
Write a function that sorts a list of numbers based on the sum of their digits in descending order. - The function should return the sorted list.
def sum_digits(x): count = 0 for i in str(x): count+= int(i) return count def digit_sum_sort(inputList): newList = [] for i in inputList: newList.append((i, sum_digits(i))) newList.sort(key=lambda item : (item[1], item[1]), reverse=True) sortedList = [item[0] for item in newList] return sortedList print(digit_sum_sort([134, 23, 45, 98, 321, 500]))
44
how to sort a parts of a tuple in descending order
newList.sort(key=lambda item : (item[1], item[1]), reverse=True) sortedList = [item[0] for item in newList]
45
when to use while True opposed to while(a condition)
Use while True when: - You don’t know beforehand when the loop will stop - eg: Finding the smallest multiple (we don’t know the number in advance) - You want to keep asking for user input until they enter valid data -eg: Keep asking for a number until the user enters a valid one. Use while (a condition) when: - You know the stopping condition in advance eg: Count down from 10 to 1 - You have a specific range or limit eg: Running a loop only if a number is less than 50. python Copy Edit
46
how to convert digit to a list
list(map(int, str(n)))
47
how to copy a list
newlist=list**[:]**
48
how to use a global variable
- define it before ALL the functions - do not include it in a functions parameters. Instead, say the global variable in the first line of the function
49
difference between break and continue
break stops a loop once its condition is met, while continue skips over the current iteration of a loop and continues to the next iteration based on its condition.