Python Getting Started Notes

brief introduction

Python is a very simple language.

Python is an interpretive language that uses indentation alignment to organize code execution.

1, 6 standard data types:

Number String List Tuple Sets Dictionary

Where,

Number String Tuple Sets is immutable data

List Dictionart is variable data

2. Basic grammar:

Operator:

Comparison operator<,<=,>,>=,==,!=
Escape Character\n Wrap \Quote \Backslash
Arithmetic Operators+, -, *, / (division of floating point numbers), //(division of integers), ** (power)
Logical operatorsand,or,not 
Assignment Operators=
Identity Operatoris, is not,in ,not in

Python comparison operators can be used concurrently without self-increasing or self-decreasing

Bit and Arithmetic > Comparison > Copy

3. Select Structure

Single branch structure:

if conditional statement: #Conditional statement cannot be assigned but can==

 print("   ")

Double branch structure:

if. conditional statement

else Conditional Statement

Ternary conditional operator:if (conditional expression) else value when the condition is true
Multi-branch structure:

if. conditional statement

elif. Conditional statement

4. Cycle structure

The while loop structure:

a = 1
sum=0
while a<=100:
    sum = sum + a
    a =a+1
print(sum)

The difference between break and continue:

break closes the entire loop
continue ends this cycle

5. zip() Parallel List

Method of parallel list operation

names = ("jiaqi","ziyu","jiayu")
ages = (16,17,18)
jobs = ("programmer","art","player")

for name,age,job in zip(names,ages,jobs):
    print("{0}--{1}--{2}".format(name,age,job))

for i in range(3):
    print("{0}--{1}--{2}".format(names[i],ages[i],jobs[i]))

6. Generator

Generator can only be called once, and the second call is empty

a = (x for x in range(4))
print(a)


<generator object <genexpr> at 0x000001A12C2004A0>
a = (x for x in range(4))
print(tuple(a))
print(tuple(a))

(0, 1, 2, 3)
0

7. String slice operation

(Start offset: End offset: Step)
(Start offset: end offset) Header does not end
Steps are -1 reverse extraction from right to left

8. Form function
 

a = "The name is:{0},Age is:{1}"
b = a.format("Fat and fat",18)
b      #Output b
'The name is: Fat and fat,Age: 18'

9. Fill and align

^ < > Center aligned left aligned right aligned, followed by bandwidth
: Character followed by a fill, can only be a character, if not specified, the default is space

>>>"{:*>8}".format("245")
'*****245'
>>>"I am{0},My favorite number is{1:*^8}".format(Fat, 666)
'I'm fat, and my favorite number is**666***'

10. Sequence

range() can help us easily create a list of integers in the syntax:
range[[start] end [step]]
start optional otherwise defaults to 0
End is required to represent the end number
Step is optional for step

list(range(20,30,4))

[20, 24, 28]

Increase of list elements:

append()

a = [20,40]
a.append(80)

a    [20,40,80]

+

a = [20,40]
a = a +[50]

[20,40,50]

extend()

a = [20,40]
a.extend([50,60])

[20,40,50,60]

insert() anywhere

a = [10,20,30]
a.insert(2,100)

a    
[10,20,100,30]

Multiplication extension

[10,20]*3      

[10,20,10,20,10,20]

List element deletion:

del

A = [100,200,300,400]
del A[2]

A = [100,200,400]

pop() deletes the element at the specified location, or the last element in the list if not specified

a = [10,20,30,40,50]

a.pop()
a = [10,20,30,40]

a.pop(1)
a = [10,30,40]

remove() deletes the first occurrence of an element

a = [10,20,30,40,50]
a.remove[20]

a  = [10,30,40,50]

Access and Counting of List Elements

index()

a = [10,20,30,40,50,20,30,20,30]
a.index(20)
1
a.index(20,3)   The first 20 after the index position
5

count() Gets the number of occurrences of a character

