Zero basis self-study python review basic theory variable data type list

preface

After graduating from junior college, I am unemployed. I am interested in programming. Python is simple, easy to learn and easy to use. Start self-learning python. You can learn Python through. The success of finding a job and specialized books is shared by the partners of station C.

catalogue

1, Variables

        1. Naming and use

        2. Avoid named output errors

        3. character string

        4. Modify string case

        5. Splice string

        6. Tabs and line breaks

        7. Delete blank

        8. integer

        9. Floating point number

        10. notes

2, List

        1. What is a list

        2. Accessing elements in a list

        3. Indexes

        4. increase

        5. Delete

        6. change

        7.remove()

summary

1, Variables

        1. Naming and use

Note: lower case letter l and upper case letter O, mistaking letters for 1 or 0

Variable naming rules: composed of letters, underscores and numbers. Variable name cannot start with a number 9_ No space is allowed between name and variable name. Name must be underlined_ 4. Name 4 otherwise an error will be raised. Function name, keyword, word reserved for special purpose. Variable names are simple and highly descriptive.

#variable
kjys = "The torrent flew down threethousand feet. It was suspected that the Milky way had fallen nine days."
print(kjys)#The torrent flew down threethousand feet. It was suspected that the Milky way had fallen nine days.
nw = "The sunshine censer produces purple smoke, and the waterfall hangs in front of the river from a distance."
print(nw)#The sunshine censer produces purple smoke, and the waterfall hangs in front of the river from a distance.
nw_4 = "Snow mastiff armor combination"
print(nw_4)#Snow mastiff armor combination
# 4_nw = "snow mastiff armor combination"
# print(4_nw)#SyntaxError: invalid decimal literal
#n 4 = "snow mastiff armor combination"
#print(n 4)#SyntaxError: invalid syntax
nw_4 = "Snow mastiff armor combination"
nw_4 = "Snow mastiff"
print(nw_4)#Snow mastiff

        2. Avoid named output errors

When tapping the code. If you accidentally input a wrong variable name, the output will be wrong (this is called avoiding naming output errors)

#Avoid wrong variable name input
nwlhm = "Don't you see that the water of the Yellow River comes up from heaven and flows to the sea without returning."
# print(nulh)#NameError: name 'nulh' is not defined
print(nwlhm)#Don't you see that the water of the Yellow River comes up from heaven and flows to the sea without returning.

        3. character string

Strings are strings enclosed in quotation marks

#character string
#First output string mode
print("Don't you see the bright mirror of the high hall mourning white hair? The morning is like green silk and the evening is like snow.")#Don't you see the bright mirror of the high hall mourning white hair? The morning is like green silk and the evening is like snow.
#Second output string mode
print('A song with you, please listen to it for me.')#A song with you, please listen to it for me.

        4. Modify string case

Use the title() function to modify the first letter of the string (lower case = > upper case)

#Use title() to change the initial capitalization of a string
name = "baidu"
print(name.title())#Baidu

        5. Splice string

It can store an ancient poem written by Li Bai, a great poet of the Tang Dynasty, for practice. Each dynasty, name and ancient poem name a variable, and then put it into practice string splicing.

#Splice string
chaodai = "Tang-"
libai = "Li Bai"
gushi_0 = "A pot of wine between flowers, drinking alone without a blind date."
gushi_1 = "Raise a glass to invite the moon, and the shadow becomes three."
gushi_3 = "<Drinking alone under the moon"
n_c_g_g =  gushi_3+""+chaodai+" "+libai+" "+gushi_0+" "+gushi_1
print(n_c_g_g)#Drinking alone under the moon there is a pot of wine between Li Bai and the flowers in Tang Dynasty. He drinks alone and has no blind date. Raise a glass to invite the moon, and the shadow becomes three.

        6. Tabs and line breaks

Use \t for string space and \n for newline

#Use tab (\t) or line break (\n) to add
print("The empty mountain is bathed in new rain, and the arrival of night makes people feel that it is early autumn.\n")#The empty mountain is bathed in new rain, and the arrival of night makes people feel that it is early autumn.
print("\t I urge you to make a cup of wine. When you leave Yangguan in the west, there is no old friend.")#I urge you to make a cup of wine. When you leave Yangguan in the west, there is no old friend.
print("\t I am a stranger in a foreign land alone. I miss my relatives every festival.\n")#I am a stranger in a foreign land alone. I miss my relatives every festival.

        7. Delete blank

The blank string makes no difference to us programmers and machines. Use the rstrip() function to delete the two leading blanks, the lrstrip() function to delete the leading blanks, the rstrip() function to delete the trailing blanks, and select the appropriate function according to the site conditions.

#Delete blank use rstrip(),lstrip(),rstrip()
libai = "Nava armored Warrior"
#rstrip() removes whitespace at the end of a string
print(libai.rstrip())#Nava armored Warrior
#lstrip() removes whitespace at the beginning of a string
print(libai.lstrip())#Nava armored Warrior
#strip() removes whitespace at both ends of a string
print(libai.strip())#Nava armored Warrior

        8. Integer

Integer is the addition, subtraction, multiplication and division in reality and python.

