- 0. Prerequisites
- 1. Common Data Types
- NOTICE
- 1.1 Numerical Operations
- NOTICE
- 1.2 Strings
- 1.3 Indexing and Slicing
- 1.4 Lists
- 1.5 Tuples
- 1.6 Dictionaries
- 1.7 Sets
- 2. Control Flow
- 2.1 if
- 2.2 while
- 2.3 for with range/zip/enumerate
- 3. Functions
- 4. Input and Output (Standard Input and Output)
- 5. File Operations
- 6. Packages
- 7. Classes
- 8. Exception Handling
- Understanding that variables in Python are all pointers/references
0. Prerequisites
- Statements in Python can be executed directly without needing to be written inside a main function like in C/C++.
- Use
<span>#</span>
to indicate that everything from here to the end of the line is a comment, similar to the use of<span>//</span>
in C/C++. - All data in Python is stored in objects, even numbers like 123 are also objects.
- Variable assignment does not require prior declaration or type specification, for example,
<span>a = 123</span>
. - From a C/C++ perspective, variables are all of type
<span>object*</span>
, so they can point to any object. - Arrays are of type
<span>object*[]</span>
, so they can store any objects mixed together. - Use
<span>type(x)</span>
to get the type of an object,<span>dir(x)</span>
to get the methods of an object, and<span>id(x)</span>
to get the ID of an object, which is actually the address of the object, similar to<span>(size_t)&x</span>
.
1. Common Data Types
Type | Name | Example |
---|---|---|
int | Integer | -100 |
complex | Complex Number | 1 + 2j |
bool | Boolean | Only True and False |
float | Floating Point | 3.1416 |
str | String | ‘hello’, “abc” |
list | List | [1, 0.3, ‘hello’] |
tuple | Tuple | (‘hello’, 10) |
set | Set | {1, 2, 3} |
dict | Dictionary | {‘dogs’: 5, ‘pigs’: 3} |
NoneType | Only None |
NOTICE
- int has no size limit and can perform operations on arbitrarily large integers.
- float is a 64-bit floating point, equivalent to the double type in C/C++.
- bool type only has True and False with the first letter capitalized.
- NoneType is a special type that has only one value/object instance, None, which is typically used to represent a null value, similar to nullptr in C++.
1.1 Numerical Operations
<span>//</span>
is floor division (rounding down, even for negative numbers).<span>round(-3.9)</span>
rounds to the nearest integer (rounding half away from zero).<span>=</span>
is based on value comparison, while<span>is</span>
is based on id (address) comparison.- To check for inequality, generally write
<span>a is not b</span>
, not<span>not a is b</span>
. - Comparison operations can be chained, and any number of comparisons can be chained:
<span>1<2<3</span>
is equivalent to<span>1<2 and 2<3</span>
.
NOTICE
To check for None, use<span>is</span>
and<span>is not</span>
, not<span>==</span>
and<span>!=</span>
, because None is a unique object and equality should be checked based on id, not value.
1.2 Strings
- Strings can be enclosed in single or double quotes.
- Single quotes within single quotes (or double quotes within double quotes) need to be escaped with
<span>\</span>
, but single quotes within double quotes (or vice versa) do not require escaping. - Triple single/double quotes can span multiple lines.
- String operations: (strings are immutable, any operation returns a new string object)
<span>" ".join(["1", "2", "3"])</span>
, results in<span>'1 2 3'</span>
, which joins the three elements of the string sequence with a space.- Concatenation:
<span>'ab' + 'cd' -> 'abcd'</span>
- Repetition:
<span>'ab' * 3 -> 'ababab'</span>
- Splitting:
<span>a.split(b)</span>
, splits a using string b as the delimiter - Joining:
<span>a.join(b)</span>
, joins the elements of string sequence b using string a: - Replacing:
<span>a.replace(b,c)</span>
replaces all occurrences of b in string a with c, for example,<span>"1,2,3".replace(",","@")</span>
, results in<span>"1@2@3"</span>
. - Case conversion:
<span>upper()</span>
and<span>lower()</span>
. - String conversion:
<span>str(a)</span>
: converts a to a string<span>hex(a)</span>
,<span>oct(a)</span>
,<span>bin(a)</span>
: converts integer a to hexadecimal, octal, and binary string respectively<span>int(str,b)</span>
: converts string str to an integer of base b<span>float(str)</span>
: converts string str to a float- Raw strings: prefix with r to indicate that
<span>\</span>
should not be treated as an escape character, which is very convenient for handling Windows paths and regular expressions. - Template strings: prefix with f, which is very convenient when needing to use variable values, just enclose the variable in
<span>{}</span>
, and inside<span>{}</span>
you can have not only variable names but also other expressions. - Example 1:
a = 1
b = 2
f"a's value is {a}, b's value is {b}"
# Result is 'a's value is 1, b's value is 2'
- Example 2:
a = 1.2345678
f"a ≈ {a:.3f}" # Keep 3 decimal places (PS: the left string is just a normal approximately equal sign ≈)
# Result is 'a=1.235'
- Example 3: If you want to use a plain `{` or `}` character, you need to write the escape identifier twice, similar to `%%` in printf
a = "b"
f" {{ a }} vs {a}"
# Result is '{a} vs b'
1.3 Indexing and Slicing
Indexing
For an ordered sequence, you can access the corresponding value by indexing. Strings are an example of an ordered sequence, and Python uses<span>[]</span>
for indexing ordered sequences. Indexing starts at 0, where index 0 corresponds to the first element of the sequence, and an error will occur if the index exceeds the maximum value. Python also has negative index values, which count from the end.
Slicing
You can extract the desired subsequence from a sequence using the syntax:<span>var[lower:upper:step]</span>
, where the left is inclusive and the right is exclusive; step indicates the stride of the values. These three parameters can be partially omitted, for example:
<span>a[:]</span>
: from start to end, creates a copy<span>a[1:]</span>
: from 1 to the end<span>a[:3]</span>
,<span>a[:-2]</span>
: from 0 to 3, from 0 to the second last<span>a[::-1]</span>
: reverse order
1.4 Lists
In Python, a list is an ordered sequence. Lists are generated using a pair of<span>[]</span>
with elements separated by<span>,</span>
, and the elements do not need to be of the same type, and the length of the list is also not fixed. You can also create an empty list using<span>list()</span>
or<span>[]</span>
. The operations on lists are similar to those on strings, as follows:
<span>len()</span>
: outputs the length of the list<span>+</span>
: concatenation<span>*</span>
: repetition- Array indexing and slicing can be used for both reading and assigning values, and using slicing for assignment automatically expands the length of the list.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a[5:7] = ["a", "b", "c", "d"] # Replace two elements with four elements
# Result is [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 8, 9]
- You can also use
<span>del</span>
to delete
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[0:3]
print(a)
# Result is [4, 5, 6, 7, 8, 9]
- List operations
<span>l.append(a)</span>
adds a to the end of the list, treating any type of data as a whole<span>l.extend(a)</span>
if a is a sequence, it is equivalent to appending each element of the sequence<span>l.insert(idx,a)</span>
inserts a at index idx of the list<span>l.remove(a)</span>
removes the first occurrence of a, raises an error if it does not exist- Testing membership:
<span>in</span>
- Counting and indexing:
<span>l.count(a)</span>
returns the number of occurrences of a in list l;<span>l.index(a)</span>
returns the index of the first occurrence of a in list l, raises an error if it does not exist (an AttributeError) - Adding, inserting, and removing:
- Sorting and reversing:
<span>a.sort()</span>
sorts a in place, changing the values in a;<span>sorted(a)</span>
returns a new sorted list without changing the original values;<span>a.reverse()</span>
and<span>reversed(a)</span>
indicate reversal, the difference is similar to the above.
1.5 Tuples
Similar to lists, tuples are also an ordered sequence, can be indexed and sliced, but they are immutable.
- Empty tuple:
<span>()</span>
or<span>tuple()</span>
- Note: A single-element tuple should be written as
<span>(1,)</span>
to avoid ambiguity with a regular parentheses expression. - Generally, tuples can be used to return multiple values from functions, and can also be used for multiple variable assignments. The counting function of tuples
<span>count()</span>
and finding the first occurrence of an element<span>index()</span>
.
t = (1, 3, 4, 6, 7, 56, 83, 3)
print(t.count(3), t.index(56))
# Result is 2 5
w, x, y, z = 1, 2, 3, 4 # 1,2,3,4 is a tuple, parentheses can be omitted here
x = 1, 2, 3, 4
print(x, type(x))
print(type((1, 2, 3, 4)))
# Result is:
# (1, 2, 3, 4) <class 'tuple'>
# <class 'tuple'>
1.6 Dictionaries
A dictionary is a data structure composed of key-value pairs, and values can be accessed through keys. Dictionaries are unordered, so they do not have indexing or slicing.
Basic Operations
- Creating a dictionary: can be created using
<span>{}</span>
or<span>dict()</span>
to create an empty dictionary, and can also be initialized using the structure<span>key:value</span>
. The<span>dict()</span>
function requires a list of tuples as parameters, for example,<span>a = dict([ ("1","a1") , ("2","b2") ])</span>
constructs a dictionary with key-value pairs<span>("1"->"a1", "2"->"b2")</span>
(i.e., the string “1” (key) corresponds to the string “a1” (value), and the other is similar). - Note:
- The keys of a dictionary must be of an immutable type, such as integers, strings, tuples, etc., while values can be any Python object.
- Generally, floating-point numbers are not used as keys due to precision issues.
a = {"blue": 1, "red": 2}
b = {"white": 3, "black": 4}
c = {"color1": a, "color2": b}
print(c) # Nested dictionaries
a = {1.1: "a", 2.2: "b", 3.3: "c"}
print(a[3.3])
print(a[2.2 + 1.1])
# The first one outputs c but the second one raises an error because 2.2+1.1 is not exactly 3.3 (it is approximately 3.3 but not exactly equal)
Dictionary Methods
- Accessing values:
<span>d.get(key)</span>
, compared to using<span>d[key]</span>
, it will not raise an error if the key does not exist in the dictionary, but will return<span>None</span>
. - Deleting elements:
<span>d.pop(key)</span>
, deletes and returns the key-value pair for key, returns<span>None</span>
if it does not exist. You can also use<span>del d[key]</span>
to delete. - Updating a dictionary:
<span>d.update(d')</span>
, updates elements from dictionary d’ into d. - Querying a dictionary:
<span>a in d</span>
, checks if key a is in dictionary d. Note: “not in” is generally written as<span>a not in d</span>
rather than<span>not a in d</span>
<span>keys</span>
method:<span>d.keys()</span>
, returns a sequence of all keys.<span>values</span>
method:<span>d.values()</span>
, returns a sequence of all values.<span>items</span>
method:<span>d.items()</span>
, returns a sequence of all key-value pairs as tuples.
1.7 Sets
A set<span>set</span>
is an unordered sequence, so if there are two identical elements, only one will be retained; and to ensure that there are no duplicate elements, the elements added to a set must be deterministic objects. Because it is unordered, there are also no indexing or slicing. Sets can be created using<span>{}</span>
or <span>set()</span>
, but to create an empty set, you can only use<span>set()</span>
, because<span>{}</span>
represents an empty dictionary.
Set Operations
- Union:
<span>a.union(b)</span>
or<span>a | b</span>
- Intersection:
<span>a.intersection(b)</span>
or<span>a & b</span>
- Difference:
<span>a.difference(b)</span>
or<span>a - b</span>
; elements in a that are not in b - Symmetric Difference:
<span>a.symmetric_difference(b)</span>
or<span>a ^ b</span>
; elements in a or b, but not in both a and b - Subset relationship:
- Check if a is a superset of b:
<span>a.issuperset(b)</span>
or<span>a <= b</span>
- Check if a is a proper subset of b:
<span>a < b</span>
Set Methods
- Add a single element:
<span>s.add(a)</span>
, adds a to set s - Add multiple elements:
<span>s.update(seq)</span>
, adds all elements from seq to set s - Remove a single element:
<span>s.remove(b)</span>
, removes b from set s, raises an error if it does not exist
2. Control Flow
2.1 if
- The basic structure is
<span>if <condition>:</span>
- Unlike C language, it does not require
<span>{}</span>
, but uses indentation to distinguish levels. <span>elif</span>
means<span>else if</span>
.<span>False</span>
,<span>None</span>
,<span>0</span>
, empty strings, empty lists, empty dictionaries, and empty sets are all considered<span>False</span>
.- Since indentation is mandatory, and the content after indentation cannot be empty, we usually write a
<span>pass</span>
, which does nothing. When writing code, if you want to temporarily skip a branch, you can use<span>pass</span>
as a placeholder.
if True:
pass
2.2 while
The basic format is<span>while <condition>:</span>
, you can use<span>break</span>
to exit, and<span>continue</span>
to proceed to the next iteration.
2.3 for with range/zip/enumerate
- The basic format is
<span>for <variable> in <iterable></span>
, where iterable can be lists, strings,<span>range</span>
, opened files, etc. <span>range</span>
:<span>range(3) -> 0, 1, 2</span>
<span>range(1, 5) -> 1, 2, 3, 4</span>
<span>range(1, 10, 2) -> 1, 3, 5, 7, 9</span>
, starts from 1 to 10, with a step of 2<span>range(n)</span>
automatically generates an arithmetic sequence from 0 to n – 1 (excluding n) with a step of 1<span>range</span>
is used to create a series of arithmetic sequences<span>range(begin,end,step)</span>
, where some parameters can be omitted.- You can also use
<span>break</span>
and<span>continue</span>
. - A programming convention in Python is to use
<span>_</span>
to indicate unused values, for example, if we want to loop 3 times but do not need the loop variable, we can write it like this. Optionally, you can add<span>else</span>
, which will execute when the loop exits normally rather than via<span>break</span>
.
a = [1, 2, 3]
for i in a:
if i == 1:
print("1 is in a")
break
else:
print("1 is not in a")
for _ in range(3):
print(233)
<span>zip</span>
:<span>zip</span>
is used to iterate multiple iterables simultaneously (like lists, tuples), the number of iterations is based on the shortest one.- Assuming we have two lists
<span>names = ['Alice', 'Bob', 'Candy']</span>
and<span>num = [1,2,3]</span>
, using<span>zip(names,num)</span>
will yield an iterator consisting of three tuples, where the first position in the tuple corresponds to the element in the<span>names</span>
list, and the second element corresponds to the element in the<span>num</span>
list.
names = ['Alice', 'Bob', 'Charlie']
scores = [80, 90, 75]
for name, score in zip(names, scores):
print(name, score)
<span>enumerate</span>
:- Used to iterate both index and value simultaneously.
<span>enumerate()</span>
returns an iterator containing each character and its corresponding index.
a = "abcdefg"
for i, j in enumerate(a):
print(i, j)
- List comprehensions:
- More concise and understandable than for loops, and more efficient.
a = []
for i in range(10):
a.append(i * i)
# Equivalent to
a = [i * i for i in range(10)] # Quickly generate the required list
- You can also add conditional statements in list comprehensions.
a = []
for i in range(10):
if i % 2 == 0:
a.append(i * i)
# Equivalent to
a = [i * i for i in range(10) if i
This document mainly explains the basic knowledge of Python, covering functions, input and output, file operations, packages, classes, exception handling, and variable references among other topics. Below is the content converted to Markdown format:
3. Functions
- Format:
<span>def function_name(parameter_list):</span>
- Parameter List: directly write variable names, no need to specify types.
- Default Parameters: use
<span>=</span>
to give default values to parameters, similar to default parameters in C++. - Return Value: use
<span>return</span>
to return, the return value can be of any type. - Three Parameter Passing Modes
- Positional Arguments: pass parameter values in the order of their position in the function definition.
- Keyword Arguments: pass parameter values by their names, not relying on their position.
- Passing Parameters by Position or Keyword: when calling the function, you can pass parameter values either by their position in the function definition or by using keywords.
- Assuming a
<span>complex</span>
function is defined with parameters<span>real</span>
and<span>imag</span>
, the call<span>complex(real,imag)</span>
and<span>complex(3,4)</span>
have the same effect. - In custom functions, you can use
<span>/</span>
and<span>*</span>
to declare the parameter passing method. Parameters before the forward slash<span>/</span>
are positional; parameters between the forward slash<span>/</span>
and the asterisk<span>*</span>
can be passed either by position or keyword; parameters after the asterisk<span>*</span>
must be passed by keyword. For example,<span>def fcn_name(a, b, /, c, d, *, e, f)</span>
, where<span>a</span>
and<span>b</span>
are positional parameters,<span>c</span>
and<span>d</span>
can be passed either by position or keyword, and<span>e</span>
and<span>f</span>
are keyword parameters. - Setting Default Parameter Values: you can set default values for parameters when defining the function, and omit parameters with default values when calling.
def quad(x, a=1, b=0, c=0):
return a * x**2 + b * x + c
print(quad(2.0))
print(quad(2.0, c=3))
# Result is 4.0 7.0
- Receiving Variable-Length Parameters
<span>*args</span>
indicates that excess positional parameters are passed as a tuple to<span>args</span>
.
def add(x, *args):
total = x
for arg in args:
total += arg
return total
print(add(1, 2, 3, 4))
print(add(1, 2))
print(add(1))
# Result is 10 3 1
- `**kwargs` indicates that excess keyword parameters are passed as a dictionary to `kwargs` .
def add(x, **kwargs):
total = x
for arg, value in kwargs.items():
print("adding ", arg)
total += value
return total
print(add(10, y=11, z=12, w=13))
# Result is:
# adding y
# adding z
# adding w
# 46
- Returning Multiple Values: a function can return multiple values, which will automatically become a tuple. Using tuple and array assignment, the values in the tuple can be assigned to corresponding variables, but the number of variables on both sides must match, otherwise an error will occur.
def f():
return 1, 2, 3
a, b, c = f()
print(a, b, c)
# Result is 1 2 3
a, b = 1, 2
a, b = (1, 2)
a, b = [1, 2]
a,b,c = 1,2
# Will raise an error
a = 1,2,3
x,y,z = a
4. Input and Output (Standard Input and Output)
Use<span>input</span>
function to get input.
a = input("Please enter:")
<span>print</span>
formatted output can use the previously mentioned template strings, and formatted input can consider using regular expressions.
5. File Operations
- Writing Text Files: the first parameter is the file path, the second parameter is the open mode (
<span>'w'</span>
mode overwrites,<span>'a'</span>
mode appends), and the<span>encoding</span>
parameter specifies the file encoding (commonly<span>'utf8'</span>
, on Chinese Windows systems sometimes use<span>'gb2312'</span>
).
with open("123.txt", "w", encoding="utf8") as f:
f.write("This is a line\n")
f.write("This is another line\n")
with open("123.txt", "a", encoding="utf8") as f:
f.write("Adding another line\n")
- Reading Text Files: use
<span>'r'</span>
mode, and use<span>encoding</span>
to specify the file encoding.
with open("123.txt", "r", encoding="utf8") as f:
for i in f:
print(i)
with open("123.txt", "r", encoding="utf8") as f:
a = f.read()
print(a)
<span>f.read()</span>
returns the entire file as a single string; <span>f.readlines()</span>
returns a list of strings for each line.
6. Packages
- Importing Entire Packages:
import math
math.sin(math.pi / 4)
You can rename the imported package:
import math as mt
mt.sin(mt.pi/4)
- Importing Specific Functions:
from math import sin
sin(1)
- Importing Multiple Functions:
from math import sin
from math import cos
Or
from math import sin, cos
cos(1)
- Renaming Imported Functions:
from math import sin as math_sin
math_sin(1)
- Importing Everything from a Package (Not Recommended):
from math import *
7. Classes
- Basic Format:
class A:
def __init__(self):
self.a = 1
def f(self, x):
self.b = 1
return self.a + x
- Functions or variables that start and end with
<span>__</span>
are special functions or variables, and the<span>__init__</span>
function is executed when an object is created, similar to a constructor. - Functions that do not start with
<span>_</span>
or<span>__</span>
are generally considered<span>public</span>
; functions starting with one underscore are<span>protected</span>
, and those starting with two underscores are<span>private</span>
(to be confirmed). - Non-static class member functions receive the class object as the first parameter, usually named
<span>self</span>
. - Inheritance: In any function, you can add new attributes to an object using
<span>self.xxx = xxx</span>
.
class A:
def __init__(self, x):
self.x = x
def print_x(self):
print(self.x)
class B(A):
def __init__(self, y):
super().__init__(233)
self.y = y
def print_y(self):
print(self.y)
b = B(3)
b.print_y()
<span>super()</span>
returns the inherited parent class, and <span>super().__init__()</span>
calls the<span>__init__</span>
function of the parent class.<span>self</span>
refers to the object constructed by the class, which is automatically passed in and does not need to be written out when called.<span>type(x)</span>
can get the type of an object, and <span>isinstance(obj,cls)</span>
can check if an object inherits from a certain class.
8. Exception Handling
try:
pass
except...:
pass
else:
pass
finally:
pass
<span>try</span>
is followed by code that may throw an error.<span>except</span>
is followed by the type of error to catch, if not specified it is equivalent to<span>except Exception</span>
, which can catch most error types, and the code to execute when an error occurs is written inside.<span>else</span>
is the code that executes when there is no error (optional).<span>finally</span>
is the code that will execute regardless of whether an error occurs (optional).- Use
<span>raise</span>
to throw a specified type of error, and you can also specify error feedback information.
raise ValueError ("Error message")
- Use
<span>assert</span>
to throw an<span>AssertionError</span>
type error when the assertion is false.
assert 1 + 1 == 3, "Does one plus one not equal three?"
Understanding that Variables in Python are All Pointers/References
a = [1, 2, 3]
b = a
def f(b):
b[1] = 100
f(b)
print(b,a)
b[0] = 4
print(a, b)
b = a[:]
Or
b = list(a)
a = [1, 2, 3]
b = a[:]
b[0] = 4
print(a, b)
If the list contains lists, the above is a “shallow” copy and will not copy the inner lists. To deeply copy any object, use<span>deepcopy</span>
.
from copy import deepcopy
b = deepcopy(a)