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 are the 19 major grammars learned by Python?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the grammar of the 19th National Congress of Python, which is not very clear to many beginners? in order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Python has been a big hit in the past two years, and many novices choose to start programming from Python. It's true that Python is easy to learn, elegant in code, and easier to get started with than other languages, but it's also broad and profound. If you also want to enter the programming world from Python, you should read this summary of 19 grammars carefully!

The 19th National Congress of Grammar learned by Python

Characteristics of Python

Interpretive language that runs without compilation

Provides an interactive command line

Object-based programming idea

Cross-platform and good compatibility, can run on Windows, Mac, Linux

Simple, easy to use and powerful

01 Chinese coding

Many students will encounter the problem of garbled code when opening the data, which is due to the coding of the character set. The default coding set for Linux and Mac is UTF8, while Windows is ASCII. If the character set encoded by the data is different from the character set you use when you use Python for processing, there will be garbled problems.

In addition, my personal habit is to add the following to the header of the Python code, where the second line declares the use of the UTF8 character set.

#! / usr/bin/env python# coding:utf8

02 variable

Variables in Python can be thought of as containers that hold the values we need to use.

Python has the same requirements for variable names as in other languages: it can include English, numbers, and underscores, but cannot start with a number and is case-sensitive. Of course, I recommend that variable names are good in pure English, and choose some meaningful names to make it easier for you to understand the role of each variable.

Python is a weakly typed language, and there is no need to declare the type of a variable when using it. Variables in Python include the following categories: numeric values, strings, lists, tuples, dictionaries.

03 value

Values include integers and floating-point numbers, which correspond to integers and floating-point numbers, respectively, and the latter is more accurate.

# Integer a = floating point b = 2.1print a, b

04 string

Strings, that is, the text we often come into contact with, can be of any length, enclosed in single or double quotation marks. It should be noted that Chinese and Chinese symbols can only appear within the string. If the comma of the Chinese input method is used in the third line below, Python will report an error.

C = Hello d = Hello print c, d

Use + to concatenate two strings.

Print c + d

Use len () to get the length of the string.

Print len (Hello World)

Use slices to access a character or fragment in a string.

# position subscript starts at 0 c = Hello World # the print result is H, and the subscript 0 indicates the first character print c [0]

# the print result is d, and the subscript is negative to indicate the number from back to front # so-1 means the penultimate character print c [- 1]

# use: returns a fragment with the start subscript and the ending subscript before and after the colon # including the starting subscript, but excluding the ending subscript

# so c [1:5] indicates that it returns a fragment of subscript from 1 to 4, that is, the second to fifth characters print c [1:5]

# negative numbers can also be used for subscripts before and after colons

# or not provided, indicating print c [1:-1], c [: 5], c [3:] from the leftmost end to the rightmost end

05 list

A list is like a team with multiple variables stored in turn. A list is similar to a string, but each element in a string is a character, and each element in a list can be any type of variable.

# use [] to define an empty list, use append () to add an element to the end of the list # if you want to add to the beginning of the list, use prepend () a = [] a.append (1) a.append (2.1) a.append (Hello) print a

Use len () to get the length of the list.

Print len (a)

Operations such as subscript access and assignment of list elements are similar to strings.

Print a [1], a [- 1] a [1] = 100print a

Use del to delete an element from the list.

Del a [0] print a

06 tuple

A tuple is similar to a list, except that the elements in the tuple cannot be changed after initialization, so they can be understood as a read-only variable.

# use () to define a tuple a = (1,2.1, Hello) # try to modify the elements in the tuple will report an error a [0] = 100

07 dictionary

Dictionary is a very important variable type, which uses a key to access the corresponding value, that is, a data form of key-value pairs.

# define a dictionary using {} a = {} # use key to assign valuea [K1] = 1a [K2] = 2.1a [K3] = Hello

So you can sum up the differences between dictionaries and lists. The elements in the list are ordered and equivalent, so subscripts are used for assignment and access, while the elements in the dictionary are unordered, so key is used to manipulate the corresponding value.

# you can also assign values li = [1,2.1, Hello] di = {K1: 1, K2: 2.1, K3: Hello} while defining dictionaries and lists

Use has_key () to determine whether there is a key in the dictionary.

Print di.has_key (K4)

An error will be reported if you visit a key,Python that does not exist. When assigning, if the key already exists, the existing value is overwritten with the new value.

08 comments

The commented code will not run and can be seen as notes and instructions for yourself and other programmers to read to improve the readability of the code.

# here are single-line comments. Here are many line comments.

In Sublime, select the content that needs to be commented and press Ctrl+/ to complete the comment.

09 reserved characters

In Python, there are some strings that have certain functions, such as import, class, and so on. When choosing variable names, we should be careful to avoid these reserved characters.

# the following variable assignment will report an error import = 1

10 lines and indent

In Python, the boundaries of code blocks are not explicitly delimited by symbols such as curly braces, but by line indentation. Continuous code with the same indentation level is in the same code block, and you need to pay attention to the indentation of each line of code when using syntax such as for, while, if, try, and so on.

11 operator

The function of the operator is to generate new variables based on existing variables, mainly in the following ways:

Arithmetic operators: +, -, *, /,%, that is, add, subtract, multiply, divide, take remainder

Comparison operator: = =,! =, >, =

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

Development

Wechat

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

12
Report