Python learning -- first week

Posted by cshinteractive on Wed, 02 Feb 2022 01:48:51 +0100

I A brief introduction to python

1. The bottom layer of Python is different from c. It is an explanatory language. It is not compiled into an executable file, but implemented by interpreting it to the bottom layer of the computer

c codepython code
Compiler compiles to executableExplain to the bottom
Bottom implementationBottom implementation

2. Recognize simple functions

printprintf similar to c

Application:

print('hello,world')
print(10+3)
print('sandwich is',125+125)

Note: print can be used to calculate the number without adding additional variables.                                                                                                

Counting and printing are two contents. Different contents are separated by.

3. Features of Python

1) When the code is written, the line is used as the delimiter, which is omitted; Use of

2) Due to; python has strict requirements for line and indentation. For example, only one space below will cause an error

print('hello,world')
 print(10+3)          #report errors

3) As mentioned above, python adopts line annotation and #, which can be compared with c//

4) Due to the strictness of the line, the code can be divided by / if it is too long, as shown below

words = 'The world we live in is disintegrating into a place of malice and
hatred, where we need hope and find it harder. In this world of fear, hope to
find better, but easier said than done, the more meaningful life of faith will
make life meaningful.'

words = 'The world we live in is disintegrating into a place of malice and
hatred, where'\
'we need hope and find it harder. In this world of fear, hope to find better,
but easier'\
'said than done, the more meaningful life of faith will make life meaningful.'

2, Python variables and data types

  1. Variable:

1) Meaning: used to represent the unknown quantity of a data

2) Specification: a, variable initial is not numeric

b. composition of conformity identifier

c. no conflict with key identification

d, case sensitive

3) Declaration: the type can be declared directly by assignment (that is, the interpreter will automatically judge the type)

4) Declaration method:

m=12
print(m)

a=b=c=10
print(a,b,c)

d,e,f=3,2,1
print(d,e,f)

   2. Data type: same as c, omitted

   3. Variable input and type conversion

1) The input function is used for data input, as follows:

password = input('The password is')
print(type(password),password)

In this way, we can assign the data we want to password

2) Type conversion: since input will only treat the data as str (string), if we want to add and subtract, we have to carry out type conversion

password = int(password)
print(type(password))

3, Operators in Python

For example, / / -, * (the result of the floating-point operation will change to the power of 1, / -, *), / -, * (the result of the floating-point operation will change to the power of 1, / -, *)

2. Walrus operator: personal understanding is used to beautify the expression and make it concise

y=2 + (x = 5-2)
y=2 + (x := 5+2)

Namely: = enables x to be assigned in the middle of the arithmetic, so the second formula is a valid expression (note that the lower version cannot be used)

3. Boolean operator: the same as c.

4. Logical operators:

andLogic and
or

Logical or

notLogical non

Note: 0 and 1 in c represent none and have, while in python, they are false and true

4, Process control in Python

1. Branch statement

1) Single branch: a branch caused by an if

2) Double branch: as follows

score = 100
if score>10
    print('nice')
else
    print('terrible')

Here, two branches are formed, one towards nice and the other towards terrible.

3) Multi branching: i.e. if... elif... elif... else... Form multi case branches with continuous assumptions

2. Circular statement

1) for loop: applicable to traversing elements and specifying the number of loops

Syntax:

for variable in Circulatory body

Interpretation:
    Is to assign the value of the loop body to the variable in turn until the end of the loop
name ='sandwich'
for i in name:
    print(i)

At this time, the string in name , will be assigned to i bit by bit

2) Range: if we carry out multiple cycles, it seems that this is not enough. We can use range to create a sequence of numbers to control the number of cycles

range: 

1.range(x): Indicates from 0 to x-1 An equal difference sequence with a tolerance of 1
2.range(x,y): Indicates from x reach y-1 An equal difference sequence with a tolerance of 1
3.range(x,y,z): Indicates from x reach y-1 One tolerance of is z Arithmetic sequence of
,
Example:
sand=range(10)
for i in sand:
    print(i)         #Print out 0 1 2 3 4 5 6 7 8 9

sand2=range(0,10)
slightly

3) While loop: like while in c, it is applicable to the case where the loop end condition is known

4) Break and continue: the usage is the same as that in c. break is used to exit the loop and continue is used to continue the loop. For a simple example, break here is used as the end condition to jump out of the loop

width = 0.08
x=0
while True:
    if width>=8848.13e3:
        break
    width*=2
    x=x+1
print(x)

5, Data structure in Python -- string

1. Escape character

1) "\" is an escape character in python, which is the same as c language \ n for line feed

2) The "\" escape character can also be used to restore information. For example, print ('My name is \ 'sand') can print single quotation marks here

3) "R" or "R" can make \ invalid. For example, print(r '\ Hello') can print \ hello

2. Splicing and repetition

1) Splicing: use "+" to print hello, world10086 as follows

s1 = 'hello,world'
s2 = str(10086)
s3 = s1 + s2
print(s3)

2) Repeat: use "*", such as s1 =* 5, then s1 =!!!!!

3) Get string length and member judgment

1) Get the string length and use the built-in function len usage: len (x)

Instead: you can customize the function calculation as follows

s1 = 'hello,world'
x=0
for i in s1:
    x+=1
print(x)

2) Member judgment: use in and not in to judge whether a string contains a substring

s1 = 'mum'
s2 = 'mu'
print(s2 in s1)

#Print out True

Topics: Python