Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Python syntax?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use Python grammar. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Preface

The syntax of Python is different from that of other programming languages. Before writing Paython programs, you need to know some syntax before you can write standard Python programs.

Input and output print () # print display input () # input content variable name = None # placeholder (you can write None if you have to declare a variable but are not sure what data to write) data type conversion

The use of if elif else

Pass# occupies space

Written as follows:

If condition 1 else:# # satisfies condition 1 execution... elif condition 2 else:# # does not meet condition 1 condition 2 execution...

Conditional expression: (variable = "data" if judgment condition holds execution left otherwise right else "data")

Sex01= input ("Please enter gender:") sex02 = 1 if sex01== "male" else 0

Truth expression:

Sex = input ("Please enter gender:") No value is performed only if if sex:# has a value: 0 "" None [] {} False print (sex) while cycle

Write method 1

While True: loop body... if exit condition: break# ends the loop

Writing method 2

While True: loop body... if exit condition: break# ends loop else:print (") # if you want the program to be executed when the loop condition is not met, write an else code block. # if the condition is met, the else statement is not executed when exiting from break.

If the condition is met, the else statement is not executed when exiting from break.

Summary: else can determine the location of while loop exit (loop condition / loop body)

Write 3 cycle count

Count = 0while count ("I", "yes", "small", "Ming")

Variable = element, element

Get element

Variable = tuple [index]

Variable = tuple [slice] # what is assigned to the variable is the new list created by the slice

Ergodic tuple

Positive:

For variable name in list name:

The variable name is the element

Reverse:

The for index name in range (len (list name)-1):

The tuple name [index name] is the element

Dictionary dict

Create a dictionary

Variable = {key 1: value 1, key 2: value 2, key 3: value 3}

Variable = dict (iterable object)

Add and modify

Dictionary [key] = value

Add if the key does not exist

Modify if the key exists

Get element

Variable = dictionary [key] # an error will be reported if the key does not exist

Delete element

Del dictionary [key]

Ergodic

Traversal get key

For key in dictionary .keys ():

Print (key) # get key

Ergodic value

For value in dictionary .values ():

Print (value) # get the value

Traversing to get keys and values

For key,value in dictionary .items ():

Print (key,value) # get keys and values

Dictionary derivation

Variable = {key: value for item in iterable object}

Set set

Create a collection

Variable = set ()

Variable = set (iterable object)

Variable = {1pm 2pm 3}

Add element

Collection .add (element)

Delete element

Collection .discard (element)

Ergodic

For item in collection: print (item)

Intersection union complement

Intersection &: return to the common elements that you and I both have

S1 = {1pr 2je 3} s2 = {2je 3je 4} print (S1 & S2) # {2jue 3}

Union |: return you and I will not repeat the element. If it is repeated, only one will be returned.

S1 = {1pr 2pm 3} s2 = {2je 3je 4} print (S1 | S2) # {1pm 2pm 3je 4}

Supplementary collection ^: return the different elements of you and me

S1 = {1pr 2je 3} s2 = {2je 3je 4} print (S1 ^ S2) # {1Jet 4}

Complement -: returns the elements left after subtraction

S1 = {1 print (S2-S1) # {4}

Superset S2 # True

Set derivation

{expression for variable in iterable object}

{expression for variable in iterable object if condition}

The above is what is used in Python grammar. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report