Primer
We mainly learned on a python as a programming language, and today it is about learning content on user interaction, data types and operators in terms of
User interaction
What is the user interaction?
User interaction is human input data into the computer (input), computer output (print)
A very simple example, we enter the following code in the pycharm
the INPUT = age ( " Please enter the Age: " ) # to enter data into a computer Print (of the type (age)) # let the computer age output data types
At this time, the computer will receive user input, in fact, no matter what type of data input, the final return must be a string (str)
PS: python2 must declare the type of input you entered, python2 the raw_input and input in the same python3
Formatted output
The something inside a string and then replaced after the output is formatted output.
How to format the output?
1, placeholders, such as:% s,% d:
# % S placeholder: You can receive any type of value # % d placeholders: only receive a number, if not a number will error Print ( ' Dear% s% s Hello you sign up month course has been successful! , tuition D% ' % ( ' Tom ' , 10, 18000))
The output is:
2、format
Ado, a direct example:
# .Fomat usage name = ' Tom ' Age = ' 18 is ' Print ( " My name IS {}, {} IS My Age " .format (name, Age)) # can also be used the following methods Print ( " My name IS name} {, My Age Age {IS} " .format (name = name, Age Age =))
The results are output
3, f-string (this is the only version will be after the python3.6)
# f-string的用法 name = 'Tom' age = '18' print(f" my name is {name}, my age is {age}")
type of data
1, integer int (define: 'except X' telephone number, ID number, age)
PS: Long long: only python 2 has a long integer, long range -2147483647 2147483647, i.e. -2 ^ 31 to 2 ^ 31-1
age = 18 # age=int(18) print(id(age)) print(type(age)) print(age)
140718798304048
<class 'int'>
18
2, float folt (definable weight, height, salary)
salary = 2.1 # salary=float(2.1) print(id(salary)) print(type(salary)) print(salary)
2035028452208
<class 'float'>
2.1
3, the string str (define loving, personal profiles)
name1 = 'Tom' name2 = "Sam" print(id(name1)) print(type(name1)) print(name1)
1398355144944
<class 'str'>
Tom
PS: Here mention
str nature python 2 has in fact a sequence of 8-bit bit
str nature python 3 is actually a unicode sequence
Use indistinguishable quoted string (single lead, double lead, triple quotes can), but can not mix
If you need to have a quoted string, you must use a nested
String concatenation that has opened up a new space, into the data
4, the list of list (stored values of one or more different types)
Any type of value are separated by a comma within [].
hobby = 'read' hobby_list = [hobby, 'run', 'girl'] print(id(hobby_list)) print(type(hobby_list)) print(hobby_list)
2855587238472
<class 'list'>
['read', 'run', 'girl']
Deposit is not an end, is taking aim, we introduce the method list index value, bearing in mind the index number from zero.
= hobby_list [ ' Read ' , ' RUN ' , ' Girl ' ] # index number 012 # Remove second interested Print (hobby_list [. 1])
run
= hobby_list [ ' Read ' , ' RUN ' , [ ' girl_name ' , 18 is, ' Shanghai ' ]] # removed girl age Print (hobby_list [2] [. 1])
18
5, the dictionary dict
effect
A plurality of values used to access, in accordance with key: value of the stored-value mode, may not take the time to go to the index value by the key, key has a function of descriptive value. Store a variety of types of data and more data when you can use a dictionary.
usage
{} In the plurality of elements separated by commas, each element is key: value format, the format in which the value is an arbitrary data type, since the key has a function descriptive, the key is usually a string type.
# Dictionary set list USER_INFO = { ' name ' : ' Tom ' , ' Gender ' : ' MALE ' , ' Age ' : 20 is , ' company_info ' : [ ' Oldboy ' , ' Shanghai ' , 50 ]} Print (USER_INFO [ ' name ' ]) Print (USER_INFO [ ' company_info ' ] [0])
tom
oldboy
# Dictionary sleeve dictionary USER_INFO = { ' name ' : ' Tom ' , ' Gender ' : ' MALE ' , ' Age ' : 20 is, ' company_info ' : { ' c_name ' : ' Oldboy ' , ' c_addr ' : ' Shanghai ' , ' c_num_of_employee ' : 50 }} print(user_info['name']) print(user_info['company_info']['c_name'])
tom
oldboy
6, Boolean type bool
Mainly for judging right or wrong things, only two values: Ture and False, not separately defined general Boolean type
# Boolean type A. 1 = B =. 1 Print (A == B) # == is the comparison value Print (A IS B) # IS is compared id
True
True
PS: a case where the values are equal, id not necessarily the same
The same id, it necessarily the same
Operators
Arithmetic Operators
# Arithmetic operators print (. 1 + 2) # plus print (. 1 - 2) # Save print (2. 1 *) # by print (. 1/2) # inter print (10 //. 3) # divisible only rounded print (10% 3) # modulo Print (10 ** 3) # power
Output:
3
-1
2
0.5
3
1
1000
Comparison Operators
# Comparison operators A = 10 B = 20 is Print (A == B) # Analyzing equal
Print (A! = B) # judged not equal Print (A> B) # judged greater than Print (A> = B) # judged greater than equals Print (A <B) # Analyzing less than Print (A <= B) # Analyzing less
Output:
False
True
False
False
True
True
Assignment Operators
age = 19 age = age + 1 print(age)
20
=. 19 Age Age + = #. 1 plus equals Print (Age)
20
=. 19 Age Age * # = 10 by equal Print (Age)
190
Chain assignment
# Chain assignment X. 1 = Y =. 1 Z =. 1 X = Y = Z Print (X, Y, Z)
1 1 1
Cross assignment
# CROSS assignment A =. 1 B = 2 Print (A, B) a, b = b, a print(a, b)
1 2
2 1
Decompression assignment
# Decompression assignment L1 = [. 1, 2,. 3,. 4 ] L2 = [1, 2, 3, 4, 5, 6 ] A, B, C, D = L1 Print (A, B, C, D) # At this time, if the print error would l2 # * _ accept overflow elements A, B, C, D, * _ = l2 Print (A , B, C, D, * _) Print (_ *, A, B, C, D)
1 2 3 4
1 2 3 4 5 6
5 6 1 2 3 4
Logical Operators
and: there is a return False, the whole equation are False
or: There is a return Ture, the whole equation are Ture
not: not true (inverted)
# Logical operators Print (10 <20 is and 20 is> 30 and 30 <40) # a return False, whole expression are False Print (10> 20 is or 20 is> 30 or 30 <40) # has a return Ture , the whole equation are Ture
false
True