Python Day-3

Source: Internet
Author: User
Tags bit set

1. Data type

Digital int is mainly used for computational purposes and is not used in many ways. e.g. 1 100 1000

boolean bool has two types, true,false. The correct condition of the reaction. If True False

The string str is used to store a small amount of data for operation. Like ' I am a boy ' a lot of children '

The list of lists stores a large amount of data. such as [All-in-a--"Thai, ' 12353234 ', [up and Down]]

Ganso A tuple- read-only list. such as ("third party",)

Dictionary dict: Key-value pairs (relational data) for storing information dictionary {' Cloud elder sister ': [], ' two elder brother ': [200,200,200,。。。。。。 ]}

collection (rarely used): {1,2,34, ' asdf '}

2. Common functions for each data type

1 int i

The length of the i.bit_length () the minimum number of digits represented by the binary

1                 bit_length2 1     0000 0001      13 2     0000 0010      24 3     0000 0011      2

Mutual transformation of int and Str

1 int----> str2 i = 13 s = str (i)

Mutual transformation of STR and int

1 str---> int2'123'3 i = Int (s)

2 bool

int----->bool 0----False not 0----True

BOOL (6)----True

BOOL (0)----False

BOOL----> int True----1 False----0

int(True)----1

int(False)----0

The empty string is false, and the non-empty string is true.

High-end conditional notation (higher conversion efficiency):

1  while True: 2     Pass 3  while 1: High efficiency 4     pass

Determine if the user entered:

1  s = input (' xxx ')2  if  s:3      print('  You entered the empty, please re-enter ')4  else:5       Pass

3 str

s = ' abcdlsesrf '
Index

The position of the first character of the string is 0!!!

1 s1 = S[0]  take the No. 0 character 2 s2 = s[2]  take the 2nd character 3 s3 = S[-1] Take the last character to the bottom of the  /c7>4 s4 = S[-2]  take the second last character

Slices: Gu Tou regardless of tail

1 s5 = S[0:4]  take the first 4 characters 2 s6 = s[0:-1]  take the characters from beginning to end without the last digit 3 S7 = s[:]  take all contents 4 s8 = s[0:] Take all contents 5 S9 = s[0:0] empty string nothing.

s = ' abcdlsesrf ' # s[First: Tail: Stride Length]

1 s10 = S[0:5:2] Take the first 5 steps 22 S11 = s[4:0:-1] from 4th to 1th digit does not contain 1th bit 3 s12 = s[3::-1
   
    4 S13 = s[3::-2] from 3rd to take the head with step 2
   

s = ' abcdlsesrf '

1 s14 = s[-1::-12 S15 = s[::-1] Take the head from the last one

s = ' Alexwusir '

Initial capital S1 = s.capitalize ()

All uppercase, all lowercase (can be used in user input processing and database data alignment) (no effect on numbers)

1 s2 =2 S21 = S.lower ()

Case Flip

1 s3 = S.swapcase ()

Capitalize the first letter of each word separated (special characters or numbers)

1 ' Alex*egon-wusir ' 2 s4 = S.title () (A E W letter capitalized)
1 ' Fade,crazy*w4rri0r_songsong node_3 ' 2 s4 = S.title () (f C W R R S n Letter capitalized)

Center, Blank fill

1 s5 = S.center ("~") (fill length fill content)

With a 8-bit set of spaces, the content before/T is filled with more than 8 bits of the 16-bit 24-bit calculation (not very common)

1 s6 = S.expandtabs ()

Length Len (s) (the length the base character can see)

1 s7 = Len (s) (characters)

With what beginning End

1 S8 =s.startswith ('Alex'2 s81 = S.startswith ('E  ', 2,5)3 S9 =s.endswith ('Alex'   4 s91 = S.endswith ('e', 2,5)

Find indexed by element, cannot find return-1
Index indexed by element, no error found

1 s8 = s.find ('a')2 s81 = S.index ('a' )

Strip default delete before and after space Lstrip (left delete) Rstrip (right delete)

In Lstrip (), you can add content to be deleted (only for the front and back content is not for intermediate content) (can be used in user input processing and database data comparison)

It works by cleaning up the contents of the strip () and encountering a different stop cleanup

1Username = input ('Please enter your first name:'). Strip ()2 ifUsername = ='Brother Chun':3     Print('Kong Hichun is rich')4 5s ='alexwusir%'6S9 = S.strip ('%') (Removal%)7s ='*a%lexwusi* r%'8S91 = S.strip (' %*') (remove%*)

Count occurrences S.count (' content ')

1 ' Alexaa Wusirl ' 2 s10 = S.count ('al')3print(S10)

Split Split, you can convert a string to a list.

1 ' ; Alex;wusir;taibai ' 2 L = s.split ('a') (Enter the content to be split)3print(l )

Format output in three ways

1s ='my Name {}, this year {}, hobby {}, say again my name {}'. Format ('Taibai', 36,'Girl','Taibai') (one by one corresponds)2Name = input ('Please enter your first name:')3s ='My name is {0}, this year {1}, hobby {2}, say my name {0}'. Format (name,36,'Girl') (by location)4Name = input ('Please enter your first name:')5s ='my name {name}, this year {age}, hobby {hobby}, say again my name {name}'. Format (age=18,name=name,hobby='Girl') (name corresponds)

Replace S.replace (', ', ') (original content, replacement content, number of replacements)

1 ' The old King came to visit the neighborhood, visiting relatives for 2 days. ' 2 S11 = S.replace (' neighborhood ',' Lao Wang ', 13  Print(S11)

For Loop (traversal)

1 ' Fhdsklfds ' 2  for inch S: 3 Print (i)

In ... Medium (in the condition can only put one)

' fdsa Cang jing Empty fdsalk ' if ' Cang Jing Empty ' inch s:     Print (' your comments have sensitive words ... ')

Python Day-3

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.