- I choose the latest version of Python interpreter
- Start with the simplest Hello world
>>> print("Hello world!") Hello world! >>>
- Like the simplest calculator
Needless to say, you can understand it at a glance.
>>> 2+6 8 >>> 2/6 0.3333333333333333 >>> 2//6 0 >>> 5*9 45 >>> 5.0/6 0.8333333333333334 >>> 2.0//6 0.0 >>> 6//2 3 >>> 6.0//2 3.0 >>> 6.0/2.4 2.5 >>> 6.0//2.4 # integer division, discarding decimal parts 2.0 >>> 6%3 0 >>> 3%6 # Remainder operation 3 >>>
- variable
>>> x = 9 >>> x*x 81 >>>
- Sentence
>>> print(5*5) 25
- Get user input
>>> input("How old are you: ") How old are you: 250 '250' >>>
- function
>>> 2**3 8 >>> pow(2,3) 8 >>> -10 -10 >>> abs(-10) 10 >>>
- Modular
>>> import math >>> math.floor(54.69) 54 >>>
Here we put in a mathematical module math, which has many functions that can be accessed by using math.flooar().
>>> from math import sqrt >>> sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error >>> sqrt(100) 10.0 >>> >>> import cmath # cmath module supports complex numbers >>> cmath.sqrt(-1) 1j >>>
The above is interactive window programming. Of course, we can create. py files to write programs.
- Create the file 1001_base.py
#!/usr/bin/env python print("Hello world")
(base) [root@8f5e50928415 python_action]# python 1001_base.py Hello world
10 notes
Use single-line comments in Python#
Multi-line comment usage
# Annotated content ''' Annotated content ''' """ Annotated content """
- Character string
Processing strings in Python is as comfortable as dealing with English.
>>> x = 'Ni shi' >>> y = 'zhu ma?' >>> >>> x + y 'Ni shizhu ma?' >>> y = ' zhu ma?' >>> x + y 'Ni shi zhu ma?' >>>
Very convenient stitching.
The most basic is finished.