1- & basic data types Python basic operators

  • Interaction with the user

python3 in

Nothing will be input by the user input are stored as a string type

 

python2 in

Nothing will raw_input user input are stored as a string type

name=raw_input('>>>>>>>>>>>>>>>>>>>>>>>>>>>>: ')
>>>:egon
>>> print(name)
egon
>>> print(name,type(name)
>>> ('egon', <type 'str'>)

name=raw_input('>>>>>>>>>>>>>>>>>>>>>>>>>>>>: ')
>>>:1231231231232
>>> print(name,type(name))
>>> ('1231231231232', <type 'str'>)
>>> name
>>> '1231231231232'
>>> name,type(name)
>>> ('1231231231232', <type 'str'>)

input the user must enter a specific data type, enter what type it into what type of deposit

>>> x=input("输入: ")
输入: egon
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'egon' is not defined

>>> x=input("输入: ")
输入: 'egon'
>>> x,type(x)
('egon', <type 'str'>)


>>> x=input("输入: ")
输入: [1,2,3]
>>> x,type(x)
([1, 2, 3], <type 'list'>)
>>> y=raw_input('####: ')
####: [1,2,2,3]
>>>
>>> y,type(y)
('[1,2,2,3]', <type 'str'>)
  • Formatted output

% D% s and

name = INPUT ( 'Please INPUT your username:') 
Age = INPUT ( 'Please INPUT your Age:') 
Print ( 'My name IS', name, 'My Age IS', Age) 
Print ( 'My name IS% S , my age is% S '% (name, Age)) 

' '' 
username my name is entered, my age is inputted age 
'' ' 

Print (' my name is% S My Age iS% S '% ( 18 is, 'Egon')) 


Print ( 'My name IS% S My Age IS% D'% ( 'Egon', 18 is)) 
Print ( 'My name IS% S My Age IS% S'% ( 'Egon', 18 is)) 
print ( 'my name is% s my Age iS S%'% ( 'Egon', [l, 2,3])) #% S may receive any type of value 
print ( 'my name is% s my age is% d '% (' egon ',' xxx ')) #% d can receive the digital type
  • type of data
  • Probably there are so few data types: integers, floats, strings, dictionaries, lists, boolean, etc.
       Integer (Integer)
# Effect: recording qq, age, ID number and other information 
defined: 
my_qq_id = 1,025,100,056 # is equivalent to int = my_qq_id (1,025,100,056) 
Print (my_qq_id, type (my_qq_id)) 
wherein Python2 If the definition of an integer, will be divided into long integer and categories 
my_age = 22 is
 Print (type (my_age))
 >>> int # int 
the else 
X = 12356788990023466666666788544243
 Print (type (X))
 >>> long # long
       Float (float)
# Effect: accurate information recording slight height, weight, and so the number of shooting ring 
Def: 
my_weigh = 161.4 # is equivalent to int = my_weigh (161.4) 
Print (my_weigh, type (my_weigh))  
       Type String (String)
# Effect: Record countries, provinces and cities of residence, Taobao shipping address and other information 
Def: 
My_hometown = ' Yangzhou ' 
Print (of the type (My_hometown))
 # Note: 1 between strings can be summed 
Province = ' JiangSu ' 
City = ' of Yangzhou ' 
Print (Province + City)
 # 2. The string can be a digital operation 
Print (10 * ' of Yangzhou ' )
     Dictionary (Dictionary)
# Role: recording a plurality of key: value value, and is represented by mapping key and value, the key is in the form of name, value is the essence of its content. 
DEF:                      # Key may be a string type, value can be a string data type may be other types = China 
{ ' Captial ' : ' Beijing ' ,        
 ' Population ' : ' 14 Billion ' ,       
 ' Full name ' : ' People Republic China of ' }
 Print (China [ ' Captial ' ]) 

# may represent a data type having a plurality of object characteristics, and it is possible to know the information corresponding to characteristics of a person, a country, a historical period so You may be used to indicate different types of data
     List (List)
# Role: recording in the same category multiple sets of data, a value which is easily removed, such as a permanent member of the United Nations mentioned may think of China, Russia, Britain, France, the United States 

Def:    # comma plurality of sets of strings, and other data separated type, and the first number is 0 from the start of the call 
United_of_Nations = [ ' China ' ,
 ' Russia ' ,
 ' England ' ,
 ' French ' ,
 ' America ' ]
 Print (United_of_Nations [0]) 

# by superimposing [ ] for the list of in-depth data call
     Boolean (Boolean)
# Effect: Analyzing logic operation or the like is correct or not is determined directly by the Boolean logical operators but by 
Def: 
X = 29 <30
 Print (X) 
Y = ' ZZC ' > ' pH '  # can also be used the comparison between the strings, beginning from the first character comparison, the same comparison skip next print (y)
  • Basic operators

 

# Arithmetic 
RES =. 1 +. 3 Print (RES)
 Print (. 1 +. 3 )
 Print (10 /. 3) # Results decimals portion Print (10 //. 3) # leaving only the integer part Print (10%. 3) # take the remainder Print (2 **. 3 ) # comparison operation: == => <> = <=! 
# knowledge of: 
# may compare the size between each digital Print (10>. 3 )
 Print (10> 3.1 ) # and the string only You can compare the size of the character string (the character corresponding to the position according to the ASCII table to compare the reference) 
MSG1 = ' Hello ' 
Msg2 = ' Z '









Print (MSG1> Msg2) 

# A-Za-Z 
Print ( ' A ' > ' the Z ' )
 Print ( ' the Z ' > ' the Y ' )
 Print (len ( ' Hello ' )>. 3 )
 Print ( ' A ' >. 3 ) 
 
# lists are only compared in magnitude with a listing (according to the value corresponding to the position of sequentially comparing the corresponding values of the position must be the same type) 
L1 = [l, 2,3 ] 
L2 = [10 ,]
 Print (L2> L1) 
L3 = [10, 2,' B ' ,. 3 ] 
L4 = [10,2, ' B ' , ' C ' ]
 Print (L3> L4) 




# assignment operator 
Age = 18 is # incremental assignment 
Age +. 1 = # Age Age +. 1 = Print (Age ) # chain assignment 
X = 100 
Y = X 
Z = X 
X = Y = Z = 100
 Print (ID (X), ID (Y), ID (Z)) # intersecting assignment 
m = 1000 
n- = 2000 # TEMP = m 
# m = n- 
#








TEMP = n- 
n-, m = m, n-
 Print (m, n-) 

# decompression assignment 
ual relations = [11,22,33,44,55 ,] 
MON1 = ual relations [0] 
MON2 = ual relations [. 1 ] 
MON3 = ual relations [2 ] 
MON4 = ual relations [. 3 ] 
MON5 = ual relations [. 4 ] 
MON1, MON2, MON3, MON4, MON5 = ual relations
 Print (MON1, MON2, MON3, MON4, MON5) 

# number of values must contain an equal sign and an equal sign right the number of variable names coincide left 
MON1, MON2, MON3, MON4, MON5, MON6 = ual relations 
MON1, MON2, MON3, MON4, = ual relations 

_ = 3333
 Print (_) 
MON1, MON2, _, _, _=salaries  # 下划线最终被赋值为55
mon1,mon2,*_=salaries
print(mon1)
print(mon2)

salaries=[11,22,33,44,55,]
first=salaries[0]
last=salaries[4]

first,_,_,_,last=salaries
first,*_,last=salaries
print(first)
print(last)

 

  • logic operation
20 is = Age 
Sex = ' FEMALE ' 
# logical operators 
# and: connecting the left and right two conditions, only the final result in the case where the two conditions are true only for the True 
Print (Age> 18 is and Age <26 is and Sex == ' FEMALE '  and . 1>. 3 ) 

# or: connecting the left and right conditions, whenever a condition is established for the final result True 
Print (. 1>. 3 or 2>. 4 or  ' X ' == ' Y '  or . 1 ==. 1 ) 

# Not 
Print ( Not . 1>. 3 )
 Print (not (1 > 3 or 2 > 4 or 'x' == 'y' or 1==1))

res=(3>4 and 4>3) or (1==3 and ('x' == 'x' or 3 >3))
print(res)

 

Guess you like

Origin www.cnblogs.com/ITchemist/p/11117592.html