User interaction, basic data types and their use

First, the procedures and user interaction

  The user interaction is input to the computer people / input data, the computer print output, the nature / interaction is input, the output

  1, input input

      input function and in python3 in python2 there are some differences:

# In python3 in, input function will wait for user input, the user input any type of content will become a string, and then assigned to the variable name on the left 
>>> name = the INPUT ( " Please enter your name: " )
Please enter your name: abc     # name = "abc" 
>>> the INPUT = password ( " Please enter your password: " )
Please enter your password: 123     # password = "123" 
>>> of the type (name)
 < class  ' str ' >
>>> type(password)
<class 'str'>

# In python2 in, input feature requires the user to input a specific data type 
# but there are a python3 and the same usage is raw_input 
>>> name = the INPUT ( " Please enter your name: " )
Please enter your name: abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> name = input("请输入你的名字:")
Please enter your name: " abc " 
>>> name
 ' abc ' 
>>> of the type (name)
 <of the type ' str ' >
Password = the INPUT >>> ( " Please enter your password: " )
Please enter your password: 123
>>> type(password)
<type 'int'>
The INPUT name = >>> ( " Please enter your name: " )
Please enter your name: [ 1,2 ]
 >>> type (name)
 <type ' List ' >
>>> name = raw_input("名字:")
名字:[1,2]
>>> type(name)
<type 'str'>

  2, the output print

      Some content formatted output is to replace the inside of a string before outputting

      Output format in three ways:

      a, placeholder% d,% s

# % S placeholder may receive any type of data 
# % D can only receive digital placeholder

>>> Print ( ' Your name is% s, your age d% ' % ( ' abc ' , 18 ))
Your name is abc, your age is 18

      b, using the format method

# The format according to the received data sequence before and after 
>>> Print ( ' your name: {}, your age: {} ' .format ( ' ABC ' , 18 is ))
Your name: abc, your age: 18

# The format data received in a corresponding relationship 
>>> name = ' ABC '
>>> age = 18
>>>
>>> Print ( ' Your age: {age}, your name: {name} ' .format (name = name, Age = Age))
Your Age: 18 Your name: abc

        c, f-string using the method of

# f"{}"方法
>>> name = 'abc'
>>> age = 18
>>> Print (f " your age {age}, your name, name} { " )
Your age 18, your name abc

Second, the basic data types

  Learn variable is to allow computers to like people to remember things in a certain state, and the state of things there are a variety of types, such as a person's height, weight, age, position, etc., and will have different data variables Types of

  Data : description, measure the state of the data

  Type : different things require different types of storage

  1, digital type

    a, int int

      age = 18 essentially: age = int (18)

# In python3, regardless of how much the value of integer data types are int
>>> a = 11111111111111111111111111111111
>>> type(a)
<class 'int'>

# In python2 in the range [-24xxxxxxxx, 24xxxxxxxx]: int 
#                        not in this range: long
>>> a = 1111111
>>> type(a)
<type 'int'>
>>> a = 1111111111111111111111111111111111111
>>> type(a)
<type 'long'>

    b, float type floating-point

      weight = 65.4 nature: weight = float (65.4)

>>> weight = 65.4
>>> type(weight)
<class 'float'>

  2, str string type

    Definition: keep some descriptive information, keep personal interests, personal profiles

     Quoted string is no difference, but can not be mixed
     , if needed quoted string, it must be nested

     Essence s1 = 'abc' is: s1 = str (abc)

# To python3 STR sequences are really Unicode sequence 
>>> SS1 = ' ABC ' 
>>> type (SS1)
 < class  ' STR ' >
>>> ss1 = ss1.encode('utf-8')
>>> ss1
b'abc'
>>> type(ss1)
<class 'bytes'>

# Python2 STR sequences are really 8-bit bit sequence 
>>> S1 = ' ABC ' 
>>> type (S1)
 <type ' STR ' >
>>> s1 = s1.decode("utf-8")
>>> s1
u'abc'
>>> type(s1)
<type 'unicode'>

    The string can be the + operator, the effect is to stitching together two strings

>>> s1 = 'abc'
>>> s2 = 'def'
>>> s3 = s1 + s2
>>> s3
'abcdef'

  3, list the type of list

    The presence of one or more types of values, index list index starting from 0

>>> s1 = ['a',12,['b','c','d',18]]
>>> s1[1]
12
>>> s1[2][0]
'b'

  4, dict dictionary

    The method defined: by braces store data by key: value of this key-defined mapping relationship

    Each key-value pairs are separated by brackets

>>> d1 = {"name":'abc',"age":19}
>>> type(d1)
<type 'dict'>
D1 >>> [ ' name ' ]
 ' ABC ' 
# dictionary can be nested 
>>> D2 = { " name " : ' ABC ' , " Age " :. 19, " Hobby " : [ ' D ' , ' E ' ] }
 >>> D2 [ " Hobby " ] [. 1]     # to take a second hobby E 
' E '

  5, bool boolean

    Right or wrong judgment of things mainly used, generally not separately defined

>>> 1<2
True
>>> a = 277
>>> b = 277
A == b >>>     # equal sign is comparable value 
True
 >>> A IS b     # IS address comparison is 
False
“““
The values ​​are equal, id not necessarily equal
Case of equal id, value must be equal
”””

Third, the basic operators

  1, arithmetic

    +、-、*、/

    // divisible

    % Modulus

    ** Index

  2, the assignment operator

    +=、-=、*=、/=

>>> a = 1
= A +. 1 >>>     # similar. 1 + A = A 
>>> A
 2
>>> b = 5
B >>> -. 1 =     # similar to B = B -. 1 
>>> B
 . 4
>>> c = 2
C * = 2 >>>     # similar = C * 2 C 
>>> C
 . 4
>>> d = 2
D >>> / 2 =     # similar to D = D / 2 
>>> D
 . 1

  3, chain assignment

>>> x = y = z = 1
>>> print(x,y,z)
(1, 1, 1)

  4, cross assignment

>>> a = 1
>>> b = 2
>>> print(a,b)
( 1, 2 )
 # exchanging a, b values 
>>> a, b = B, A
 >>> Print (a, b)
(2, 1)

  5, decompression assignment

>>> s1 = [1,2,3,4,5]
>>> a,b,c,d,e = s1
>>> print(a,b,c,d,e)
( 1, 2, 3, 4, 5 )
 # when the number of the need to assign less than the length of the list, by default represents _ * 
>>> S2 = [1,2,3,4,5 ]
 >>> A, B, C, _ = * S2
 >>> Print (A, B, C)
 . 1 2. 3
>>> *_,e,d,f = s2
>>> print(e,d,f)
3 4 5
>>>

  6, the logic operation

    And, or, not

    and、or、not

    

 

Guess you like

Origin www.cnblogs.com/hexianshen/p/11783274.html