table of Contents
1. List role of digital type, string type, list, dictionary definition and usage mode respectively
Digital Type:
Plastic
Role: used to describe the numbers, such as phone number, age, student number, ID number, etc.
Defined method:
age=18 #age=int(18)
Instructions:
Basic operations: addition, subtraction, '+', '-', '*', '/', rounding: '//', take the remainder: '%', Mi: '**', etc.
a=2
b=3
print(a+b,a-b,a*b,a/b,a%b,a//b,a**b)
Cast data types:
age=int(18.9)
age=int('25')
Float
Action: used to describe the height, weight, salary, etc.
Defined method:
weight=140.5 #weight=float(140.5)
height=180.5 #height=float(180.5)
Instructions:
Basic operations: addition, subtraction: '+', '-', '*', '/', rounding: '//', take the remainder: '%', Mi '**' etc.
a=5.0
b=2.5
print(a+b,a-b,a*b,a/b,a%b,a//b,a**b)
Cast data types:
weight=float(140)
height=float(180)
String type:
Role: used to describe the name, address, appearance, etc.
Defined method:
name = 'ZhangSan'
hobby="music"
class='''math'''
Instructions:
'+': String concatenation string, '*': the same string plurality of splice
s1=’a‘
s2=’b‘
print (s1 + s2, s1 * 2) # output is 'ab' and 'aa'
Slice: care regardless tail
s1='abcdefgh'
print(s1[0:5:2],s1[-1:1:-1])
The output is: ace hgfedc
.join (): An somehow spliced list
msg="|".join(['a','b','c','d'])
print(msg)
Output: a | b | c | d
List:
Action: to store a plurality of values, each value may be any type
Defined by:
with [] storage, a plurality of spaced elements (of any data type) with a comma
msg = [ 'wxx', 19, [ 'Alex', 150]] # is equivalent to msg = list [ 'wxx', 19, [ 'Alex', 150]]
Instructions
Index values \ change value
print (msg [0], msg [2], msg [2] [1], msg [-1]) # Value
'' 'Output:
WXX [' Alex ', 150] 150 [' Alex ', 150]‘’‘
msg [0] = 'xiaoming' # change value
print(msg,msg[0])
'' 'Output:
[' Xiaoming ',. 19, [' Alex ', 150]] Xiaoming‘’‘
Slice: care regardless tail
print(msg[0:2],msg[-1:-3:-1])
'' 'Output:
[' Xiaoming ',. 19] [[' Alex ', 150],. 19]‘’‘
.append (): Add
msg.append('sb')
print(msg)
Output Results: [ 'wxx', 19, [ 'Alex', 150], 'sb']
dictionary:
Action: The key: value stored plurality of key-value pairs, each value corresponds to a dedicated key
key: typically a string, having a descriptive sense
value: specific value, the value of any data type
Defined method:
msg={'name':'alex','age':18,'height':185.5}#等同于msg=dic{'name':'alex','age':18,'height':185.5}
Instructions
Press the key values \ change the value \ add value
print (msg [ 'name']) # Value
Output: alex
msg['name']='xiaohong'#改值
print(msg,msg['name'])
Output: { 'name': 'xiaohong', 'age': 18, 'height': 185.5} xiaohong
msg['hobly']='music'
print(msg)
Output: { 'name': 'xiaohong', 'age': 18, 'height': 185.5, 'hobly': 'music'}