Quick guide from C to python
This tutorial only introduces the basic knowledge points required to use Python as a scripting language. Its purpose is not for machine learning or data analysis, but to help you write short, maintainable, and
never-overflowingscripts.Readers with a solid foundation in C can master these concepts in an afternoon.
This tutorial partially references Liao Xuefeng's blog and is based on Python 3.
Main Differences from C
- Variables do not require type declaration when defined, but Python is a strongly typed language.
- No semicolon
;
at the end of statements. - No curly braces
{}
; indentation is used instead (be consistent with spaces or tabs, and ensure uniform length). - Add
:
at the end ofif
andwhile
statements. - Conditional expressions use
and
andor
instead of&&
or||
. - Integer division in Python is
//
, not/
. - Comments use
#
instead of//
. 'abc'
and"abc"
have the same meaning.
I/O
- Read an entire line and store it:
var = input()
- Read space-separated data:
a, b = map(int, input().split())
- Read with a prompt:
var = input('please type what you want')
- Output a line:
print(res)
- Output with spaces:
print('hello', res1, res2)
- Formatted output:
print('my name is %s and I\'m %d year(s) old.' %(name, age))
, orprint(f'my name is {name} and I\'m {age} year(s) old.')
- File I/O:
with open('D:/tmp1/file.txt','a') as f:
f.write('this is a trivial test for file I/O.')
with open('D:/tmp1/file.txt') as f:
content = f.read()
print(content) # However, when the file contains Chinese characters, garbled text appears. "你好" becomes "浣犲ソ". The solution is provided later.
f.close()
Data Type & Conversion
Once a variable is created (assigned), its type is fixed. You can check its type using type(var)
.
- Integer (int):
567
,0xc0ffee
,2**100+1
,998244353 // 114514
, etc. - Float:
3.14
,1.6e-19
,0.1 + 0.2
,math.sqrt(1919810)
, etc. - String (str):
'I\'m \"OK\"!'
,'\\\t\\'
, or:
'''hello,
world!''' # Strings can be written across multiple lines.
-
Bytes: e.g.,
b'A'
,b'1qaz2wsx'
. They cannot be operated with int or str. -
Boolean:
True
andFalse
, equivalent to 1 and 0. They can be used in arithmetic and logical operations but cannot be reassigned. -
Null value:
None
-
Convert string to integer:
int(input())
-
Convert integer to float:
float(19260817)
-
Convert hexadecimal to decimal:
int('0xdeadbeef',16)
orint('0ff1ce',16)
-
Convert decimal to binary:
bin(10)
Encoding
(Finally, I can handle files with Chinese characters!)
- Main differences between encodings (Unicode is only used for memory display, not for data storage):
Character | ASCII | Unicode | UTF-8 |
---|---|---|---|
A | 01000001 | 00000000 01000001 | 01000001 |
中 | × | 01001110 00101101 | 11100100 10111000 10101101 |
- Convert int to char using
chr()
; convert char to int usingord()
. - Convert str to ASCII or UTF-8 using
'xxx'.encode('ascii')
or'xxx'.encode('utf-8')
. - Convert bytes back to str using
decode
:
>>> b'ABC'.decode('ascii')
'ABC'
>>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
'中文'
Returning to the earlier file I/O example, we only need to add encoding = 'utf-8'
when opening the file the second time.
Basic Data Structures
In Python, data structures are treated as objects and come with built-in methods, which is a key difference from procedural C.
list——[]
-
Create a list:
subjects = ['calculus', 'programming', 'politics']
-
Query the last item:
subjects[2]
orsubjects[-1]
-
Reassign the second item:
subjects[1] = 'PE'
orsubjects[-2] = 'PE'
-
Insert at the end:
subjects.append('introduction_to_computer_system')
-
Insert at a specified position (at index 2):
subjects.insert(2, 'databases')
-
Remove from the end:
subjects.pop()
-
Remove the element at index 3:
subjects.pop(3)
-
Sort the elements (elements in the list must be of the same type):
subjects.sort()
tuple——()
- Create a tuple:
subjects = ('calculus', 'programming', 'politics')
Once a tuple is created, it cannot be modified.
dict——{}
(Dict is similar to map
in C++. I originally thought dict was also implemented using an rb-tree, but an error told me that it is implemented using hashing. Therefore, the elements in a dict must be immutable objects such as strings or tuples, and you cannot insert a list.)
-
Create a dict:
score = {'calculus':96, 'programming':92, 'politics':86}
-
Insert an element:
score['PE']=80
-
Delete an element:
score.pop('calculus')
set——set(list)
As the name implies, a set does not contain duplicate elements.
-
Create a set:
academic = set(['programming', 'databases', 'assembly'])
-
Insert an element:
academic.add('networking')
-
Remove an element:
academic.remove('programming')
-
Intersection and union:
>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}
Condition and Loop
- Emphasize once again: use
and
andor
for combining conditions, not&&
or||
!
For other considerations in conditional statements, see the example below:
age = int(input())
if age >= 6:
print('teenager')
elif age >= 18:
print('adult')
else:
print('kid')
- A for loop can iterate over every element in a list, tuple, or string:
subjects = ['calculus', 'programming', 'politics']
for i in subjects:
print(i)
- It can also iterate over a sequence of integers (sum of integers from 0 to 100):
sum = 0
for x in range(101):
sum = sum + x
print(sum)
- break and continue still work (sum of even numbers from 0 to 50):
sum = 0
for x in range(101):
if x > 50:
break
elif x & 1:
continue
sum = sum + x
print(sum)
- A more efficient way to iterate over elements in a list, tuple, or string is slicing. The three parameters in slicing represent the start value, end value, and step size.
>>> L = list(range(100))
>>> L[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> L[10:20]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> L[:10:2]
[0, 2, 4, 6, 8]
>>> L[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>> L[:]
[0, 1, 2, 3, ..., 99]
>>> L[::-1]
[99, 98, 97, 96, ..., 0]
Function
A simple factorial function:
def fact(n):
if n==1:
return 1
return n * fact(n - 1)
- A function can return multiple values—which is actually a tuple.
def rot90(x, y):
tmp = x
x = -y
y = tmp
return x, y
Positional Arguments
They have the same meaning as parameters in the C language.
Default Parameters
For example, the help documentation for the open
function is written as follows:
>>> help(open)
Help on built-in function open in module io:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Open file and return a stream. Raise OSError upon failure.
file is either a text or byte string giving the name (and the path
if the file isn't in the current working directory) of the file to
be opened or an integer file descriptor of the file to be
wrapped. (If a file descriptor is given, it is closed when the
-- More --
- Except for the first item, the file path, the rest are all default parameters. Since I didn't provide a value for the
encoding
parameter, it defaulted toNone
, which caused the read file to display garbled characters.
Note: Default parameters must point to immutable objects! Otherwise, the results obtained after each function run may differ.
Summary
The best way to learn a language is to use it.
Go solve problems now!