Exchanging user basic data types basic operators formatted output

Users exchange

First, users exchange

1.1 Definitions: The user is the person to exchange computer input / input data, the computer print / output.

1.2 Purpose: To allow computers to communicate with users like human beings

>>> input("your name:")#输入数据
your name:sean
'sean'
>>> print("hello world !")#输出结果
hello world !
The difference between 1.3 python2 and python3

python2 input: you must declare the type of input

>>> input(">>:")
 >>:sean# 没有告知变量值得类型,引发错误
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 1, in <module>
  NameError: name 'sean' is not defined
>>> input(">>:")
>>:"sean"#输入的是字符串'str'
   'sean'#可以正常的输出
>>> input(">>:")
>>:1#输入int
 1#输入int
>>> input(">>:")
>>:[1,2] #输入list
[1, 2]输出list
>>>

python3 receiving user input, the user input regardless of what type, must eventually return string "str"

>>> name = input("dfhfghgf :  ")
dfhfghgf :  David #输入内容
>>> print(name)
David
>>> print(type(name))
<class 'str'>#字符串



>>> name = input("dfhfghgf :  ")
dfhfghgf :  1234 #输入内容
>>> print(name)
1234
>>> print(type(name))
<class 'str'> #字符串

python 2 can be used with the effect of a 3 inptut raw_input python

Second, the basic data types

1.int (integer) such as age, class size, etc.

python2

In this interval [-24XXXXXXXX, 24XXXXXXXX]: variable type: int

If not in this range type: long

python3: type:int

>>> i = 29459696979939695959696#
>>> print(type(i))
<class 'int'>

2.float (float) as height and weight

>>> i = 25566.2556  #带小数点
>>> print(type(i))
<class 'float'>

3.str (string)

Definition: There are some descriptive information such as personal preferences, profiles, etc.

Quoted string of no distinction can '' '' But not mix can not "football '

If the string need quotes, must be fitted

For example: "ab 'cds'"

# 单引号

# s1 = 'sean'
# print(s1)

#  双引号

# s2 = "sean"
# print(s2)

# 三引号

# s3 = '''sean'''
#
# s4 = """sean"""
#
# print(s3)
# print(s4)

# s5 = 'dadada"dada"'
>>> i = "David"
>>> print(type(i))
<class 'str'>#字符串

python2

The nature str actually has a bit sequence of eight bits

>>> s1 = 'sean'
    >>> type(s1)
    <type 'str'>
    >>> s1 = s1.decode("utf-8")
    >>> type(s1)
    <type 'unicode'>
    >>> s1
    u'sean'

python3

In fact, the essence str unicode sequence

>>> ss1 = 'sean'
    >>>
    >>> type(ss1)
    <class 'str'>
    >>>
    >>> ss1 = ss1.encode('utf-8')
    >>> ss1
    b'sean'
    >>>
    >>> type(ss1)
    <class 'bytes'>

1024G = 1T

1024M = 1G

1024KB = 1M

1024B = 1KB

1B = 8bit

The string can be spliced ​​open up a new memory space, you will value the stitched into them

>>> s1 = "hello"
>>> s2 = "world"
>>> print(s1+s2) #拼接处理
helloworld

4. list (list)

Definition: deposit one or more different types of values, each value with a comma (,) separated

In the count is zero-based programming

>>> t = ["abcf","大象",['read','鲜花',12344],12334]
>>> print(type(t))
<class 'list'>#列表
>>> print(t)
['abcf', '大象', ['read', '鲜花', 12344], 12334]

The method takes certain values ​​from which the following operations

>>> t = ["abcf","大象",['read','鲜花',12344],12334]
>>> print(t[2][1])
鲜花

Type (dict) 5 dictionary

The method defined: by braces store data by key: value of this mapping relation to define a key value,

Each pair of keys separated by commas

