Variable
Container
The role of tag data with variable names
Character encoding
bits = bit = 8bits
1 bits is the smallest representation unit in a computer
1 bytes is the smallest storage unit in a computer
8bits = 1Byte = 1 bytes
1024Bytes = 1KB = 1 bytes
1024KB = 1MB = 1 million byte = 1 million character = 1 MB
1024MB = 1GB = One HD short film, 1080p 20 minutes,
1024GB = 1TB
1024TB = 1PB
ASCII 256 Each character occupies 8 bits
GB2312 1980 67XX
GBK1.0 1995 21000
GB18030 2000 27000
Unicode Universal Code
utf-32 = 4 bytes
utf-16 = 2 bytes
Utf-8 = 8bits Variable length encoding
English one byte, all Chinese 3 bytes
Europe 2 bytes
Note: #或是 ""
Single and double quotes do not have any difference, only for Single-line
Three single or double quotation marks for multiple lines of string or multiple lines of Comment.
Data type
Str
Number Type
int integral type
Long integer type
float Float Type
Plural
BOOL Boolean value
True True
False false
Character
Character Common function string is%s; integer%d; floating-point number%f
Remove whitespace
Name = ' Alex Li '
Print (name)
Print (name.strip ()) #把一个字符串中的空行脱掉
Segmentation
Print (name.split ()) #把一个字符串 split into lists by space
Length
Len (name) # length judgment
Index
Slice
Print (name1.index ("h")) Index
Print (name1[0:8]) #切片
Reverse print (name1[-6:]) #切片 default order from left to right Gu Tou disregard tail
Skip cut print (name1[0::2]) #2 indicate step size
List
Name = [' ', ' ', ']
Print (type (NAMES))
Print (name[-1]) extracts a slice of data from the list
Print (name.index ("person Name")) #返回元素的索引下标 for finding in many data
Print (name.count ("person Name")) #统计
Name.append ("bald") #追加
Specify location add (insert) # Insert Insert
Name.insert (4, "chen Tao") write in front of the position, write the insertion behind
Name.pop (4) #删除 to position the index to the default deletion of the last
Name.remove ("li Zhi") #删除第一个李智 elements
Del name[1] # Delete the value at the first position
Change name[index] = NewValue #新的值
Names[names.index ("liar")] = "big liar"
Break jumps out of this layer loop
Continue #跳出本次循环
For and while loops, the other else condition behind, is when the loop ends normally, only after the else condition is gone!
Job 1
Jump out of the multilayer loop, three-layer loop, innermost layer, jump directly out of 3 layers, exit ()
Code:
#! Usr/bin/env python
# _*_ Coding:utf-8 _*_
# jump out of the multilayer loop, Three-layer loop, the innermost layer, jump directly out of 3 layers!!!
Count = 0
running = True #定义一个变量, assigned value is true!
While Running: # first layer, while loop
Print ("number---->", Count) #打印值
Count +=1 #每打印一次自加1
if count = = 5: #当值等于5时 for the second loop
For I in range (5):
Print ("i---->", i) #输出i的值
Else: #if i = = 4: When the value of I is 4 o'clock, the for loop ends, the next layer of loop
For j in range (5): #进行第三层循环
Print ("j---->", j) #打印j的值
Else: #for循环结束时, Print
Print ("exit")
running = False #退出整个循环!
Job 2
Shopping Cart Procedure
Shopping Requirements:
Python shopping.py
Input your salary:5000
You can buy the following things:
1. iphone 5800
2. Coffee 30
3. Book 50
4. Condom 90
>>: 1
Can't afford to print, how much is the difference?
>>:2
Put in the shopping cart, deduct the money while printing the balance ... 4970
>>:3
...
>>:4
>>:q
You bought the following things
Coffee 30
book.. 333
How many first 3000 do you have?
Bye.
The code is as Follows:
#! Usr/bin/env python
# _*_ Coding:utf-8 _*_
good_list=[["iphone", 5800],["bike", 1000],["coffee", 30],["book", 35],["pen", 3] # Product List
shopping_car=[] #购物车
Print ("welcome!")
salary = int (input ("please input your Salary:")) #输入工资
running = True
While Running:
Print ("can buy something in the List:") #打印商品列表
Print ("--------------------------------")
For I in range (len (good_list)):
Good_name = good_list[i][0]
Good_price = good_list[i][1]
Print (i+1,good_name,good_price)
Print ("--------------------------------")
User_choice = input ("what is want to buy?" Check=c;quit=q) "" "please input serial Number:") #输入选择
If User_choice.isdigit (): #判断是否是数字isdigit on-line search judgment
user_choice = int (user_choice) -1# is a number.
If User_choice < len (good_list): #先判断, the number entered is less than the item number
Good_choice = good_list[user_choice] #客户选择的商品
If good_choice[1] <= salary: # Judging the relationship between commodity prices and wages
Shopping_car.append (good_choice) #将商品加入购物车
salary-= good_choice[1] #买完东西之后的余额
For I in range (len (shopping_car)):
Print ("you buy:%s"%shopping_car[i]) #打印购物车中的东西
Print ("balance Money Is:%s"%salary) #打印余额
Print ("--------------------------------")
Else
Print ("!!!! You are not having so money!!!! ") #钱不够, can't buy!
Print ("you balance Money Is:%s"%salary) #提示有多少钱!
If Len (shopping_car) > 0: #判断是否买了东西, buy it and print it without buying it.
Print (shopping_car)
Print ("--------------------------------")
#break #没钱买了还扯蛋!!!
Else
Print ("we are not having this good,please choice Again!") #输入的数字大于序列号, Print without this product
elif User_choice = = "q": #输入 "q" exit, Print the purchased items and balance
For item in Shopping_car:
Print (item)
Print ("you Balance Money Is:%s"%salary)
Print ("--------------------------------")
running = False
elif User_choice = = "c": # Enter "c" to choose to continue shopping
Print ("you Balance Money Is:%s"%salary)
Print ("--------------------------------")
Else
Print ("incorrect input,please input Again!!!") #错误的输入
Python full stack Python foundation (ii) variable character list