Python Introductory Notes __python

Source: Internet
Author: User

One, Python basics

1. Input and output: print () input () e.g. input (' Please enter your name: ') #input的数据为字符串, A=int (b) can be used to change the data type

2. #作注释 Four spaces Indent size

3.print ("line1")

... line2

... line3 ' ") #其中符号 ... Automatically appears for interactive mode, if the quote plus r indicates symbol \ non-change in the string

4. Boolean logical operator True, false can and, or, not operations

5. Null value: With none representation

6. Variable: input a=123, Memory creation A and 123,a point 123

7. Input 10/3 gets 3.333333333335 (for floating-point number), board except: 10//3=3 is an integer (Intercept integral part)

8. Character encoding: ASCII code is a byte, the English letter; Unicode is usually two bytes; UTF-8 simplifies Unicode and is a byte in English. Python is encoded in Unicode with Ord () to obtain character encoding, CHR () to obtain the encoded corresponding character.

9. Len () function to calculate string length

10. Format: '%2d-%03d '% (3,1) #其中2表示空一格, 03 means plus two 0

'%.2f '% 3.1415926 #其中. 2 to keep two decimal places

% of the #字符串中若有%

11.list:classmates =[' Michael ', ' Bob ', ' Tracy '

Count: Starting from 0, Classmates[-1] represents the penultimate first

Classmates.append () append element to end

Classmates.insert (1, ' Mike ') Insert element #1表示插入位置 (insert as second element)

Classmates.pop (i) Delete element # I representation position, no I delete last one

#可对classmates [1] Direct Assignment

#list中可有不同类型数据 (including list, can be composed of two Yuan array)

12.tuple: Same as List but cannot be modified (tuple point to element, if element has list,list can still be changed)

#tuple只有一个元素时, if the input t= (1), the t=1 should be entered t= (1,)

13. Conditional judgment: If ...: elif ...: Elif...else ...

#if x:print (' True ') indicates that the x is not null to zero output True (note the second line of empty Sig)

14. Cycle: ①for...in ... e.g. for name in Names:

Print (name) #names可为list或tuple

#range (101) can generate series from 0 to 100

②while ...

15.dict:e.g. D ={' Michael ': ' Bob ': $, ' Tracy ': 85}

d[' Michael '] 95

In statement to determine whether the key is in Dict (return bool): ' Thomas ' in D

The Get statement determines whether the key is in Dict: D.get (' Thomas ') #返回None时不出现结果

Pop delete key:d.pop (' Bob ')

# dict Disorder

16.set:e.g.s=set ([1,1,2,2,3,3])

s {1,2,3} #创建时需提供一个list, collection filter repeating elements, unordered

Add (key) remove (key) can be added elements to Set:s.add (4)

Two

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.