Python Data Type & amp; character encoding, python Data Type

Source: Internet
Author: User

Python Data Type & character encoding, python Data Type

I. Number

Integer int

x=10 #x=int(10)print(id(x),type(x),x)

Float

salary=3.1 #salary=float(3.1)print(id(salary),type(salary),salary)

Ii. String

name='hantao'
# Value by index (forward and reverse): print (name [0], type (name [0]) print (name [-1])
# Slice (regardless of tail, step size) print (name []) print (name []) print (name [5:-1])
# Lenprint (len (name ))
# Member operations in and not inmsg = 'hellochanta' print ('hanta' in msg)
# Remove blank strippasswd = 'hantao123 'print (passwd. strip () passwd1 =' ***** hantao *** 'print (passwd1.strip ('*'))
print(passwd.lstrip())
print(passwd.rstrip())
# Split splituser = 'hantao: liangchaowei: liudehua' print (user. split (':'))
# Determine the start and end of msg = 'hantao _ is_cool 'print (msg. startswith ('Han') print (msg. endswith ('cool '))
# Replace msg = 'Alex say I have a tesla, my name is alex 'print (msg. replace ('Alex ', 'SB') with replace '))
# Format formatting print ('My name is % s, my age is % s' % ('hantao', 18) print ('My name is {}, my age is {}'. format ('hantao', 18) print ('My name is {0}, my age is {1 }'. format ('hantao', 18) print ('My name is {a}, my age is {B }'. format (a = 'hantao', B = 18 ))
# Find msg = 'Hello world' print (msg. find ('hel') print (msg. index ('O') print (msg. count ('l', 0, 4 ))
# Join print (':'. join (['hanta', 'liangchaowei', 'liudehua'])
# Fill print ('info '. center (30, '=') print ('info '. must ust (30, '=') print ('info '. ljust (30, '=') print ('info '. zfill (30 ))
# Is determines the number num1 = B '4' # bytesnum2 = u'4' # unicode, in python3, The unicodenum3 = '4' # Chinese numeric num4 = 'iv' # Rome digital print (num1.isdigit () print (num2.isdigit () print (num3.isdigit ()) print (num4.isdigit ())

Iv. List

# Index-based access value (forward access + reverse access): print (my_familly [2]) print (my_familly [-1]) can be saved.
# Slice (regardless of tail, step size) print (my_familly [0: 3]) print (my_familly [0: 5]) print (my_familly [5:-1])
# Print (len (my_familly ))
# Member operations in and not inprint ('nep' in my_familly)
# Append my_familly.append ('corgi') print (my_familly)
# Delete goods = ['apple', 'Banana ', 'pear'] del goods [-1] print (goods. remove ('apple') # print (goods. pop (1) # Delete by index. Deletion starts at the end by default. The deleted Value print (goods) is returned)
# Other goods = ['apple', 'Banana ', 'pear', 'Banana '] # goods. insert (0, 'SB ') # goods. extend (['meat', 'eggs ']) # print (goods. count ('bana') # goods. reverse () # reverse # l = [2, 4, 6, 1,-3] # l. sort (reverse = True) # print (l) print (goods)

5. yuanzu

Ages = (, 34,) # by index value (Forward fetch + reverse fetch): Only print (ages [1]) can be taken. # Slice (skip the end of head, step size) print (ages [0: 2]) # length print (len (ages) # member operations in and not inprint (23 in ages)
Ages = (, 34,) print (ages. index (12) # search index print (ages. count (23) # search count

Small exercises

'''Simple shopping cart, the requirements are as follows: to print the product details, the user enters the product name and the number of purchases, the product name, price, the number of purchases added to the shopping list, if the input is null or other illegal input, the user must re-enter '''msg _ dic = {'apple': 10, 'Tesla ': 100000, 'mac': 3000, 'lenovo ': 30000, 'chicken': 10,} l = [] while True: for key in msg_dic: print (key, msg_dic [key]) goods = input ('input your good :'). strip () if goods not in msg_dic: continue while True: counts = input ('input the number :'). strip () if counts. isdigit (): break l. append (goods, msg_dic [goods], counts) print (l)

6. Dictionary

Info = {'name': 'egon', 'age': 18, 'sex': 'male'} # access by key: printable print (info ['name']) info ['jobbies'] = ['eid', 'drink', 'Sleep '] print (info) # lenprint (len (info) # Delete print (info. pop ('na1me', None) # key keys (), value values (), key value pair items () print (info. keys () print (info. values () print (info. items () for item in info. items (): print (item)
Info = {'name': 'egon', 'age': 18, 'sex': 'male'} print (info. get ('name') # retrieve valueprint (info. get ('na1me') # returns Noneprint (info. popitem () # random Delete t = ['23', '23', '45', '45'] ,*_, d = t # compression assignment print (a, d) for a, B in info. items (): print (a, B) info_new = {'A': 1, 'B': 3, 'name': 'hanta'} info. update (info_new) # update print (info) dic = {}. fromkeys (['name', 'age', 'jobbies']) # print (dic) print (info. setdefault ('age', '20 '))#

VII. Set

# Function: deduplicate, relational operation s = {1, 2, 'A'} # s = set ({1, 2, 'A'}) s1 = {1, 2, 3} s2 = {2, 3, 4} # | Union s1 | s2s1. union (s2) # & intersection s1 & s2s1. intersection (s2) #-differential set s1-s2s1.difference (s2) # ^ symmetric difference set s1 ^ s2s1. into ric_difference (s2) # parent set s1> = s2s1. issuperset (s2) # subset s1 <= s2s1. issubset (s2)
S1 = {1, 2, 3, 4, 'A'} # print (s1.pop () # randomly Delete, return result # print (s1.remove (1) # Delete element, no return value, element does not exist. Error # s1.discard (1) # Delete element, no return value, element does not exist, no error s2 = {5, 6} print (s1.isdisjoint (s2) # s1 and s2 have no intersection, returns True.
# The following list is available. The list element is non-hash type and deduplication is used to obtain a new list. The new list must be in the original order of the list l = [{'name ': 'egon', 'age': 18, 'sex': 'male'}, {'name': 'Alex ', 'age': 73, 'sex ': 'male'}, {'name': 'egon', 'age': 20, 'sex': 'female '}, {'name': 'egon ', 'age': 18, 'sex': 'male'}, {'name': 'egon', 'age': 18, 'sex': 'male'},] l1 = [] for I in l: if I not in l1: l1.append (I) print (l1)

 

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.