#integer
print(3+4)#7
print(9/1)#9.0
print(10-4)#6
print(9*9)#81
print(9//3)#3

        9. Floating point number

Floating point numbers are python numbers with decimal points

#Floating point number
print(3.2+4.4)#7.6000000000000005
print(4.4-2.2)#2.2
print(4.1*2.1)#8.61
print(4.2/2.1)#2.0
print(4.9//2.9)#1.0

        10. notes

Notes allow you to add program descriptions when writing programs, so that the next person who takes over the same project can know and understand the programs you write.

#Note use
#First type of annotation well number annotation
#Output function print()
print("Hello wolrd")
#Second annotation double quotation mark annotation
"""
The Tianmen gate interrupts the opening of the Chu River, and the clear water flows back to this point in the East.
The green mountains on both sides of the Strait are opposite each other, and the solitary sails come from the sun.
"""
#Third annotation single quotation mark annotation
'''
The green mountains stretch across the north, and the white water flows around the east city.
As soon as I leave here, I will leave here alone.
'''
#example
libai = """
    <To Wang Lun
       Tang-Li Bai
 Li Bai is about to go by boat,
Suddenly I heard the sound of stepping on the bank.
The peach blossom pond is a thousand feet deep,
Not as good as Wang Lun.
"""
print(libai)

2, List

        1. What is a list

The list is composed of sequential elements such as numbers, letters and strings. There are no independent elements between the elements, but a sequential set.

#List use square brackets []
kjys = ['Li Bai','Glory of Kings','CS:GO','CF','Game for Peace']
print(kjys)#['Li Bai', 'glory of the king', 'cs:go','cf',' peace elite ']

        2. Accessing elements in a list

A list is an ordered collection. To access an element in the list, you can tell python the index of the element.

#Access element
na_wa = ['python','c','java','php','go']
print(na_wa[0])#python
print(na_wa[1].title())#C
print(na_wa[2].upper())#JAVA
print(na_wa[3].strip())#php

        3. Indexes

The index starts from 0 instead of 1. Negative numbers can be indexed. If the index exceeds the range, an error will be reported.

#Index 0 does not start with 1
xingtian = ['pycharm','NongFu Spring','a brand of instant noodles','Wahaha','Ice dew','Erie','Baisui mountain','Evergrande ice spring']
print(xingtian[3])#Wahaha
print(xingtian[-4])#Ice dew
libai = 'Li Bai drank it'+xingtian[1].rstrip()+'?'
print(libai)#Has Li Bai ever drunk Nongfu mountain spring?

        4. increase

Increase the number of elements in the list using the append() function and inser() function. The append() function adds elements at the end without affecting other elements in the list. The insert() function adds elements at any position in the list.

#Add element append()
k = ['python','c','php']
print(k)#['python', 'c', 'php']
k.append('Li Bai')
k.append('Dufu')
print(k)#['python','c','php',' Libai ',' Dufu ']
#Insert element insert()
python = ['ballad','Rap','Rock','Archaic style','popular','classical','DJ']
print(python)#['ballads',' rap ',' rock ',' Antique ',' popular ',' classical ',' DJ ']
python.insert(0,'go')
print(python)#['go',' Folk ',' rap ',' rock ',' Antique ',' popular ',' classical ',' DJ ']

        5. Delete

Delete list elements using the del statement. Use the pop() function to remove the element from the end of the list and which element can be used next.

#Delete element pop()
nw_k = ['mobile phone','computer','The server','Switch','base station','Router','film']
print(nw_k)#['mobile phone', 'computer', 'server', 'switch', 'base station', 'router', 'movie']
nw_4= nw_k.pop()
print(nw_4)#film
print(nw_k)#['mobile phone', 'computer', 'server', 'switch', 'base station', 'router']
nw_4 = nw_k.pop(4)
print(nw_4)#base station
print(nw_k)#['mobile phone', 'computer', 'server', 'switch', 'router']
#del deleting an element will cause an error if the list is completely deleted
k = ['python','c','php']
# del k
# print(k)#NameError: name 'k' is not defined
print(k)#['python', 'c', 'php']
del k[0]
print(k)#['c', 'php']

        6. change

To change an element in the list, you can specify the name in the list and the index of the element to be changed. You can complete this by specifying the value of the new element.

#Modify element
#Create list
nf_sq = ['Coca Cola','Pepsi Cola','Sprite','Sugar free Coca Cola','Diet Pepsi','Fanta','Green Ice Tea ']
#Output value before modification
print(nf_sq)#['Coca Cola', 'Pepsi', 'sprite', 'sugar free Coca Cola', 'sugar free Pepsi', 'Fanta', 'Master Kang iced black tea']
#Modify values in the list
nf_sq[2] = 'Ice Tea '
#Output modified value
print(nf_sq)#['Coca Cola', 'Pepsi Cola', 'unified iced black tea', 'sugar free Coca Cola', 'sugar free Pepsi Cola', 'Fanta', 'Master Kang iced black tea']

        7.remove()

Do not know where the element to be deleted is in the list. If you know the value of the element, you can use the remove() function to delete it.

#Remove element by value remove()
tangdai = ['p','y','t','h','o','n']
tangdai.remove('p')
print(tangdai)#['y', 't', 'h', 'o', 'n']

summary

Only after reviewing python can I know that there are still some people who know that it is not a thing.

Learning materials:

Python Programming: from door to practice by [US] Eric Mathers translator yuanguozhong by people's Posts and Telecommunications Press

Python from introduction to mastery by tomorrow Technology

       

Tags: Python

Posted by james_4k2 on Wed, 01 Jun 2022 13:26:07 +0530