1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-11-21 20:35:12 +00:00

Update python.md

This commit is contained in:
Yash 2021-09-25 14:31:50 +05:30
parent dbc5b69dc8
commit 71e45aa43e

View File

@ -134,7 +134,7 @@
- Lists are created using square brackets: - Lists are created using square brackets:
```py ```python
thislist = ["apple", "banana", "cherry"] thislist = ["apple", "banana", "cherry"]
``` ```
@ -147,18 +147,31 @@ thislist = ["apple", "banana", "cherry"]
- To determine how many items a list has, use the `len()` function. - To determine how many items a list has, use the `len()` function.
- A list can contain different data types: - A list can contain different data types:
```py ```python
list1 = ["abc", 34, True, 40, "male"] list1 = ["abc", 34, True, 40, "male"]
``` ```
- It is also possible to use the list() constructor when creating a new list - It is also possible to use the list() constructor when creating a new list
```py ```python
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
``` ```
- pop() function removes the last value in the given list by default.
```python
thislist = ["apple", "banana", "cherry"]
print(thislist.pop())# cherry
print(thislist.pop(0)) #apple
```
### Tuple ### Tuple
- Tuple is a collection which is ordered and unchangeable. Allows duplicate members. - Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- A tuple is a collection which is ordered and unchangeable. - A tuple is a collection which is ordered and unchangeable.
- Tuples are written with round brackets. - Tuples are written with round brackets.
```py ```python
thistuple = ("apple", "banana", "cherry") thistuple = ("apple", "banana", "cherry")
``` ```
- Tuple items are ordered, unchangeable, and allow duplicate values. - Tuple items are ordered, unchangeable, and allow duplicate values.
@ -168,16 +181,16 @@ thistuple = ("apple", "banana", "cherry")
- Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. - Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
- Since tuple are indexed, tuples can have items with the same value: - Since tuple are indexed, tuples can have items with the same value:
- Tuples allow duplicate values: - Tuples allow duplicate values:
```py ```python
thistuple = ("apple", "banana", "cherry", "apple", "cherry") thistuple = ("apple", "banana", "cherry", "apple", "cherry")
``` ```
- To determine how many items a tuple has, use the `len()`function: - To determine how many items a tuple has, use the `len()`function:
```py ```python
thistuple = ("apple", "banana", "cherry") thistuple = ("apple", "banana", "cherry")
print(len(thistuple)) print(len(thistuple))
``` ```
- To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. - To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.
```py ```python
thistuple = ("apple",) thistuple = ("apple",)
print(type(thistuple)) print(type(thistuple))
@ -186,7 +199,7 @@ thistuple = ("apple")
print(type(thistuple)) print(type(thistuple))
``` ```
- It is also possible to use the tuple() constructor to make a tuple. - It is also possible to use the tuple() constructor to make a tuple.
```py ```python
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple) print(thistuple)
@ -195,7 +208,7 @@ print(thistuple)
### Set ### Set
- Set is a collection which is unordered and unindexed. No duplicate members. - Set is a collection which is unordered and unindexed. No duplicate members.
- A set is a collection which is both unordered and unindexed. - A set is a collection which is both unordered and unindexed.
```py ```python
thisset = {"apple", "banana", "cherry"} thisset = {"apple", "banana", "cherry"}
``` ```
- Set items are unordered, unchangeable, and do not allow duplicate values. - Set items are unordered, unchangeable, and do not allow duplicate values.
@ -206,27 +219,38 @@ thisset = {"apple", "banana", "cherry"}
- Sets are unchangeable, meaning that we cannot change the items after the set has been created. - Sets are unchangeable, meaning that we cannot change the items after the set has been created.
- Duplicate values will be ignored. - Duplicate values will be ignored.
- To determine how many items a set has, use the `len()` method. - To determine how many items a set has, use the `len()` method.
```py ```python
thisset = {"apple", "banana", "cherry"} thisset = {"apple", "banana", "cherry"}
print(len(thisset)) print(len(thisset))
``` ```
- Set items can be of any data type: - Set items can be of any data type:
```py ```python
set1 = {"apple", "banana", "cherry"} set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3} set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False} set3 = {True, False, False}
set4 = {"abc", 34, True, 40, "male"} set4 = {"abc", 34, True, 40, "male"}
``` ```
- It is also possible to use the `set()` constructor to make a set. - It is also possible to use the `set()` constructor to make a set.
```py ```python
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
``` ```
- frozenset() is just an immutable version of Set. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.
```python
set1 = {"apple", "banana", "cherry"}
frzset=frozenset(set1)
print(frzset)
```
### Dictionary ### Dictionary
- Dictionary is a collection which is unordered and changeable. No duplicate members. - Dictionary is a collection which is unordered and changeable. No duplicate members.
- Dictionaries are used to store data values in key:value pairs. - Dictionaries are used to store data values in key:value pairs.
- Dictionaries are written with curly brackets, and have keys and values: - Dictionaries are written with curly brackets, and have keys and values:
```py ```python
thisdict = { thisdict = {
"brand": "Ford", "brand": "Ford",
"model": "Mustang", "model": "Mustang",
@ -234,7 +258,7 @@ thisdict = {
} }
``` ```
- Dictionary items are presented in key:value pairs, and can be referred to by using the key name. - Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
```py ```python
thisdict = { thisdict = {
"brand": "Ford", "brand": "Ford",
"model": "Mustang", "model": "Mustang",
@ -246,11 +270,11 @@ print(thisdict["brand"])
- Dictionaries cannot have two items with the same key. - Dictionaries cannot have two items with the same key.
- Duplicate values will overwrite existing values. - Duplicate values will overwrite existing values.
- To determine how many items a dictionary has, use the `len()` function. - To determine how many items a dictionary has, use the `len()` function.
```py ```python
print(len(thisdict)) print(len(thisdict))
``` ```
- The values in dictionary items can be of any data type - The values in dictionary items can be of any data type
```py ```python
thisdict = { thisdict = {
"brand": "Ford", "brand": "Ford",
"electric": False, "electric": False,
@ -259,9 +283,26 @@ thisdict = {
} }
``` ```
- pop() Function is used to remove a specific value from a dictionary. You can only use key bot the value. Unlike Lists you have to give a value to this function
```python
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.pop("model")
print(x)# Mustang
print(car)#{'brand': 'Ford', 'year': 1964}
```
### Conditional branching ### Conditional branching
```py ```python
if condition: if condition:
pass pass
elif condition2: elif condition2:
@ -278,7 +319,7 @@ thisdict = {
#### While loop #### While loop
- With the `while` loop we can execute a set of statements as long as a condition is true. - With the `while` loop we can execute a set of statements as long as a condition is true.
- Example: Print i as long as i is less than 6 - Example: Print i as long as i is less than 6
```py ```python
i = 1 i = 1
while i < 6: while i < 6:
print(i) print(i)
@ -296,7 +337,7 @@ while i < 6:
- This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. - This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
- With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. - With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
```py ```python
fruits = ["apple", "banana", "cherry"] fruits = ["apple", "banana", "cherry"]
for x in fruits: for x in fruits:
print(x) print(x)
@ -310,7 +351,7 @@ A nested loop is a loop inside a loop.
- The "inner loop" will be executed one time for each iteration of the "outer loop": - The "inner loop" will be executed one time for each iteration of the "outer loop":
```py ```python
adj = ["red", "big", "tasty"] adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"] fruits = ["apple", "banana", "cherry"]
@ -320,19 +361,19 @@ for x in adj:
``` ```
- for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error. - for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.
```py ```python
for x in [0, 1, 2]: for x in [0, 1, 2]:
pass pass
``` ```
### Function definition ### Function definition
```py ```python
def function_name(): def function_name():
return return
``` ```
### Function call ### Function call
```py ```python
function_name() function_name()
``` ```