len() Returns the length of the list

a = [10,20,30]

len(a)
3

11. Tuple creation

a= 1,2,3) or a= 1,2,3
There is only one time a=(1,)

12. Dictionaries

1) Definition:

a ={'name':'jiaqi','age':18,'job':'programmer'}
a = dict(name='jiaqi',age=18,job='programmer')
a = dict([("name","jiaqi"),("age",18)])

2) Access to dictionary elements:

a={'name':'jiaqi','age':18,'job':'programmer'}
a['name']
'jiaqi'
get()
a.get('name')     a.get('name','ddd')Not Occasionally'ddd'
a.items()      #List all key-value pairs
dict_items([('name', 'jiaqi'), ('age', 18), ('job', 'programmer')])
a.keys()
dict_items([('name', 'jiaqi'), ('age', 18), ('job', 'programmer')])
len(a)
3
"name"in a
True

3) Addition, modification and deletion of dictionary elements:

update() adds all key values from the new dictionary to the old dictionary object, overwriting them directly if they are repeated

b={'name':'jiaqi','money':1000','sex':'nv'}
a.update(b)
a  {'name':'jiaqi','age':17,'job':'programmer','money':1000,'sex':'nv'}

del() Delete the specified key

pop

b=a.pop('age') b 17 Delete and assign a value to b

clear () Delete all keys

4) Sequence unpacking:

Assigning values to multiple variables

s={'name':'jiaqi','age':18,'job':'programmer'}

name,age,job=s   #Default key operations
name  
'name'

name,age,job=s.items()    #Operations on key values
name
{'name','jiaqi'}

name,age,job=s.values()    #Operating on values
name
'jiaqi'

13. Tabular data use and list storage

r1={"name":"Less One","age":18,"salary":30000,"city":"Beijing"}
r2={"name":"Waiter","age":17,"salary":40000,"city":"Shanghai"}
r3={"name":"Junior Three","age":16,"salary":60000,"city":"Shenzhen"}
tb = [r1,r2,r3]

#Get second-line pay
print(tb[1].get("salary"))

#Pay for everyone in the printed form
for i in range(len(tb)):
    print(tb[i].get("salary"))

#Print all data in the table
for i in range(len(tb)):
    print(tb[i].get("name"),tb[i].get("age"),tb[i].get("salary"),tb[i].get("city"))

14. Sets

add() Add () is not repeatable

a = {3,4,7}
a.add(9)

set() Converts an iterative object, such as a list tuple, into a set, leaving only one if there is duplicate data in the original data

a = {'a','b','c','b'}
b = set(a)
b = {'a','b','c'}

remove() deletes the specified element, clear() empties the entire collection

a = {10,20,30,40,50}
a.remove(20)
a = {10,30,40,50}

Collection related operations

a={1,3,'s'}
b={'he','s','gt'}
a|b #union
{1,3,'s','he','gt'}
A&b #Intersection
{'s'}
a-b #difference set
{1,3}
a.union(b) #union
a.intersection(b) #intersection
a.difference(b) #difference set

15. Other Knowledge Points

strip() removes the specified information at the beginning and end of a string

"   sfhjdf ". strip()
output    "sfhjdf"

lstrip() Removes the information to the left of the string
rstrip() removes the information to the right of the string

a="YUJIAQI love to play program very much"
a.capitalize() produces a new string with the first letter capitalized
a.title() produces a new string The first letter of each word is capitalized
a.upper() All characters are capitalized
a.lower() All characters are lowercase
a.swapcase() All letter case conversions

Is isalnuml() a letter or a number
isalpha() checks whether a string is composed of only letters (or Chinese characters)
isdigital() checks whether a string consists of numbers
isspace() checks for whitespace
Is isupper() a capital letter
Is islower() a lowercase letter

Keywords: Python Back-end

Added by mfacer on Sun, 23 Jan 2022 21:08:17 +0200