>>> s1 = {"name": "Davis","age": 20, "hobby":["篮球","football","running"],"id":1235}
>>> print(type(s1))
<class 'dict'>#字典
>>> print(s1["age"])
20
>>> print(s1["hobby"])
['篮球', 'football', 'running']

Boolean

The main judge right and wrong things

General does not define a separate boolean

False True

>>> print(1<2)
True#布尔类型
>>> a = 2
>>> b = 2
>>> print(a==b)
True
The above represents the equivalent value ==
>>> a = 2
>>> b = 2
>>> print(a is b)
True
Above "is" meaning refer to the same id

Third, formatted output

Definition: the certain content inside a string after the replacement and then the output is formatted.

We used:

1.% s: can receive any type of variable;

2.% d: receiving a digital only type, python 3 is not used

  1. .format

  2. f-string (3.6 python only after a characteristic)

    For example as follows:

    name = input(" :")
    month = input(':')
    Phone_Bill = input(" :")
    Balance = input(" :")
    print("你好,亲爱的%s,你%s月份的话费是%s元,余额是%s元"%(name,month,Phone_Bill,Balance))#针对的%s
    name = input(" :")
    month = input(':')
    Phone_Bill = input(" :")
    Balance = input(" :")
    # print("你好,亲爱的%s,你%s月份的话费是%s元,余额是%s元"%(name,month,Phone_Bill,Balance))
    print("你好,亲爱的%s,你%s月份的话费是%s元,余额是%d元"%(name,month,Phone_Bill,23))#针对的是%d
    name = input(" :")
    month = input(':')
    Phone_Bill = input(" :")
    Balance = input(" :")
    # print("你好,亲爱的%s,你%s月份的话费是%s元,余额是%s元"%(name,month,Phone_Bill,Balance))
    # print("你好,亲爱的%s,你%s月份的话费是%s元,余额是%d元"%(name,month,Phone_Bill,23))
    print("你好,亲爱的{},你{}月份的话费是{}元,余额是{}元".format(name,month,Phone_Bill,23))# 针对.format的使用
    
    print("你好,亲爱的{name},你{month}月份的话费是{Phone_Bill}元,余额是{Balance}元".format(name =month,month = name,Phone_Bill = Phone_Bill,Balance = 23))# .format的一种 用法
     name = input(" :")
    month = input(':')
    Phone_Bill = input(" :")
    Balance = input(" :")
    print(f"你好,亲爱的{name},你{month}月份的话费是{Phone_Bill}元,余额是{Balance}元")#f的使用方法

    Four basic operators

    4.1 Arithmetic operators

    suansh

    a = 9
    b = 2
    print(a+b)
    print(a-b)
    print(a*b)
    print(a/b)
    print(a//b)
    print(a%b)
    print(a**b)

    4.2 Comparison Operators

    Comparison operation for comparing the two values ​​and returns the boolean value True or

    False

    alse

a = 9
b = 2
print(a==b)
print(a!= b)
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
4.3 Assignment Operators

4.3.1 Augmented assignment

2222

x = 10
# x += 1
# print(x)
# x -= 1
# print(x)
# x *= 2
# # print(x)
# x %= 3
# print(x)
x **= 3
print(x)

4.3.2 Chain assignment

We want a value is assigned to multiple variables

So to operate

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

4.3.3 Cross assignment

2 allows the exchange value of a variable over

a = 1
b = 2
a,b =b,a
print(a,b)

4.3.4 decompression assignment

l1 = [1,2,3,4]#相当于一个压缩包
a,b,c,d = l1
print(a,b,c,d) 

Note: The number of variables must be the name of the equal sign on the left and right contain the same number of values, otherwise an error.

** If we want to just take the head and tail, can be used to match ** * _

l1 = [1,2,3,4,5,6,7]
#*_,a,b,c,d = l1
a,b,c,d,*_ = l1 # 用*_ 来匹配的
print(a,b,c,d)

Guess you like

Origin www.cnblogs.com/bs2019/p/11791715.html