Fundamentals of Python language

Basic syntax and variables

print('hello world!')

1, Basic grammar

1. Common shortcut keys

ctr + / - add or remove comments
ctr + s - save
ctr + c - copy
ctr + v - paste
ctr + x - shear
ctr + a - select all
ctr + z - undo
ctr + shift + z - reverse undo

2. Notes

Comments are the parts of the code that will not be compiled and executed (not interpreted by the interpreter). The existence of comments will not affect the program function

Value of notes:

1) annotate and explain the code to increase the readability of the program
2) make the valid code function disappear

1. Single line note: add before the note content#

'''
2.Multiline note 1:
            
'''
"""
Multiline note 2:


"""

3. Statement

A valid code is a statement
1) Generally, a statement occupies one line, and a semicolon may not be added after the end of a statement
2) Indents (spaces, tabs...) cannot be added at the beginning of a statement

print('Hello')
print('Python')

4. Identifiers and keywords

Identifier (naming requirement) - consisting of letters, numbers or underscores' ', And the number doesn't start. (after Python 3. X, Chinese can be used in the identifier)
Variable name = (value)

Keyword - some identifiers whose existence has special meaning or special function

import keyword
print(keyword.kwlist)	# View keyword list

The following is a list of keywords output from the console:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

5. Common data and data types

1) Digital data - data used to represent the size of numerical values, such as age value, height value, distance value

The representation of digital data in the program is the same as that in mathematics, such as 18, + 20, - 30, 1.23, etc. (1e3 == 1*10^3)
There are three types of numbers: int, float and complex
2) Text data - data that provides data in text, such as name, company name, school name, home address, etc.
Single quotation marks or double quotation marks shall be added when representing text data in the program, such as' Chengdu, Sichuan ',' Chengdu, Sichuan '
Type corresponding to text: str (string)
3) Boolean data - True and False in the program, use True to indicate positive and True, and use False to indicate negative and False data is Boolean
Boolean corresponding type: bool (Boolean)
4) Other common data types: list, dict, tuple, set, iterator, generator, function, custom type

print(1e3)
print(3e-4)
print('Chengdu, Sichuan')
print("Chengdu, Sichuan")
print('182XXXXX058')
print(True)
print(False)

Get data type - type

print(type(20))
print(type(3e-4))
print(type(True))

Type conversion

print(int(3e-4))
print(int(3.99))
print(float(10))
print(int(True))    # 1
print(int(False))   # 0
print(int('2021'))   # A string that is itself an integer without quotation marks
print(float('2021'))
print(float('3.14'))    # The string can be converted to floating-point type by removing quotation marks and numbers

2, Input and output functions

1. Output function (print data to console) - Print

1) Print single data: Print (data) / print (expression with result)

print(123)
print('abc')
print(type(123))
print(100 + 2)

2) print multiple data at the same time: print (data 1, data 2, data 3,...)

print(1, 2, 3, 4, 1.1, 'abc', False, 1 + 1)
print(1, '/', 2, 1 / 2)

3) Custom end symbol

The default end of print is end = '\ n' (newline character). Each print has an end

print(100)  # print(100, end= '\n')

print(100, end=';')
print(200, end=' ')
print(1)
print('abc', 100, end='. \n')

4) Custom data separator

sep= ' '

The default value of the data separator is space, which controls the division method between data when multiple data are printed at the same time

print(100, 200, 300, sep='/')
print(100, 200, 300, sep='+', end='=')
print(100 + 200 + 300)

2. Input function - input

Variable = Input - enter content from the console and save the input to the variable

name = input('Please enter your name:')
print(name, type(name))	#Type to output and view the name type

Through the input function, no matter what the input content is, the type of data returned is a string (str)

a = float(input('Number 1:'))
b = float(input('Number 2:'))
print(a + b)

3, Variable

1. What are variables

A container for saving data in a program. After saving data to variables, you can use data by using variables

2. Define variables (save data to variables)

Syntax: variable name = value
explain:
Variable name - named by the programmer
Requirement: identifier; Not a keyword
Specification: see the name and meaning (you can probably know what data is saved in the variable when you see the variable name). If there are multiple words, the words are separated by underscores, and the system function name, class name and module name are not used
=- (assignment symbol) fixed writing
Value - any expression with results, such as specific data, operation expression and function call expression

dog_name = 'Wangcai'
name = 'Li Lei'
print(name, dog_name)

3. Use variables

Using variables is to use the data stored in variables

num1 = 10.0
num2 = 11
print(num1 + num2, type(num1), type(num2))

4. Re assignment (different types of values can be assigned)

name = 'Li Lei'
print(name)
name = 'LL'
print(name)

5. Define multiple variables at the same time

1) Define multiple variables at the same time and assign the same value

a = b = c = 200
print(a, b, c)

2) Define multiple variables at the same time and assign different values

x, y, z = 10, 20, 30
print(x, y, z)

Keywords: Python

Added by zaneosak on Mon, 17 Jan 2022 08:27:46 +0200