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 basic syntax of Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the basic grammars of Python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 single-line comments and multi-line comments:

Function: explain the functions and functions of some parts of the program and improve the readability of the program. Single-line comment: # indicates the beginning of a single-line comment, and code until the end of this line is ignored: use three single quotation marks' comment'or three double quotation marks''comment''

# this is a simple comment print ("Hello world!")'' This is all multi-line comments Python language really simple''# print ("this line of code has been commented, will not be compiled and executed!") "" this is a multi-line comment Python enclosed in double quotation marks is also allowed ""

Running result:

Hello world comments\ nThis is a multiline comment enclosed in double quotation marks\ nPython is also allowed\ n'

2 variables

2.1 python is a weakly typed language:

1. Variables can be assigned directly without declaration: assigning a value to a variable that has never been used is equivalent to declaring the variable

two。 The data type of a variable can be changed dynamically: the same variable can be assigned to a numeric type and a string type

# define a numeric type variable a = 5print (a) # re-assign a string to the a variable a = 'Hello, wangnan'print (a) print (type (a)) # output the type of incoming data

Running result:

5Hello, wangnan

2.2 print function: output function

User_name = 'wang ning'user_age = output multiple variables and string print at the same time ("Reader name:" >

Output result:

Reader name: wang ning Age: print outputs multiple variables and strings at the same time, specifying the delimiter print ("Reader's name:", user_name, "Age:", user_age, sep=' |')

Output result:

Reader name: | wang ning | Age: | setting the end parameter, specifying that print (40,'\ tasking, end= "") print (50,'\ tasking, end= ") print (60,'\ tasking, end=") will not be wrapped after the output.

Output result:

40 50 60f = open (r "F:\ python_learn\ python_code\ poem.txt", "w") # Open the file to write to print ('bright moonlight in front of the bed', file=f) # print function output to a specific file print ('like a cold frost on the ground', file=f) f.close ()

Output result:

The document is written: the moonlight in front of the bed is suspected to be frost on the ground

2.3 naming rules for variables

1. Identifiers can consist of letters, underscores (_), and numbers, but numbers cannot begin

two。 Identifiers cannot be python keywords, but can contain keywords

3. Identifiers cannot contain spaces

Identifiers in 4.Python are case-sensitive.

5. Identifiers that begin with an underscore have a special meaning. Class attributes that cannot be accessed directly by starting with a single underscore are represented by _ foo, which need to be accessed through the interface provided by the class and cannot be imported with from xxx import *.

6. The _ _ foo that begins with a double underscore represents the private member of the class, and the _ _ foo__ that begins and ends with a double underscore represents the identity specific to a particular method in the Python, such as _ _ init__ () represents the constructor of the class.

2.4 keywords and built-in functions for python

1. If you use a keyword as the variable name, the python interpreter will report an error

two。 If you use the built-in function name as the variable name, the python interpreter will not report an error, but the built-in function will be overwritten by this variable and the built-in function cannot be used

# python keyword False None True and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield# Import keyword module import keyword# displays all keywords keyword.kwlist#python built-in function abs () delattr () hash () memoryview () set () all () dict () help () min () setattr () any () dir () hex () next () slicea () Ascii () divmod () id () object () sorted () enumerate () input () oct () staticmethod () bool () eval () int () open () str () breakpoint () exec () isinstance () ord () sum () bytearray () filter () issubclass () pow () super () float () iter () print () tuple () callable () format ( ) len () property () type () chr () frozenset () range () vars () classmethod () getattr () locals () repr () zip () compile () globals () map () reversed () _ import__ () complex () hasattr () max () round () what is the basic grammar of Python? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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