Python Flashcards
How to run a python file?
Navigate to file location in terminal. run “python file.py”
What’s a variable in Python?
Variables are labels. They reference a value.
What are the rules for naming variables in Python?
- They can only contain letters, numbers, and underscore
- They can start with a letter or an underscore, but not a number
- Spaces are not allowed in variables
- Avoid using Python keywords such as print or class
- Variables should be short and descriptive
What’s a string in Python?
In programming, programs collect data and do something with them. Since they are different types of data, it helps to differentiate the different types.
A string is a data type. A string is a series of characters. Anything inside a single or double quote is a string.
Examples of string:
- “this is a string”
- ‘this is a string’
What is the string method title do in Python?
It capitalizes each first letter of a string
```python
name = “james leveille”
print(name.title())
~~~
What is the string method upper do in Python?
upper case a string
name.upper()
What is the string method lower do in Python?
It lower case a string
name.lower()
How to use variables in a string in Python?
By using the f keyword to format the string.
print(f”{fName} {lName}”)
How to add a tab in a string in Python?
\t
print(“\tname”)
How to add a new line in a string in Python?
\n
print(“\nname”)
How to remove write space from a string in Python?
name = “ James “
// remove left whitespace
name.lstrip()
// remove right whitespace
name.rstrip()
// remove left and right whitespace
name.string()
How to remove a part of string from a string in Python?
remove prefix
name = “James”
name.removeprefix(“Ja”)
# remove suffix
name.removesufix(“es”)
What’s a list in Python?
A list is a way of storing a list of items. Square brackets ([]) are used to create a list. Lists in python are zero index-based. You can access them by passing an index.
bicycles = ['trek', 'cannondal', 'redline', 'spcialized']
How do you access an element of a list in Python?
bicycles[0]
How do you access the last element of a list?
you can use Python special syntax bicycles[-1]. If you pass -1, python will return the last element in the list. -2 return the second item from the end and so forth
How do you modify an element in the list in Python?
Just set the data using the indexbicycles[0] = “LUKI”
How do you add an element to a list in Python?
- You can append the element to the end of the list.
bicycles.append('ducati')
- You can also insert the element at a certain position
bicycles.insert(0, 'ducati')
This will insert the new element and shift the rest of the list
How do you remove an item from a list in Python?
- You can use del to delete an element given its index. For example,
del bicycles[0]
will delete the first element of the list. If the index is not found, it will throw an out of index error - pop is another way you can remove an item from the list. Pop removes the last element of the list and returns the value.
popped_item = bicycles.pop()
How do you remove an item from the list given the value in Python?
You remove an item if you know the value by calling the remove method on the list. bicycles.remove(‘ducati’). This will remove the first occurrence of that value.
How to sort a list in Python?
- sort - this will sort the list permanently.
bicylces.sort()
- sorted - sorted is a function that you can call on your list to sort it.
newList = sorted(bicycles)
How do you loop through a list in Python?
bicylces = ["ice", "looki", "pit", "brake"]
```python
for bike in bicycles:
print(bike)
~~~
The colon (:
) is required.
The line after the :
must be indented
How do you loop through a list of numbers in Python?
range (k, n)
- is use to iterate over a list start at k through n-1
range (n)
- is use to iterate over a list start at 0 through n-1
```python
for value in range(1, 5):
print(value)
~~~
How do you access the last item of a list?
players = [“john”, “luke”, “peter”]. print(players[-1]will print peter
How do you convert a range to a list?
list = list(range(0, 4)). This will return [0, 1, 2, 3] because list converts a range to a list.