2.Python Basics: Data Type Introduction

Source: Internet
Author: User
Tags shallow copy

There are about 3,000 languages in the world, of which Chinese is the most spoken language, English is the most widely used language, international communication, and generally speaking English. Computer Also, he has his own language, on the one hand, the computer is very powerful, can deal with a variety of complex procedures, on the other hand he is far less than human, that is the recognition of language.

2.1 Introduction to data types

int (integral type)

In 32-bit machines. The number of integers is 32 bits, and the value range is -2-31~2-31-1-2147483648-2147483647

In a 64-bit system, the integer digits are 64 bits and the value range is -2-63~2-63-1, i.e. -92232272036854775808~9223372036854775807

Long (integer)

Unlike the C language, Python's long integer does not refer to the positioning width, that is, Python does not limit the length of the well-known size, but in fact due to limited machine memory, we use a long integer value can not be infinite.

Note that since Python2.2, if an integer overflow occurs, Python will automatically convert the integer data to a long integer, so there is no serious consequence of not adding the letter L after the Long data.

Note: There's no longer a long in Python3, it's all int.

>>> A = 2**64

Type (a) #type () is a way to view data types

<type ' Long ' >

>>> type (b)

<type ' int ' >

i = 3

i = 6

Print (I.bit_length ())

Transformation:

int---> str:int (int) str all consist of numbers

STR---> BOOL 0 False not 0 True

Print (int (True))

