Python Basics 03

Source: Internet
Author: User

The main contents of this section:
1. Python Basic Data type review
2. int----Numeric type
3. BOOL---boolean type
4. str---string type
One. Python basic data type
1. int ==> integer. Used primarily for mathematical operations
2. str ==> string, can save a small amount of data and do the corresponding operation
3. Bool==> judgment True, false
4. list==> stores large amounts of data. Use [] to denote
5. Tuple=> tuples, can not be changed with () indicated
6. Dict==> dictionary, save key value pair, same can save large amount of data
7. Set==> collection, saving large amounts of data. Can not be repeated. In fact, it is not the dict to save value

Two. Integer (int)
All integers in Python3 are of type int. But in Python2 if the amount of data is larger. A long type is used. There is no long type in Python3
An integer that can be manipulated:
Bit_length (). Calculates the length of the binary code that an integer occupies in memory

Three. Boolean value (BOOL)

TRUE (1) or False (0)

Four. String (str)
String the characters together. What is called ",", "", "" in Python is referred to as a string.

4.1 Slices and indexes
Index is starting from 0

Slice syntax: str[start:end:step] Start: End: Step

4.2 Related ways to manipulate strings
Remember that a string is an immutable object, so any action will have no effect on the original string.

Transformation:

s= "Hello World"

S.capitalize () Capitalize first letter

S.title () characters separated by special characters are capitalized and Chinese are special characters

S.upper () converted to uppercase

S.lower () Convert to lowercase

Verification Code Application:

< Span style= "Font-size:small" >verify_code = Span class= "FONTSTYLE0" > "Abde"
user_verify_code = input ( "Enter the CAPTCHA:" )
If verify_code.upper () = = User_verify_code.upper ():
print< Span class= "FONTSTYLE0" > ( "validation succeeded" )
Else:
print ( "validation Failed" )

S.swapcace () Large turn small small turn large reverse conversion

Cutting:

s= "Jay Chou"

S.center (5, "*")-------->>> * Jay * Note: The elongated character is 5, the meta-word descriptors the middle, the rest with * complement the two sides

S.strip () Remove the left and right spaces () to add the specified element

S.lstrip () Remove the left space

S.rstrip () Remove the space on the right

# app, impersonate user login. Ignore user-entered spaces
Username =input ( "Please enter user name:" Password = input ( "Please enter your password:" if username = = ' Alex ' and password = = 123 ' :
print ( "login Successful" )
else :
print ( "Login Failed" )

S.replace ("original character", "new character", replacing several)

S.split ("+") with + cut

Formatted output:

Find:

s = "My name is Sylar, I like Python, Java, C and other programming languages."

S.startswith () Decide what to start with

S.endswith () Judge with what end

S.count ("a") find the number of occurrences of a

S.find ("a") find where a appears if not present returns-1

S.index ("a") find the index of a appears if it does not exist the error

Condition Judgment:

S.isalnum () whether numbers and letters

S.isdigit () whether the number

S.isalpha () Whether the letter

S.isnumeric () Whether digital Chinese is also recognized

Len (s) determines the length of the string s

7. Iteration
We can use a For loop to facilitate (get) every character in a string
Grammar:
The for variable in can iterate over the object:
Pass
An iterative object: an object that can take a value outward

Python Basics 03

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.