Python basic two--data type first knowledge

Source: Internet
Author: User

1, Integer int
int is primarily used to calculate-----"i.bit_length ()": Represents the minimum number of bits that I occupy.
#int-----i.bit_length (): Indicates the minimum number of bits that I occupy. I= 13print (I.bit_length ())
2, Boolean bool

There are two types of Boolean values: True,false. is the correct condition of the reaction.

True 1 true.

False 0 false.

3. String str

String is mainly used for storing a small amount of data, and subsequent operations.

integer int and string str convert to and from each other:

int---> str str (int)

STR----> int int (str) str can only be a combination of numbers at this time

Conversions between INT and bool BOOL:

int----> BOOL non 0 O'Clock value is True, 0 is False

BOOL----> int true converts to an integer int with a value of 1,false converted to an integer int type value of 0

# int---> bool      non 0 O'Clock value is true, 0 is Falsea = bool (2) b = bool (0) print (a)             #运行结果: Trueprint (b)             #运行结果: False#bool ---> int        True Converts the value of the int literal to 1,false to integer int with a value of 0c = Int (true) d = Int (False) print (c)             #运行结果: 1print (d)             # Run Result: 0  

Mutual conversions between the string STR and the boolean bool:

STR---> BOOL non-null return value is true, NULL return value is False
BOOL---> str str (True) str (False)

#str--    the bool non-null return value is true, and the null return value is Falsei = bool (' 3 ') print (i)           #运行结果: Truei = bool (0) print (i)           #运行结果: Falsei = BOOL (") print (i)           #运行结果: False#bool--– Str str (TRUE) str (     False) i= str (true) print (i)           # Run Result: truei= str (FALSE) print (i)           #运行结果: False

Common Knowledge point Additions: If conditional statements and while loop statements are used for conditional judgments.

If 1:    print (' a ') Else:    print (' B ')       #运行结果: AIF 0:    print (' a ')      else:    print (' B ')       # Run Result: bwhile True:    print (' a ')    break            #运行结果: Awhile 1:             # and while True compare the execution efficiency of while 1 high   print (' B ')   break             #运行结果: b
4. Indexing and slicing of strings

 The index is subscript, that is, the elements of the string begin with the first, the initial index is 0, and so on.

A = "a1b2c3" Print (a[0])           #运行结果: Aprint (a[1])           #运行结果: 1print (a[2])           #运行结果: b

A slice is a section of a string that is truncated by an index (START index: Stop index: Step), forming a new string (the principle is that Gu head ignores butt).

A = ' Abcdefghijk '
Print (A[0:3])
Print (A[2:5])
Print (a[0:]) #默认到最后
Print (a[:]) #从头到尾
Print (A[0:-1]) #-1 is the last one.
Print (A[0:5:2]) #加步长
Print (A[5:0:-2]) #反向加步长
Print (A[-1:-6:-2]) #反向取加步长

The output of the above code:

Abccdeabcdefghijkabcdefghijkabcdefghijacefdbkig
View Code

String Common methods: format is similar to formatted output but each has a different application scenario.

#第一种s = ' I'm {}, this year {}, height {} '. Format (' Jinxin ', 21,175) print (s) #第二种s = ' I'm {0}, this year {1}, height {2}, I still call {0} '. Format (' Jinxin ', 21,175) Print (s) #第三种s = ' I'm {name}, this year {age}, height {high} '. Format (name = ' Jinxin ', high=175,age=21) print (s) 

Common methods of strings

s="aBc D12%h&j ("Print(S.capitalize ())#capitalize the first letter of the string run result: ABC d12%h&j (Print(S.swapcase ())#Case Rollover Run Result: AbC d12%h&j (Print(S.title ())#non-letter-separated place first uppercase, other lowercase run result: ABC d12%h&j (Print(S.upper ())#ALL caps Run Result: ABC d12%h&j (Print(S.lower ())#large all lowercase run result: ABC D12%h&j (Print(S.center (25))#Center content Run Result: ABc d12%h&j (Print(S.center (25,'*'))#Center content on both sides with * fills the running result: *******ABC d12%h&j (******Print(S.expandtabs ())#the tab symbol (' \ t ') in the string is converted to a space, and the tab symbol (' \ t ') default number of spaces is 8Print(S.find ('D'))#through the element to find the index, can look for the whole, can slice, can't find return-1 run Result: 4Print(S.index ('D'))#through the element to find the index, you can look for the whole, can be sliced, can't find the error running result: 4Print(S.startswith ('a'))#checks whether the string starts with the specified substring, or returns True if it is, otherwise FalsePrint(S.endswith ('('))#checks whether the string ends with the specified substring, returns True if yes, otherwise returns FalsePrint(S.strip ())#Remove the space at the front and back of the string, line breaks, tab keys, or specified characters. Run Results: ABc d12%h&j (Print(S.lstrip ())#truncate the left space of a string or specify a character to run the result: ABc d12%h&j (Print(S.rstrip ())#truncate the space to the right of the string or specify the character to run the result: ABc d12%h&j (Print(S.split ('b', 3))#slices A string by specifying a delimiter--->list run result: [' aBc d12%h&j (')Print(S.replace ('a','Small', 2))#replace the "a" in the string with "small" 2 run results: small BC D12%H&J (Print(S.isalnum ())#A string consists of a letter or a number running the result: FalsePrint(S.isalpha ())#The string consists only of letters. Run Result: FalsePrint(S.isdigit ())#string only consists of numbers run result: FalsePrint(S.count ('a'))#counts the number of occurrences of "a" in a string. Run Result: 1Print(Len (s))#calculates the length of the string "s" or the number of items running results:#Strip application Examples:Name = input ('Please enter your first name:'). Strip ()ifName = ='Alex':    Print('Somebody')Else:    Print('Please re-enter')#Upper and lower application examples, all uppercase and all lowercase actual application: Case-insensitive verification code. Code ='Aedd'Your_code= Input ('Please enter the verification code:')ifYour_code.upper () = =code.upper ():Print('Enter the correct')Else:    Print('Please re-enter')
View Code

Python basic two--data type first knowledge

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.