2015-06-01 05:51:04 +00:00
|
|
|
---
|
|
|
|
title: Python
|
2018-12-06 22:15:40 +00:00
|
|
|
category: Python
|
2015-06-01 05:51:04 +00:00
|
|
|
---
|
|
|
|
|
2021-09-18 13:46:50 +00:00
|
|
|
### Tuples (immutable)
|
|
|
|
|
|
|
|
tuple = ()
|
|
|
|
|
|
|
|
### Lists (mutable)
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
list = []
|
2017-10-05 08:16:53 +00:00
|
|
|
list[i:j] # returns list subset
|
2018-07-20 14:40:38 +00:00
|
|
|
list[-1] # returns last element
|
|
|
|
list[:-1] # returns all but the last element
|
2021-09-18 13:46:50 +00:00
|
|
|
*list # expands all elements in place
|
2017-10-05 08:16:53 +00:00
|
|
|
|
2015-06-01 05:51:04 +00:00
|
|
|
list[i] = val
|
2019-08-12 22:41:46 +00:00
|
|
|
list[i:j] = otherlist # replace ith to jth-1 elements with otherlist
|
2015-06-01 05:51:04 +00:00
|
|
|
del list[i:j]
|
|
|
|
|
|
|
|
list.append(item)
|
2018-07-20 14:40:38 +00:00
|
|
|
list.extend(another_list)
|
2018-11-07 20:31:46 +00:00
|
|
|
list.insert(index, item)
|
2018-07-20 14:40:38 +00:00
|
|
|
list.pop() # returns and removes last element from the list
|
2018-11-07 20:31:46 +00:00
|
|
|
list.pop(i) # returns and removes i-th element from the list
|
|
|
|
list.remove(i) # removes the first item from the list whose value is i
|
2017-10-05 08:16:53 +00:00
|
|
|
list1 + list2 # combine two list
|
|
|
|
set(list) # remove duplicate elements from a list
|
2015-06-01 05:51:04 +00:00
|
|
|
|
2018-11-07 20:31:46 +00:00
|
|
|
list.reverse() # reverses the elements of the list in-place
|
2015-06-01 05:51:04 +00:00
|
|
|
list.count(item)
|
2017-10-05 08:16:53 +00:00
|
|
|
sum(list)
|
2015-06-01 05:51:04 +00:00
|
|
|
|
2018-11-07 20:31:46 +00:00
|
|
|
zip(list1, list2) # returns list of tuples with n-th element of both list1 and list2
|
2018-07-20 14:40:38 +00:00
|
|
|
list.sort() # sorts in-place, returns None
|
|
|
|
sorted(list) # returns sorted copy of list
|
|
|
|
",".join(list) # returns a string with list elements seperated by comma
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
### Dict
|
|
|
|
|
2021-09-18 13:46:50 +00:00
|
|
|
dict = {}
|
2015-06-01 05:51:04 +00:00
|
|
|
dict.keys()
|
|
|
|
dict.values()
|
2018-07-20 14:40:38 +00:00
|
|
|
"key" in dict # let's say this returns False, then...
|
|
|
|
dict["key"] # ...this raises KeyError
|
|
|
|
dict.get("key") # ...this returns None
|
2015-06-01 05:51:04 +00:00
|
|
|
dict.setdefault("key", 1)
|
2021-09-18 13:46:50 +00:00
|
|
|
**dict # expands all k/v pairs in place
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
### Iteration
|
|
|
|
|
|
|
|
for item in ["a", "b", "c"]:
|
2018-11-07 20:31:46 +00:00
|
|
|
for i in range(4): # 0 to 3
|
|
|
|
for i in range(4, 8): # 4 to 7
|
|
|
|
for i in range(1, 9, 2): # 1, 3, 5, 7
|
2015-06-01 05:51:04 +00:00
|
|
|
for key, val in dict.items():
|
2018-11-07 20:31:46 +00:00
|
|
|
for index, item in enumerate(list):
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
### [String](https://docs.python.org/2/library/stdtypes.html#string-methods)
|
|
|
|
|
|
|
|
str[0:4]
|
|
|
|
len(str)
|
|
|
|
|
|
|
|
string.replace("-", " ")
|
|
|
|
",".join(list)
|
|
|
|
"hi {0}".format('j')
|
2020-05-15 22:48:21 +00:00
|
|
|
f"hi {name}" # same as "hi {}".format('name')
|
2015-06-01 05:51:04 +00:00
|
|
|
str.find(",")
|
|
|
|
str.index(",") # same, but raises IndexError
|
|
|
|
str.count(",")
|
|
|
|
str.split(",")
|
|
|
|
|
|
|
|
str.lower()
|
|
|
|
str.upper()
|
|
|
|
str.title()
|
|
|
|
|
|
|
|
str.lstrip()
|
|
|
|
str.rstrip()
|
|
|
|
str.strip()
|
|
|
|
|
|
|
|
str.islower()
|
2020-01-11 00:14:13 +00:00
|
|
|
|
|
|
|
/* escape characters */
|
|
|
|
>>> 'doesn\'t' # use \' to escape the single quote...
|
|
|
|
"doesn't"
|
|
|
|
>>> "doesn't" # ...or use double quotes instead
|
|
|
|
"doesn't"
|
|
|
|
>>> '"Yes," they said.'
|
|
|
|
'"Yes," they said.'
|
|
|
|
>>> "\"Yes,\" they said."
|
|
|
|
'"Yes," they said.'
|
|
|
|
>>> '"Isn\'t," they said.'
|
|
|
|
'"Isn\'t," they said.'
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
### Casting
|
|
|
|
|
|
|
|
int(str)
|
|
|
|
float(str)
|
2017-10-23 13:52:12 +00:00
|
|
|
str(int)
|
|
|
|
str(float)
|
2018-11-16 20:07:42 +00:00
|
|
|
'string'.encode()
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
### Comprehensions
|
|
|
|
|
|
|
|
[fn(i) for i in list] # .map
|
2017-10-05 08:16:53 +00:00
|
|
|
map(fn, list) # .map, returns iterator
|
|
|
|
|
|
|
|
filter(fn, list) # .filter, returns iterator
|
2015-06-01 05:51:04 +00:00
|
|
|
[fn(i) for i in list if i > 0] # .filter.map
|
|
|
|
|
|
|
|
### Regex
|
|
|
|
|
2017-10-02 14:01:54 +00:00
|
|
|
import re
|
2015-06-01 05:51:04 +00:00
|
|
|
|
|
|
|
re.match(r'^[aeiou]', str)
|
|
|
|
re.sub(r'^[aeiou]', '?', str)
|
|
|
|
re.sub(r'(xyz)', r'\1', str)
|
|
|
|
|
|
|
|
expr = re.compile(r'^...$')
|
|
|
|
expr.match(...)
|
|
|
|
expr.sub(...)
|
|
|
|
|
2019-12-18 02:41:29 +00:00
|
|
|
## File manipulation
|
2019-11-24 17:19:00 +00:00
|
|
|
|
2019-12-18 02:41:29 +00:00
|
|
|
### Reading
|
|
|
|
|
|
|
|
```py
|
|
|
|
file = open("hello.txt", "r") # open in read mode 'r'
|
|
|
|
file.close()
|
2020-06-13 00:09:01 +00:00
|
|
|
```
|
2019-12-18 02:41:29 +00:00
|
|
|
|
2020-06-13 00:09:01 +00:00
|
|
|
```py
|
2020-05-15 22:29:05 +00:00
|
|
|
print(file.read()) # read the entire file and set the cursor at the end of file
|
|
|
|
print file.readline() # Reading one line
|
2021-09-18 13:45:51 +00:00
|
|
|
file.seek(0, 0) # place the cursor at the beginning of the file
|
2019-12-18 02:41:29 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Writing (overwrite)
|
|
|
|
|
|
|
|
```py
|
|
|
|
file = open("hello.txt", "w") # open in write mode 'w'
|
2020-05-15 22:29:05 +00:00
|
|
|
file.write("Hello World")
|
2019-12-18 02:41:29 +00:00
|
|
|
|
|
|
|
text_lines = ["First line", "Second line", "Last line"]
|
|
|
|
file.writelines(text_lines)
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
```
|
|
|
|
|
|
|
|
### Writing (append)
|
|
|
|
|
|
|
|
```py
|
|
|
|
file = open("Hello.txt", "a") # open in append mode
|
2020-05-15 22:29:05 +00:00
|
|
|
file.write("Hello World again")
|
2019-12-18 02:41:29 +00:00
|
|
|
file.close()
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context manager
|
|
|
|
|
|
|
|
```py
|
|
|
|
with open("welcome.txt", "r") as file:
|
2020-05-15 22:29:05 +00:00
|
|
|
# 'file' refers directly to "welcome.txt"
|
2019-12-18 02:41:29 +00:00
|
|
|
data = file.read()
|
2020-05-15 22:29:05 +00:00
|
|
|
|
|
|
|
# It closes the file automatically at the end of scope, no need for `file.close()`.
|
2019-12-18 02:41:29 +00:00
|
|
|
```
|