Python Basics Essay

Source: Internet
Author: User

One: Scope

For a variable's scope, the variable can be used as long as it exists in memory.

Two: ternary operation

Name = value 1 if condition else value 2

If the condition is true: result = value 1

If the condition is false: result = value 2

Way One:

Name = ' CCC '
If
1==1:
Name = ' SB '
else
:
Name = ' SB '
Way two:
Name = ' sb ' if 1==1 Else ' 2b '

Instance:

Name = Raw_input (' Your name: ')
Reslut = ' sb ' if name = = ' Alex ' Else ' nice '

Print Reslut

Three: Binary

All things in Python are objects. Objects are created based on a class.

See what the list does in this class

Dir (type name)

Help (type name)

Show the details of the content

Help (type name. function name)

View the type of an object by type ()

Instance:

Print dir (list)

Print Help (list)

print Help (list.append)

The methods in the class are divided into underlined and not underlined.

Underlined is the built-in method, there may be a variety of execution methods;

Non-underlined is a non-built method, with only one execution method, and an object. method

For the understanding of the above statement

One: Number

i = 10
i1 = Int (10)
i2 = Int ("Ten", 2)

The following numbers represent the binary, and the preceding values are the corresponding value in the binary.

Here are some of the methods in int

1 #比较大小 2  3 age = 4 Print age.__cmp__ (+) 5 print age.__cmp__ () 6 print age.__cmp__ (+) 7 print CMP (18,19) 8 #取绝对 A value of 9 A = -910 print a.__abs__ () one #相加12 B = 913 print b.__add__ (8) #强制生成一个元组15 c = 716 Print c.__coerce__ (6) #取商和余数, calculated Page Use D = 9819 print d.__divmod__ (3) #取商21 e = 922 print e.__div__ (+) at #转换为浮点类型24 f = 725 F1 = f.__float__ () the PRI NT type (F1) #哈希值用于在字典中快速找到他的键

Two: string (str)

1 str_1 = "Alex" 2 str_2 = str ("Alex") 3 #首字母变大写 4  5 Print str_2.capitalize () 6  7 #将内容显示在中间; total length is 20; fill 8 with *  

###############################################################

Encoded Conversion Gbk==>unicode==>utf-8

Encode: Encoding

decode: Decoding

1 >>> ' I '  2  

Code interpretation

###############################################################

 1 #以什么结尾; is in return true 2 str_1 = "Alex" 3 Print Str_1.endswith (' x ') 4 #将tab转换成空格, default one tab to 8 spaces; Convert to 1 5 name = ' Ale x ' 6 Print Name.expandtabs () 7 print name.expandtabs (1) 8 #寻找子序列的位置;-1 means No 9 print str_1.find (' e ') print str_1.find (' Le ') 1 1 Print str_1.find (' s ') #找到第一个13 str2 = "Alexa" Print str2.find (' a ') #字符串格式化16 # #按照下标 (ordered) page name = "I am {0},age {1 } "Print Name.format (" CGT "," 23 "), #使用列表的方式20 name_list = [" CGT ", 24]21 print Name.format (*name_list) # #按照名称 (unordered) Name = "I am {aa},age {bb}" Print Name.format (aa= "CGT", bb=24) print Name.format (bb=24,aa= "CGT") # #使用字典的方式27 Name_di ct = {' AA ': ' CGT ', ' BB ': 24}28 print Name.format (**name_dict) #使用index来查找; error not found, and find is return -130 name = "Alex" to print Name.index (' a ') #是否是数字和字母33 print name.isalnum () #是否是字母35 print name.isalpha () #是否是数字37 print name.isalnum () 38 # Whether it is a lowercase print name.islower () #是否是空格41 print name.isspace () #是不是标题istitle (the title is in the first letter of the word) Print name.istitle () 44 #设置为标题的格式title45 print name.title () #是不是大写isupper47 print name.isupper () #连接join49 print ' __ '. Join (name) #内容左对齐ljust (width, padding) print name.ljust (20, ' * ') 52 # Content right-aligned rjust (width, padding) name.ljust print name.lower (58, ' & '), print name.upper (), #变成小写lower55 #大小写交换swapcase59 name = "AleX" Print name.swapcase () #移除左边的空白lstrip62 print Name.lstrip () #分隔partition (front, middle, rear) 64 name = "Hello CGT", "Name.partition" ("ll"), #替换replace (old,new), the name = ' Alex ' of the print Name.replace (' A ', ' B ') #从右边  Start looking for RFIND70 print name.rfind (' e ') #以什么开始72 print name.startswith (' a ')

Three: Lists (list)

1 #定义一个列表 2  

Quad: tuples

Elements of tuples cannot be modified, but elements of tuples can be modified

Count

Index

Five: Dictionary

#判断元素的类型是不是某种类型

#例子

