03 python grammar notes, user interaction, the output format, the basic data types, operators

03 python grammar notes, user interaction, the output format, the basic data types and operators

1. Types comment

Write head and the number of 'three quotes'

1561977467628

2. interaction with the user

1. What is the interaction with the user?

A programmer to enter information, a program executing the feedback to the programmer

2. Why do you want the program to interact?

To give people an alternative program, communicate with others

3. How to use?

  • python3 in

    converting all input information input by the user is 'string'

  • python2 in

    • User needs to specify the type of data input by itself. Otherwise error
    >>> name=input("输入: ")
    输入: shilingling
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'egon' is not defined
    
    >>> name=input("输入: ")
    输入: 'shilingling'
    >>> name,type(name)
    ('shilingling', <type 'str'>)
    
    >>> msg=input("输入: ")
    输入: [1,2,3]
    >>> msg,type(msg)
    ([1, 2, 3], <type 'list'>)
    • Using the same method used in python3 raw_input
    name=raw_input('>>>: ')
    >>>:shilingling
    >>> print(name)
    shilingling
    >>> print(name,type(name)
    >>> ('shilingling', <type 'str'>)
    
    name=raw_input('>>: ')
    >>>:23455323
    >>> print(name,type(name))
    >>> ('23455323', <type 'str'>)

3. Output Formatting

%s,%d。

% Is a placeholder, 's' and 'd' for receiving the traditional values.

name=input('姓名:>>>>')
age=input('年龄:>>>>')
print('my name is',name,'my age is ',age)

print('my name is %s,my age is %s'%(name,age))

'S' to receive any type of value; 'd' for receiving digital

print('my name is %s my age is %s'%('shilingling',[1,2,3])))

print('my name is %s my age is %d'%('shilingling,18'))

4. Data Type

4.1 Integer int
作用:记录年龄、登记、号码
定义:age=18
print(age,type(age))
使用场景:加减乘除
4.2 float float
作用:记录身高、体重、薪资等
定义:
salaries=2.3
print(salaries,type(salaries))
使用场景:加减乘除
4.3 String str

Role: represents the content descriptive. Such as name, name
definition: in quotation marks left to right, contains a character, quotes may be a single and double quotation marks, three marks

name='shilingling'
info='''
company
name:shilingling
addr:sq
'''

String splicing

mag='hello'
mag2='world'
print(mag+mag2)
#强调:
1.字符串之间可以相加
2.字符串相加是申请新的内存空间,效率不高
3.字符串乘以数字就是重复写多少次的字符串
print('hello'*10)
4.4 List list

Role: record multiple values, such as a group of people interested in a bunch of student names

Definition: [], the recording any type of data type, separated by a comma

l=[123,'shilingling',['d','g']]
print(l[2[1]])
4.5 Dictionary dict

It is an upgraded version of the list. Even though it can store multiple values, and values ​​is also convenient, but if I want to save myself down all the information, which need to use a dictionary.

作用:记录多个key:value值,优势是每一个value都对应一个key,而K就是描述这个值。
定义:在{内,用逗号分隔开key:value元素。其中value是字符串类型,key是不可变类型。
info={'name':'shilingling','sex':'male',age=18}
#为什么要用字典?字典能够明确表示数据的信息
emp_info={
    'name':'egon',
    'age':18,
    'sex':'male',
    'comapy_info':{
        'cname':'oldboy',
        'emp_count':200,
        'addr':'SH'
    }
}
#字典是无序的,因为不需要通过顺序来存取数据而是用的key
# 列表与字典存取数据选择:当存取的是相同的一类数据可以用列表,当存储的数据需要明确一一对应关系用字典

5. Boolean type bool

作用:用来作为判断的条件去用
定义:tag=True
#一般情况下我们是通过使用布尔值去做判断
print(age>=20)


#==比较的是值
print(x==y)
is 比较的是id 
ID相同,值一定相同。值相同,ID不一定相同
a=1
b=x
print(a is b)
#布尔值只有两个值,不会反复开辟新的内存空间存放

6. basic operators

6.1算数运算
    print(1+3)
    print(1*3)
    print(10/3)
    print(10//3)#取整
    print(10%3)#取余
    print(2**3)#上标
6.2比较运算:== != >= <=
6.3赋值运算
age=18
6.4增量赋值
age +=1
print(age)
6.5链式赋值
x=100
y=x
z=y
6.6交叉赋值
m=1000
n=2000

# temp=m
# m=n
# n=temp
n,m=m,n
print(m,n)
6.7 解压赋值
salaries=[11,22,33,44,55]
mon1,mon2,mon3,mon4,mon5=salaries
print(mon1,mon2,mon3,mon4,mon5)
# 等号右面包含的值的个数必须与等号左边变量名的个数一致
mon1,*_=salaries
print(*_)

7. The logic operation

7.1 and: connecting the left and right conditions, only when two conditions are true they finally to True. Note that T is capitalized.

print(age > 22 and age < 25 )

When about 7.2 or connect two conditions, whenever there is a condition is satisfied, the result is True

print(1 > 3 or 2 > 4 or 'x' == 'y' or 1==1)

7.3 not

print(not 1>3)

print(not (1 > 3 or 2 > 4 or 1==1))

Guess you like

Origin www.cnblogs.com/ZDQ1/p/11116831.html