python coding notes Flashcards

You may prefer our related Brainscape-certified 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

18
Q

what does break do

A

stops an iteration

19
Q

how to carry on an iteration

A

continue

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
Q

what does try do

A

lets you test a block of code for errors.

26
Q

how to check if something is a digit

A

.isdigit()

27
Q

how to repeat a loop, for example in a game

A

use While True

28
Q

difference between pop and remove

A

remove removes using values
pop removes using indexes

29
Q

if string.strip() what does this mean

A

checks if the input string is not empty after removing leading and trailing whitespace

30
Q

other ways to remove white space in sentence

A
  • “”.join(sentence.split())
  • sentence.replace(“ “, “”)
31
Q

what else does list()

A
  • 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
Q

what does zip do

A

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
Q

how to do square root

A

math.sqrt() but import math before u do this

34
Q

how to check if a number is whole

A

mod it by 1

35
Q

how to make a number whole

A

round()

36
Q

what does abs() do

A

gives the absolute value of a number,
- turns negative numbers positive, eg -5 to 5
- leaves positive numbers unchanged

37
Q

what does set() do

A

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
Q

how to find the position of a character in a string/list

A

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
Q

when to use try and except

A

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
Q

when to use a for loop

A

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.