Print (int (True)

2.2 String Type (str).

In Python, the quoted characters are considered to be strings!

>>>name = "Zhao" #双引号

>>>age = "#只要加引号就是字符串"

>>> Age2 = #int

>>>msg = ' My name is Alex, I am years old! ' #三引号

>>>

>>>hometown = ' Shangdong ' #单引号也可以

Note: There is no difference between single and double quotes, but this situation requires consideration of single-pair mates.

msg = "'

The weather is good today,

Want to go on a trip.

,,,

Print (msg)

string concatenation

Numbers can be subtraction and so on, grandfather wear can also be added, multiplied by the operation.

>>>name

' ALex Li '

>>>age

' 22 '

>>>

>>>name + Age #相加其实就是简单拼接

>>>name* #相乘其实就是复制多少次, in the stitching together

Alex Li Alex Li Alex Li Alex Li Alex Li Alex Li Alex Li Alex Li Alex Li Alex Li

Note that the concatenation of strings can only be a string between the two, and cannot be spliced with numbers or other types

STR slices

# s = ' python automation ops 21 '
# S1 = s[0]
# print (S1)
# s2 = s[2]
# Print (S2)
# s3 = S[-1]
# Print (S3)
#切片
# S1 = S[0:6] #顾头不顾尾
# print (S1)
Compensating slices:
# s3 = S[:5:2]
# Print (S3)
# S4 =s[:]
# Print (S4)
# s5 = s[-1:-5:-1] #:-1 Inverted must have reverse compensation
# Print (S5)
# s6 = s[-2:-5:-1]
# Print (S6)
# s7 = S[:6:2]
# Print (S7)
#s = ' Oldboy '
# # S1 = s.capitalize () #首字母大写, other letters lowercase
# # print (S1)

S2 = s.upper () #所有字母大写
Print (s2)
S3 = S.lower () #所有字母小写

Code = ' QAR ' = = Code:
Print (' Verification successful ')

S4 = S.swapcase () #大小写反转
Print (S4)

# s = ' Alex Wusir*oldboy3taibai ' #非字母的元素隔开每个单词首字母大写
# S5 = S.title ()
# Print (S5)

#s6 = S.center ()  #居中 length, default padding is None
# s6 = S.center (+, ' * ') #填充 *
# Print (S6)


# s7 = S.startswith (' ol ') #查看以什么开头
# EndsWith # ... End
# Print (S7)
# s7 = S.startswith (' B ', 3,5) #索引都是顾头不顾尾
# Print (S7)
#strip  Remove the first space, tab \ t, line break.
# S8 = S.strip ()
# Print (S8)
# NAME = input (' >>> '). Strip () write the project must add strip
# if Name = = ' Oldboy ':
# Print (' Login successful ')
#不仅仅是去除空格等, the specified element is removed until a non-specified element appears
# Lstrip () rstrip (0) Remove the left or right side only
# #split (str---> list) converted from string to list
# s = ' Oldboy,wusir,alex '
# # L = s.split ()
# # print (L)
# L2 = s.split (' o ') #三元素割出4个 4 change 5
# Print (L2)

Join Command (
Linked a new string (converts the list to str) with some connections
# s = ' Oldboy '
# S9 = ' + '. Join (s)
# Print (S9)

# s = ' hit Hammer fadasd Hammer sister Edwin van der Sar '
# s10 = s.replace (' Hammer ', ' Steel egg ')
# Print (S10)

#find index is not found by element  --1 Find cannot find an error
#s = ' Oldboy '
# ind = S.index (' d ')
# Print (IND)
# ind = s.find (' o ')
# Print (IND)
# ind = s.find (' A ')
# Print (IND)
# ind = s.find (' A ')
# print (int)

Formatted output format ()
# res= ' My name {} this year {} years old, hobby {}.format (' Egon ', ' Male ') '
# Print (RES)
# res=

Common methods: Len Count

# s = ' faafafsadasfafaafasf '
# print (len (s)) #查看有多少元素 length

List lists
# li = [111, ' Alex ', 222, ' Wusir ')
# # Print (li[1])
# print (Li[:3:2])
#l = [' Old boy ', ' Alex ', ' Wusir ', ' Taibai ']
#增
#append append a value to the back of the
# l.append (' gourd ')
# print (L)
# Insert Insert start from index
# L.insert (1, ' King Goddess ')
# print (L)

#迭代添加 Extend
# l.extend (' Alex ')
# l.extend (' 111 ')
# print (L)

#删 Pop Deletes a return value based on the index.
# l.pop (0)
# print (L)

#remove to delete the elements according to their
# l.remove (' Alex ')
# print (L)

#clear empty content does not delete the list
# l.clear ()
# print (L)

#del 1) Delete memory level list
# del L
# print (L)
#2) Follow the index to delete
# del L[:3]
# print (L)
#3) Follow the elements to delete
# l = [' Old boy ', ' Alex ', ' Wusir ', ' Taibai ')
#改
#按照索引改
# l[2] = ' Enrique '
# print (L)
#按照切片去改
# L[:2] = ' abc ' is added according to the smallest element

#查
# 1> by index
# 2> Follow the slice query
# for I in L:
# Print (i)

# LL = [1,2,3,1,2,1,1,4]
#其他方法
# count Counts
# Print (Ll.count (1))
#len Measuring length
# print (len (LL))
#通过元素找索引
# Print (Ll.index (4))

# L2 = [2,6,6,9,3,1,5,]
# #sort排序
# # L2.sort ()
# # Print (L2) from small to large
#
# # L2.sort (reverse=true)
# # Print (L2) from small to large
#
# #reverse Flashbacks
# L2.reverse ()
# Print (L2)

#列表的嵌套
# LL = [up, ' Alex ', ' Wusir ', [' Oldboy ', ' Ritian ', 99]]
# Print (LL)
# Ll[2] = ' ALEX '
# Print (LL)
# Print (Ll[2].upper ())
# Ll[2] = Ll[2].upper ()
# Print (LL)

Dict Dictionary
# dict Binary lookup: The dictionary key is unique and must be an immutable data type
# Immutable data type (hash): str bool Tuple int
# value is any data type.
# variable data type: Dict list set
# The data type of the container class list tuple dict set
#字典: Store data, relational data, fast query speed (two-point lookup)
#dic = {' name ': ' Taibai ', ' age ': ' + ', ' hobby ': ' Gril ',}
#增删改查
#增
# dic[' High ' = if it has a overwrite (change), no add
#print (DIC) #3. Dictionary unordered before version 6
# Dic.setdefault (' high ', +) #有则不变 none added
# Print (DIC)
#删
#dic. Pop (' name ')
#print (Dic.pop (' name ')) #返回值 the corresponding value

#clear删除内容
# dic.clear ()
# Print (DIC)
#删除内存
# del
# del DIC
# Print (Dic.popitem ()) #随即删除, return value
# Print (DIC)
# Print (Dic.pop (' name1 ', ' no this key SB '))
#print (DIC)

#改
#dic2. Update (DIC) #将dic的值对应添加到dic2中

#查
#print (dic[' name ')
# Print (Dic.get (' name1 '))
# Print (Dic.get (' name2 ', ' no this KEY,SB '))
 #len 
# print (len (DIC)) # Test dictionary length

#fromkeys Create a dictionary iterate
# dic1 = Dic.fromkeys (' abc ', ' Zhangsan ')
# Print (DIC1)
# dic2 = Dic.fromkeys ([+], ' Lisi ')
# Print (dic2)
# dic3 = Dict.fromkeys (' abc ', [])
# Print (DIC3)
# dic3[' A '].append (' boss also ')
# Print (dic3)

#dic = {' name ': ' Taibai ', ' age ': + ' hobby ': ' Girl ',}
# dic[' name '] = ' Ritian '
# print (DIC)
# Print (Dic.pop (' name '))
# Print (Dic.pop (' name1 ', ' Meiyousb '))
# dic.clear ()
# print (DIC)
# Print (Dic.popitem ())
# Print (DIC)
# dic[' name '] = ' Laonanhai '
# print (DIC)
# Print ( Dic.get (' name '))
# for I in Dic.items ():
# Print (i)
# for K,v in Dic.items ():
# Print (k,v)
# dic = {
# ' name_list ': [' B ', ' curate ', ' People handsome ', ' Kitty '],
# ' old boy ': {
# ' name ': ' Old boy ', '
# ' age ': 46,
# ' sex ': ' Ladyboy ',
#},
#}
# Dic.setdefault (' Cavalry ')
# print (DIC)


Ganso
# can only be found and deleted changes
# Tu = (11,2,true,[2,3,4], ' Alex ')
# # for I in Tu:
# # Print (i)
# print (tu[1])
# print (Tu[:3:2])
# # Print (Tu.index (True))
# Print (Tu.count (2))
# Print (Len (TU))
# tu[-2].append (666)
# Print (TU)

Collection:
Unordered, non-repeating data type. The elements inside it must be hashed,
But the collection itself is non-hashed
#1: Relationship test intersection and set of set differences for subsets
#2: de-weight (list of Go-heavy)
# SETL1 = {1, ' Alex ', False. (a)}
# LL = [1,1,1,1,2,2,6,5,]
# L2 =list (SETL1)
# Print (L2)
#增 Add single incremental update iteration increment
#删
#remove Delete an element
# Set1 = {1,2,3,4,5}
# Set2 = {4,5,6,7,8}
#交集:
# Print (Set1 & Set2)
# #并集
# #print (Set1 | set2)
# Print (Set1.union (Set2))
# difference Set
# Print (Set1-set2)
#反交集
# print (set1 ^ set2)
#子集
#print (Set1 < Set2) (Super Set)
#动集合
# s = Frozenset (' Barry ')
# Print (S,type (s))
#数据类型补充
#range customizable List of numbers
# for I in range (10):
# Print (i)
# L1 = [' Alex ', ' Wusir ', ' Taibai ', ' Barry ', ' old boy ']
# # del L1 [1::2]
# # print (L1)
# for I in range (len (L1) -1,-1,-1)
# if I% w = = 1:
# del Li[i]
# #再循环一个列表时, do not delete the list of actions (change the number of lists)
#再循环字典时, do not delete the dictionary (do not change the size of the dictionary)
# dic = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 ', ' U ': 666}
# L1 = []
# for I in DIC:
# if ' K ' in I:
# L1.append[i]
# for I in LL:
# del Dic[i]
# Print[dic]
#tu tuples have only one element and no comma separated, then his data type
#与该元素一致

Small data pool copy only int and str
id = = is
# a = ' Alex '
# b = ' Alex '
# print (A = = b) #数值
# print (A is B) #内存地址
# Python has a concept of small data pools
# INT-5 to 256 the same thing all points to a memory address (space saving)
#str: s = ' a ' * within 20 is the same memory address more than 20 is not
#只要字符串含有非字母元素, it's not a memory address.

#深浅copy
#赋值运算, they are common to a list
# a = [a]
# b = A
# a.append (666) n/
# print (A, B)
# Shallow Copy the first layer is different after the same
# L1 = [+]
# L2 =l1.copy ()
# l1.append (666)
#深copy totally different.
# import Copy
# LL = [1,2,3,[22,33]]

#编码
#python 3x
#1. Binary in different encodings cannot recognize each other
#2. python3x STR internal encoding mode is Unicode
#但是, for file storage, and transport cannot be Unicode (consuming too much memory)
# bytes
# bytes Type: Internal encoding is non-Unicode (utf-8 GBK)

# s = ' China '
# Print (S,type (s))
#转化
s = ' China '
S2 = S.encode (' GBK ') #str--bytes
Print (s2)
SS = S2.decode (' GBK ') #bytes-->str
Print (ss)
Homework after class

Shopping Cart Assignments
Money = input (' Please enter your money: ')
Shopping_car = [] #购物车列表
If Money.isdigit (): #判断是否为int
money = Int (money)
Else
Exit (' Please enter the correct amount ')
welcome_msg = ' Welcome ' #msg
Print (WELCOME_MSG)
Goods_list = [(' Computer ', 1999),
(' mouse ', 10),
(' yacht ', 20),
(' Beauty ', 998)
] #产品列表
Flag = 0 #标志
While flag was not True:
Print (' Commodity table: '. Center (50, '-'))
For item in Enumerate (goods_list):
index = item[0]
P_name = item[1][0]
P_price = item[1][1]
Print (Index,p_name,p_price)
Choice = input (' Input item number >>> ')
If Choice.isdigit ():
Choice = Int (choice) #限定输入的为数字
If choice < Len (goods_list): #选择序号要在范围内
P_item = Goods_list[choice]
If p_item[1] <= money:
Shopping_car.append (P_item)
Money-= p_item[1] #减去商品金额
Print (' already purchased ')
For item in Shopping_car: #循环一次打印购物车
Print (item)
Print (' Your balance is [%s] ' money ') #输出余额
Else
Print (' No this item ')
elif Choice = = ' Q ':
Print (' Already finished shopping: '. Center (40, '-'))
For item in Shopping_car:
Print (item)
Print (' End ')
Print (' remaining [%s] '% money)
Flag = True
elif Choice = = ' C ':
Print (' Please select item Serial purchase ')
For item in Shopping_car:
Print (item)
Print (' Amount is%d ')
Else
Print (' wrong input, please try Again ')



2.Python Basics: Data Type Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.