7.30 python basis of data types

7.30 python basis of data types

Numeric (int)

Integer

It can be numerically expressed. Age, number ......

definition

age = 18 # age = int(18) 
tel = 18855480890

use

Addition, subtraction, logical judgment ......

Float (float)

It represents increased body weight, salary ......

definition

height = 1.88 # height = float(height)
weight = 110
salary = 18.5

use

Addition, subtraction, logical judgment ......

String

Indicate the name, sex ......

definition

String is a string of characters are strung together, wrapped in single quotes, or three double quotation marks a string of characters. Note that: three characters in quotation marks can newline, and characters in single or double quotation marks can not.

name = 'wzh' # name = str(wzh)
gender = "male"
id1 = '''adc
def'''
id2 = """xyz
opq"""

use

+ Only string, and the logical comparison *

String concatenation, namely re-apply for a small space a copy of the two strings are then spliced. Instead you YY of the variable value in a small space inside the copy to another variable of a small space, and then stitching.

s = "nick's hand"
x = ' laji'
print(s+x) # nick's hand laji

print('nick '*3) # nick nick nick

print('nick' == 'shabi') # False
print('n' in 'nick') # True
print('ck' not in 'nick') # False
String index

Each string has an index from zero in ascending order from left to right, or right to left in descending order from -1

s = 'nick shabi'
     0123456789
    -10-9...-2-1 

With the index, you can follow the string value index

s = 'nick shabi'
s[0] # n
s[-2] # b
s[:] # nick shabi
s[:4] # nick 顾头不顾尾,末位k取到索引4
s[-5:] # shabi 
s[:8:2] # nc h 步长位2,按步长间隔取值
s[::-1] # ibahs kcin 倒序取值

List

If you now give us a demand, we need a hobby out of this man, seemingly with our current knowledge can not start. This time you have to change our strategy, we can for ways to store a person's hobby - list.

effect

Storing a plurality of values, such as a plurality of girlfriend, a plurality of hobbies.

definition

Any type of value are separated by a comma within [].

hobby = 'read'
hobby_list = [hobby, 'run', 'girl'] # 可存取任意类型的数据
print(hobby_list) # ['read', 'run', 'girl']

use

Similarly the string, but also a list of index values, index values ​​may be utilized

hobby_list = ['read', 'run', ['girl_name', 18, 'shanghai']]
hobby_list[0] # read
hobby_list[2][1] # 18

dictionary

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.

definition

{} 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.

user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
             'company_info': ['oldboy', 'shanghai', 50]}

use

Dictionary values ​​acquired value is a value obtained by taking the key value

user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
             'company_info': ['oldboy', 'shanghai', 50]}
user_info['name'] # nick
user_info['company_info'] # ['oldboy', 'shanghai', 50]
user_info['company_info'][1] # shanghai

Add key-value pairs in the dictionary

user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
             'company_info': ['oldboy', 'shanghai', 50]}
user_info['weight'] = 110
user_info['height'] = 188
for i in user_info.items():
    print(i)
#     ('name', 'nick')
#     ('gender', 'male')
#     ('age', 19)
#     ('company_info', ['oldboy', 'shanghai', 50])
#     ('weight', 110)
#     ('height', 188)

Boolean value (bool)

effect

Conditions for determination result

definition

True, False usually not directly quoted, required logic operation result obtained.

use

print(bool(0)) # False
print(bool('nick')) # True
print(bool(1 > 2)) # False
print(bool(1 == 1)) # True

Note: Python values of all data types comes Boolean value. So much data types only need to remember only 0, None, an empty, False Boolean value is False, the rest to True.

unzip

Decompression is actually unpack the more value out one-off

name_list = ['nick', 'egon', 'jason', ]
x, y, z = name_list
print(x, y, z) # nick egon jason

Sometimes we decompress values ​​may be we do not want, you can use an underscore

name_list = ['nick', 'egon', 'jason', 'tank']
x, y, z, a = name_list
print(x,y,z,a) # nick egon jason tank
x, _, z, _ = name_list 
print(x,z) # nick jason
x,*_,y = name_list 
print(x,y) # nick tank

Dictionary is also possible, but dictionaries decompression is key.

dic = {'name':'sss','age':123,'height':12312344}
for k,v in lis.items():    
    print(k,v)
# name sss
# age 123
# height 12312344

Interaction with the user

Accept the value entered by the user input, the user is interacting with the

id = 'wzh'
psw = '123'
id_info = input("请输入id:") # 与用户交互,引导用户输入id,psw
psw_info = input("请输入password:")
if id_info == id and psw_info == psw:
    print("欢迎登陆!")

Guess you like

Origin www.cnblogs.com/dadazunzhe/p/11271026.html