First, the identifier and reserved word
Identifier is used to identify variable, function, class, object name of the module and can contain letters, numbers, underscores.
(1) The first character must be a letter or an underscore, and the name can not contain spaces.
(2) Python identifiers are case sensitive.
(3) In Python3, the non-ASCII identifiers are also allowed.
(4) reserved word identifier may not be used.
Reserved words also known as keywords, you can view using the following command in the console:
>>>import keyword
>>>keyword.kwlist
import keyword
import math
# Printing system Keywords
print (keyword.kwlist)
# Simultaneously to several variables to the same value
X = Y = Z = 100
Print (X, Y, Z)
# May be simultaneously assigned to a plurality of different types of variable values
A, B, C =. 1, 2, "Three"
Print (A, B, C)
# Switching values of the two variables
A, B = B, A
Print (A, B)
Two, Python standard data types
Number (number): This indicates the number of digital data.
Python 3 supports int, float, bool, complex (complex) four kinds of digital type.
String (String): This indicates the number of characters of text.
List (list): used to represent an ordered set of elements, the latter can also be changed.
Tuple (tuple): used to represent an ordered set of elements, the latter can not be changed.
Sets (set): is used to represent an unordered set of elements is not repeated.
Dictionary (Dictionary): Save a group of elements in the form of key-value pairs.
The first two are simple data types, the rear part of the structure of four kinds of data types.
# ------------------------------------------------- -
# python3 supports four digital types: int, float, bool, complex ( plural)
# ----------------------------- ----------------------
# int integer, long integer
number_int1 = 10
# built-in type () function can be used to query variable type referred to objects
Print (type (number_int1))
# rounded down, the float cast integer, decimal remove all, taking the integer part
number_int2 = int (10.95)
Print (number_int2)
# rounding up, you need to import the math module
= Math.ceil number_int3 (10.15)
Print (number_int3)
# rounding
number_int4 = round (10.55)
Print (number_int4)
# cast string to an integer, a decimal point can not
string = "100"
number_int5 = int (string)
Print ( number_int5)
# Float float
number_float1 = 10.25
# two integers and whether or not divisible, the result is a floating-point type
number_float2 = 25/5
Print (number_float2)
# int to float
number_float3 = float (100)
Print (number_float3 )
# string to floating point
number_float4 = float ( "100.5")
Print (number_float4)
# Bool Boolean: True and False, 1 and 0 corresponding to integers.
= True number_bool
# Boolean value calculation may be used to
print (number_bool + 5)
# Complex real and imaginary parts of the complex are a floating point number, the imaginary part must be a suffix letter j or J
number_complex = 5.58 + 3.25j
# real property using the real part extracted
Print (number_complex.real)
# imag properties using plural removed imaginary part
print (number_complex.imag)
# ------------------------------------------------- -
# string in single quotes, or three double quotes quotes
# -------------------------------- -------------------
Version = "Version: 3.6.4 Python"
Print (Version)
# use the backslash (\) escape special characters such as: \ n , \ r, \ f, \ v, \ t
Version = "Version: \ the n-Python 3.6.4"
Print (Version)
# do not want the backslash (\) escape occurs, you can use \\ or string add a prefix in front of r, represents the original string
Version = "Version: 3.6.4 \\ n-Python"
Print (Version)
Version R & lt = "Version: \ n-\ R & lt Python 3.6.4"
Print (Version)
# the integer forced into a string
I = 2018
date1 = STR (I)
Print (date1)
# the float cast string
F = 2018.5
DATE2 = STR (F)
Print (DATE2)
# If the string contains a single quote, double quotes defined
str1 = "It 's very good!"
Print (str1)
# If the string contains double quotation marks, the use of single quotes defined
str2 = 'string "Hello" is a combination of characters from 5'
Print (str2)
# when the string is longer than one line must be used triple quotes
str3 = "" "Patio spring snow
Northland scenery, ice, A thousand Piao.
look inside and outside the Great Wall, but I vast;. up and down the river, will lose their surging
mountain dance Silver Snake, the original Chi wax like, with the desire heaven in stature.
On a fine day, to see the red dress in white, particularly enchanting. "" "
Print (str3)
str4 =" Patio spring snow \ n Northland scenery, ice, A thousand Piao. \ n hope Wall inside and outside, but more than vast; up and down the river, will lose their surging \ n mountain dance Silver Snake, the original Chi wax like, with heaven in stature \ n to be a fine day, red fitted in white, particularly enchanting. "..
Print ( str4)
# ------------------------------------------------- -
# list list represents an ordered set of elements, similar to arrays in other languages
# ----------------------------- ----------------------
# define a list of all digital score
score = [90, 92, 88, 79, 95, 82]
# index by value to access elements in the list, from left to right, a starting value of 0, is the last element. 1-n-
first_one Score = [0]
# can also be accessed from right to left by a negative index value, the first element is -1, the last element of -n
last_one Score = [-1]
# list of data elements can be of different types
student = [ '1001', 'Zhangxiao Xiao', 'woman', 12, [86, 88 , 92]]
# ------------------------------------------------- -
# tuple tuple represents an ordered set of elements, and a list of similar use. After the definition can not be changed
# --------------------------------------------- ------
# use parentheses to define a full tuple string
subject = ( 'language', 'mathematics', 'English', 'political')
# may be omitted parentheses, direct assignment
student_info = '1001', 'Zhang Xiaoxiao', 'female', 12
# ------------------------------------------------- -
# sets collection is a set of unordered elements can not be repeated, can not be accessed using an index
# ---------------------------- -----------------------
# braces {} to create a collection, and automatically remove duplicate elements
fruits = { 'apple', 'banana', ' orange ',' strawberry ',' apple '}
# can create a list, then set () function list into a set of
types = [' number ',' string ',' list ',' meta group ', 'set', 'dictionary', 'set']
type_set = sET (types)
# Create an empty set, use must sET (), can not be used} {
null_set = sET ()
# ------------------------------------------------- ------
# dictionary Dictionary is another variable container model, objects can be stored in any type
# syntax: D = {key1: VALUE1, key2: value2, key3: value3}
# by a plurality of keys (key ) - The value (value) pairs. Key must be unique, but the value may not be unique.
# ------------------------------------------------- ------
Stud = { 'Student ID': '1001', 'name': 'Zhangxiao Xiao', 'sex': 'M', 'Age':} 12 is
Print (Stud [ 'name'])
Print (stud [ 'aged'])
Stud [ 'aged'] = 13 is
Print (Stud)
Three, Python language priority supports the following types of operators & operator (highest to lowest):
---------------------------------------- #
# arithmetic operators: +, -, *, /,% ** //
# -------------------------------------- -
A = 10
B =. 3
# + added, adding two objects
C = A + B
Print ( "= A + B", C)
# - Save, negated or subtract two objects
C = -a
Print ( "= -a", C)
C = a - B
Print ( "a - B =", C)
# * multiplication, multiply two numbers or be repeated several times to return a string of
C = a * B
Print ( "* B = a", C)
C = 'ABC'. 3 *
Print ( " 'ABC'. 3 = *", C)
# / in addition, the division of two numbers to return to afford float
c = a / B
Print ( "a / B =", C)
#% modulo two numbers and returns the remainder of division
C = a% B
Print ( "% a = B", C)
# ** power, a ** b represents a return to the power b
C b = a **
Print ( "** B = A ", C)
# // take divisible, divided by the number after two returns the integer part of commercially
c = a // b
print("a // b =", c)
input()
---------------------------------------- #
# comparator (relationship) operator: = =,! =,>, <,> =, <=
# ----------------------------------- -----
# == equal, compare whether two objects are equal
iF A == B:
Print ( "A equals B")
the else:
Print ( "A not equal to B")
# = not equal, compare the two! objects are not equal
! IF a = B:
Print ( "a is not equal to B")
the else:
Print ( "a equals B")
#> is greater than, if a than b, that returns True; otherwise, returns False
IF a> B:
print ( "a greater than B")
the else:
print ( "a less than B")
# <is less than, if a is less than B, returns True; otherwise, returns False
IF a <B:
print ( "a less than B")
the else:
Print ( "a greater than B")
#> = greater than or equal, if a B greater than or equal, returns True; otherwise, returns False
IF a> = B:
Print ( "a is greater than equal to b ")
the else:
Print (" a is less than b ")
# <= Less than or equal, or less if a B, returns True; otherwise, returns False
IF a <= B:
Print ( "a less than equal to B")
the else:
Print ( "a greater than B")
INPUT ()
---------------------------------------- #
# assignment operator: =, + = , - =, * =, / =,% =, ** = // =
# ----------------------------- -----------
# simple assignment operator =
c = a + B
Print ( "= a + B", C)
# + = addition assignment operator is equivalent to c + = a c = A + C
C = A +
Print ( "+ C = A, C =", C)
# - = subtraction assignment operator c - = a = C equivalent to C - A
c - = a
Print ( "C - = A, C = ", C)
# * = multiplication assignment operator c * = a * is equivalent to A C = C
c * = a
Print (" c * = a, = C ", C)
# / = division assignment operators c / = a is equivalent to c = c / A
c / = a
Print ( "c / = a, = C", C)
#% = modulo assignment operator is equivalent to c% = a c = c A%
C% = A
Print ( "C% = A, C =", C)
# ** exponentiation assignment operator = c ** = a C ** is equivalent to A = C
c ** = a
Print ( "c **= a, c =", c)
# @ = Fetch divisible assignment operator is equivalent to A = C // C // A = C
c // = a
Print ( "c // = a, = C", C)
INPUT ()
---------------------------------------- #
# logical operators: and, or, not
# ----------------------------------------
A = True
b = False
# and and calculating, when all the conditions are returned as if True True, otherwise False
iF a and b:
Print ( "variables a and b are True")
the else:
Print ( "variables a and b wherein a is False, or both is False ")
# Or OR operation, as long as one of the conditions is True returns True, otherwise False
IF a or b:
Print ( "variables a and b wherein a is True, or are True")
the else:
Print ( "variables a and b are False ")
# Not a non-operational, when the variable to True and False, returns True when variable is False
IF not a:
Print ( "a variable is False")
the else:
Print ( "a variable is True")
the INPUT ()
Mobile operators left: each of the binary number to the left operation all of the left, the number of bits specified by the movement of the right operand. Discarding high, low fill 0 C = A << 2 = 1111 0000 # 240
print ( "a << 2 values:", C)
# operator >> right movement: the whole of each bit of the binary arithmetic right shifts left the number of bits specified by the movement of the right operand. Discarding low, high bit 0
C = A # 2 15 = 0000 >> 1111
Print ( "A value of 2 >>:", C)
INPUT ()
# ----------------------------------------
# member operator: in, not in
---------------------------------------- #
A = 'lotus'
B =' yellow '
Flowers = [' osmanthus', 'daisy', 'rose', 'peony', 'lotus']
# in the specified element returns True later sequence, otherwise False
IF a in Flowers:
Print ( " the variable a in the list of flowers in ")
the else:
Print (" variable a is not in the list of flowers in ")
# not in, if not in the back of the specified element in the sequence returns True, otherwise return False
IF b not in flowers:
Print (" variable b is not in the list of flowers ")
the else:
Print (" variable in the list of flowers in b ")
the INPUT ()
---------------------------------------- #
# identity operator: it is, is not
---------------------------------------- #
A = 'Python 2'
B = 'Python3 '
# iS Analyzing two identifiers are not self-reference the same object. If the reference is to the same object it returns True, otherwise return False
IF A IS b:
Print ( "A and b have the same identity")
the else:
Print ( "A and b are not the same identity")
# IS not judge two a reference identifier is not different from the object. If the reference is not the same object returns True, otherwise it returns False
IF A IS not b:
Print ( "A and b are not the same identity")
the else:
Print ( "A and b have the same identity")