#type (name) is List

 1 dic = {' K1 ': ' AA ', ' K2 ': ' BB ', ' K3 ': ' CC '} 2 #清除内容 3 #a = Dic.clear () 4 #print a 5 #根据key获取值 6 print dic[' K1 '] 7 print dic. Get (' K1 ') 8 #如果key值不存在, do not use Get method will be error, using get to set the default value, if this key exists, then the default value 9 #print dic[' K4 ']10 print dic.get (' K4 ', ' OK ') #判断是否有key12 print dic.has_key (' K5 ') #所有项的列表形式14 print dic.items () #所有key的列表16 print Dic.keys () # #判断是否有key18 fo          R k in dic:19 print k,dic[k]20 #获取显示出来并在字典中移除21 "Print dic.pop (' K1 ') #指定key删除23 print Dic.popitem ()  #随机的删除24 "" dic = {' K1 ': ' AA ', ' K2 ': ' BB ', ' K3 ': ' CC '}28 #清除内容29 #a = dic.clear () #print a31 #根据key获取值32 Print dic[' K1 ']33 print dic.get (' K1 ') #如果key值不存在, do not use get error, you can use get to set the default value, if this key exists, will not print the default value of #print dic[' K4 '] Dic.get print (' K4 ', ' OK ') PNs #判断是否有key38 print dic.has_key (' K5 ') #所有项的列表形式40 print dic.items () #所有key的列表42 print dic  . Keys () # #判断是否有key44 for K in dic:45 print k,dic[k]46 #获取显示出来并在字典中移除47 "" Print dic.pop (' K1 ') #指定key删除49 Print Dic.popitem () #随机的删除50 "" "#copy: Shallow copy and deep copy, alias of the difference between >>> dic56 {' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}57 >>> copy_dic = di C.copy () >>> copy_dic59 {' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}60 >>> second_dic = dic61 >>> sec ond_dic62 {' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}63 >>> dic64 ' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}65 >>> SE cond_dic66 {' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}67 >>> copy_dic ', ' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA '}69 &G   t;>> dic[' K4 ']= ' dd ' >>> dic71 {' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA ', ' K4 ': ' DD '}72 >>> second_dic The "K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' AA ', ' K4 ': ' DD '}74 >>> copy_dic ' @ ' K3 ': ' cc ', ' K2 ': ' BB ', ' K1 ': ' A  A '}

Six: Collection (is an unordered and non-repeating collection of elements)

 1 >>> a = Range (5,10) 2 >>> b = Range (7,12) 3 >>> a 4 [5, 6, 7, 8, 9] 5 >>> B 6 7 [ 7, 8, 9, ten, one] 8 9 #将a列表set成集合10 each >>> c = Set (a) >>> C set ([8, 9, 5, 6, 7]), #在a列表追加一 6, re-set only displays a 6 >>> a.append (6) >>> A20 [5, 6, 7, 8, 9, 6]21 >>> C = Set (a) x >>  > C23 Set ([8, 9, 5, 6, 7]), >>> d = set (b) >>> c,d (Set ([8, 9, 5, 6, 7]), set ([8, 9, 10, 11, 7]) #取交集30 >>> C & D ([8, 9, 7]) #取合集36 Notoginseng >>> C | D ([5, 6, 7, 8, 9, ten, one)) >>> c ^ D ([5, 6, ten]), #取差集48 >& Gt;> c-d Set ([5, 6]) #是否是子集54 >>> e = set ([8,9]) >>> e57 Set ([8, 9]) >>&gt ; E.issubset (d) True #是否是父集63 >>> E.issuperset (d) + False66 >>> e67 Set ([8, 9]) >>& Gt E.pop () 870 >>> E71 Set ([9]) 72 >>> d73 Set ([8, 9, 9, 7]) 8 >>> d.remove () >>> d76 Set ([7, ten,]) E78 set ([9]) >>> D80 Set ([8, Ten, one, 7]) Bayi >>> d.update (e) 8 >>> D ([7, 9, 10, 11,  ])

Seven: Time

There are three types: timestamp, format

1 Import  time  2 print time.time () 3 #以时间戳的形式存在 4 #1445324355.5 Print Time.gmtime () 6 #time. Struct_time (tm_year= tm_mon=10, Tm_mday=20, tm_hour=6, tm_min=59, tm_sec=15, Tm_wday=1, tm_yday=293, tm_isdst=0) 7 print time.strftime (' %y-%m-%d%h:%m:%s ') 8 #自定义格式 9 #2015 -10-20 15:00:5211 print time.strptime (' 2014-11-11 ', '%y-%m-%d ') #将格式化的时间转成一个结构 The time of the #time. Struct_time (tm_year=2014, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, Tm_wday=1, tm_yday=315, Tm_isdst=-1) Print time.localtime () #是一个结构化的时间

Python Basics Essay

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.