Python basic Three

Source: Internet
Author: User
Tags for in range

First, Introduction 1. What Is data?

x=10,10 is the data we want to store.

2. Why the data is divided into different types

Data is used to represent States, and different States should be represented by different types of data.

3. Data type

Digital

String

List

Meta-group

Dictionary

Collection

II. basic data type 1. Digital int

The number is mainly used for computing, not many ways to use, just remember one can:

# bit_length () when decimal is represented in binary notation, the minimum number of bits used is v =one = v.bit_length ()print(data)
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.

int------> BOOL 0:false non 0: True

BOOL----->true T----->1 F----->0

STR------->bool NOT NULL true NULL is FALSE

s='abc'print(bool (s))

BOOL----->str STR (True) str (False)

Print (Str (TRUE), type (str (TRUE)))

While 1 performs more efficiently than while true

3. String str, index and slice of string

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

' Abcdefghijk ' Print (a[0]) Print (a[3]) Print (a[5]) Print (A[7])

A slice is a section of a string that is intercepted by an index (index: Index: STRIDE), forming a new string (the principle is that Gu head ignores butt).

' Abcdefghijk ' Print (A[0:3]) Print (A[2:5]) Print # default to Last Print # -1 is the last one . Print # Add Step Print # Reverse Add step
Ii. Common methods of string

Captalize, Swapcase, title

1 ' Abcdefghijk ' 2 Print # Capitalize first letter 3 Print # Case Flip 4 msg='Egon say hi'5print#非字母隔开的部分, Capital Letter, other lowercase

Upper All caps

Lower All lowercase

A='Lao Dao Ba Zi'print(A.upper ())print(A.lower ())
1Code='Aedb'2Your_code=input ('Please enter the verification code:')3 ifYour_code.upper () = =code.upper ():4     Print('Enter the correct')5 Else:6     Print('Please re-enter')

Center in cohabitation, total length, padding

A='Lao Dao Ba Zi'= A.center ("*")  Print(Ret2)

Expandtabs

" hqw\t " # \ t Front of completion # By default, a tab key becomes 8 spaces, if the character in front of the tab is less than 8, then complete 8, if the character in front of the TAB key is more than 8 less than 16, then complete 16, and so on each complement of 8. Ret4 = a2.expandtabs ()print(RET4)

The number of elements in the count number character string

A='Lao Dao Ba Zi'= A.count ("a"#  can be sliced  Print(RET3)

StartsWith judge whether to ... Beginning
EndsWith judge whether to ... End

" dkfjdkfasf54 "  = A4.endswith ('jdk', 3,6)  #  Gu Tou disregard butt print(RET4)  #  Returns a Boolean value of Ret5 = A4.startswith ("kfj", 1,4) Print (RET5)

find finds the existence of an element in a string

" dkfjdkfasf54 "  = A4.find ("fjdk", 1,6)print(RET6)  #  Returns the index of the found element if it is not found-1ret61 = A4.index ("fjdk", 4,6)Print  #  Returns the index of the found element, unable to find an error. 

split with what split, eventually form a list this list does not contain this split element

' Title,tilte,atre, '. Split ('t')print'title,tilte, Atre,'. Rsplit ('t', 1)print(ret91)

Format formatted output

res='{} {} {}'. Format ('Egon', 18,'male')Print(res) Res='{1} {0} {1}'. Format ('Egon', 18,'male')Print(res) Res='{name} {age} {sex}'. Format (sex='male', name='Egon', age=18)Print(RES)

strip remove spaces, line breaks, tab keys, and so on at both ends of a string

Name='*egon**'print(Name.strip ('*')) Print (Name.lstrip ('*')) Print (Name.rstrip ('*'))

Replace replacement

Name='Alex Say:i has one tesla,my name is Alex'print(name.replace ('  Alex','SB', 1))

len length of the string

s='Alex'print(len (s))

Is series

Name='jinxin123'print# string composed of letters or numbers print#  strings consist only of letters the print# string consists only of numbers

Three, tuple tuple

Tuples are called read-only lists, where data can be queried but cannot be modified, so the slicing of strings also applies to tuples. Example: ("A", "B", "C")

Iv. List of lists

The list is one of the underlying data types in Python, and other languages have data types similar to lists, such as JS called arrays, which are enclosed in [] and each element is separated by commas, and he can store various data types such as:

Li = [' Alex ', 123,ture, (Wusir), [All-in-all, ' xiaoming ',],{' name ': ' Alex '}]

Compared to strings, lists can store not only different data types, but also large amounts of data, 32-bit Python is limited to 536,870,912 elements, and 64-bit Python is limited to 1.,152,921,504,606,85e,+18 elements. And the list is ordered, there are index values, can be sliced, convenient to take value.

Five, dictionary

A dictionary is the only type of mapping in Python that stores data in the form of key-value pairs (key-value). Python computes the hash function of key and determines the storage address of value based on the computed result, so the dictionary is stored out of order and the key must be hashed. A hash indicates that a key must be an immutable type, such as a number, a string, a tuple.

The Dictionary (dictionary) is the most flexible built-in data structure type in addition to the list of unexpected python. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

Six, the collection

Set={1, ' Alex ', True}

Iii. Others (For,enumerate,range)

For Loop : The user iterates through the contents of an object in order.

msg ='old boy Python is the best Python training organization in the country' forIteminchmsg:Print(item) Li= ['Alex','Silver Corner','Goddess','Egon','Taibai'] forIinchLi:Print(i) DIC= {'name':'Taibai',' Age': 18,'Sex':'Mans'} forKvinchDic.items ():Print(K,V)

Enumerate: enumerations, for an iterative (iterable)/traversed object (such as a list, string), enumerate it into an index sequence that can be used to obtain both the index and the value.

Li = ['Alex','Silver Corner','Goddess','Egon','Taibai'] forIinchEnumerate (LI):Print(i) forIndex,nameinchEnumerate (li,1):    Print(Index,name) forIndex, nameinchEnumerate (li, 100):#The starting position is 0 by default and can be changed    Print(Index, name)

Range: Specifies a range that generates the specified number.

 for  in range (1,10):    print(i) for in range (1,10,2):  #  step    print(i)for in#  Reverse Step    Print(i)

    

  

      

Python basic Three

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.