Day3- learning user interaction with Python, underlying data type, operator

A. Interact with the user program

1.1 What is the interaction with the user?

The program waits for the user to enter some data, and then completes the program for the user feedback information.

1.2 Why the program to interact with the user?

In order to let the computer like a human can interact with the user.

1.3 How to use?

In python3 in: input

= the INPUT name ( ' Please enter your name: ' ) 
Please enter your name: Alex Print (name, of the type (name)) Alex < class ' str ' >

python3, any user will input the contents of the input are stored as string type.

python2 in, input the user must enter a specific data type, enter what type Why would save type.

python2, any user input content raw_input will input function are stored as a string of the same type python3.

1.4, formatted output

1) What is the formatted output?

The something inside a string and then replaced after the output is formatted output.

2) why you want to format the output?

We often have the output format of some fixed content, such as' xxx Hello dear! XX your monthly phone bill is xxx, the balance is xxx ', we need to do is to replace the xxx specific content.

3), how to format the output?

This placeholder is used:% s,% d.

# % S placeholder may receive any type of placeholder values #% d can only receive a digital value

Print ( ' Dear% s Hello! You are% d% d month calls, the balance is% s ' % ( ' Tony ' , 11,84.5,112 )) 
 # EG receives user input to the specified print format 
name = input ( ' your name: ' ) # user input Age 
Age = iNPUT ( ' your Age: ' ) # user input Age 
sex = iNPUT ( ' your sex: ' ) # user input sex 
Job = iNPUT ( ' your Job: ' ) # Professional user input 
Print ( ' - ' 12+ 'info of% S ' % (name) + ' - ' 12 is ) 
 Print ( ' the Name:% S ' % (name)) 
 Print ( ' Age:% S ' % (Age)) # user to enter numbers, it will be saved as character string, D can not pass% 
Print ( ' Sex: S% ' % (Sex))
 Print ( ' the Job: S% ' % (Job)) 
 Print ( ' - ' 16 + ' End ' + ' - ' 16) 
#===================================== 
'''
------------info of tony ------------ 
Name : xxx 
Age  : xxx 
Sex  : xxx 
Job  : xxx 
----------------end----------------
'''

 

Second, the data type

2.1, numeric types: int and float

1), integer: int

Role: Record age, registration, QQ number and variety of numbers.

# Age = 18 is equivalent to int = Age (18 is) 
# Print (Age, type (Age))

2), floating-point type: float

Role: record height, weight, payroll and so on.

# The salary = = a float 2.1 is equivalent to the salary (2.1) 
# Print (the salary, type (the salary))

2.2, string types: str

Role: Record describing the nature of the data, such as the person's name, gender, home address, company profile and so on.

Definition: within quotation marks comprising sequentially from left to right in the order of a data, quotes may be a single and double quotation marks, three marks.

name = 'egon' 
print(name,type(name))

May be added between the string: nature apply for a new memory space, and then added to the new copy of the string of space, efficiency is not high.

a = 'Hello,' 
b = 'World!' print(a+b) 
#结果Hello,World!

The string can also do multiplication

print ( ' Hello ' * 5 ) 
 # 结果HelloHelloHelloHelloHello

2.3 List type: list

Role: record / store multiple values, you can easily remove the value of the specified locations, such as multiple-loving people, a bunch of student names.

Definition: re [] values ​​within a plurality of spaced apart any type of comma.

l = [10,3,1, ' Egon ' [ ' a ' , ' b ' ]] # l = list ([10,3,1, 'Egon' [ 'a', 'b']]) print (l, type (s)) 
print (l [0]) 
 10 
 print (l [3 ]) 
 ' Egon '

2.4 Dictionary type: dict

Action: recording a plurality of key: value value, each value is the advantage has its correspondence relationship / key mapping relationship, and the value of the key functional descriptive

Definition: then {} separated by commas within the plurality of key: value element, which value can be any data type, but generally should be a string type key.

info = {'name':'egon','sex':'male','age':18} #info = dict({'name':'egon','sex':'male','age':18}) 
print(type(info)) 
print(info['name'])

2.5 Boolean type: bool

Action: is used as a condition to judge with

definition:

tag = true #tag = bool(true) 
tag = false 
print(type(tag))

Boolean value obtained by the determination to:

18 is = Age 
 Print (Age 18 is ==) # == is the comparison values are identical 
# ============================== ======
X. 1 = Y =. 1 Print (X iS Y) # iS comparison whether the address is the same as id

Third, the operator

3.1, arithmetic operators

3.2, comparison operators

3.3 assignment operator

3.3.1 Augmented assignment

3.3.2, chain assignment

If we want to assign the same value simultaneously to multiple variable names, you can do

z = 10 
y = z 
x = y 
x,y,z 
(10,10,10)

Chain assignment means that you can get it with a single line of code

x = y = z = 10 
x,y,z 
(10,10,10)

3.3.3, cross assignment

We define two variables m and n

m = 10 n = 20

If we want to swap the values ​​of m and n is over, you can do so

m = 10 
n = 20 
a = m 
m = n 
n = a 
print(m,n) 20,10

Cross-assignment means that you can get it with a single line of code

= 10 m  
n- = 20 is  
m, n- = n-, m # cross assignment 
Print (m, n-)

3.3.4, unzip assignment

If we want more value in the list in order to take out a number of variables assigned to the name, you can do so

nums=[11,22,33,44,55] 
a=nums[0] 
b=nums[1] 
c=nums[2] 
d=nums[3] 
e=nums[4] 
a,b,c,d,e 
(11,22,33,44,55)

Decompression assignment refers to a single line of code can handle it

= the nums [11,22,33,44,55 ] 
a, b, c, d, E = the nums # the nums comprises a plurality of values, like a compressed, thus pressing the name assignment a, b, c, d , E 
(11,22,33,44,55)

Note that the above extract the assignment, the number of variables were equal sign on the left and on the right the same number of parable contains the value, otherwise it will error

But if we want to go head to tail a few values, you can use * _ match.

3.4, logical operators

3.4.1, and a plurality of continuous

And connecting a plurality of conditions can be used, in turn determines the order from left to right, once a condition is False, the determination no longer right, immediately decides the final result is False, the result only when all the conditions have not True under the circumstances, the end result was to True.

2>. 1 and . 1! =. 1 and to true and . 3> 2 # determines End second condition is terminated immediately, the final result is obtained False False

3.4.2, or a plurality of continuous

Or can be connected to a plurality of conditions, determined in turn from left to right in the order, once a condition is True, no longer right judgment, can immediately determine the final position True result, only the results of all the conditions are under the False case, the result was as recently False

2>. 1 or . 1! =. 1 or to true or . 3> 2 # End Analyzing the first condition, ends immediately, the final result is obtained True True

3.4.3、混用and、or、not

# And, or, when mixed, if not three, there is a priority of the points of time, but we need to remember daily development priorities should use # () to prioritize and enhance the readability 
(3> 4 and . 4>. 3) or ((==. 3. 1 and  ' X ' == ' X ' ) or . 3>. 3 ) 
False

3.5, member operator

Subject: Although under two kinds of judgment can achieve the same effect, but we recommend using the second form, because not in the semantics more explicit.

not 'lili' in ['jack','tom','robin'] 
True
'lili' not in ['jack','tom','robin'] 
True

3.6, the identity of the operator

It is emphasized that: == double equals sign comparison is whether the value want to wait, and is comparable is the id are equal

Guess you like

Origin www.cnblogs.com/snailhuang/p/11783655.html