Python Learning Day 3 string encoding list tuple Loop dict set

Source: Internet
Author: User

Strings and encodings

Character

Ascii

Unicode

UTF-8

A

1000001

00000000 01000001

1000001

In

X

01001110 00101101

11100100 10111000 10101101

Formatting

In Python, the format used is consistent with the C language and is implemented as a%, for example:

>>> ' Hello,%s '% ' world '

' Hello, World '

>>> ' Hi,%s, you have $%d. '% (' Michael ', 1000000)

' Hi, Michael, you have $1000000. '

%The operator is used to format the string. Inside the string, the representation is replaced by a string, which %s %d is replaced with an integer, there are several %? placeholders, followed by a number of variables or values, the order to correspond well. If there is only one %? , the parentheses can be omitted. Common placeholders are:

%d

Integer

%f

Floating point number

%s

String

%x

hexadecimal integer

where formatted integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals:

>>> '%2d-%02d '% (3, 1)

' 3-01 '

>>> '%.2f '% 3.1415926

' 3.14 '

%s always works, it converts any data type to a string:

>>> ' Age:%s. Gender:%s '% (True)

' Age:25. Gender:true '

For Unicode strings, the usage is exactly the same, but it is best to ensure that the substituted string is also a Unicode string:

>>> u ' Hi,%s '% u ' Michael '

U ' Hi, Michael '

The% inside the string is an ordinary character that needs to be escaped and is represented by a percent

A data type built into Python is a list:

List

A list is an ordered set of elements that can be added and removed at any time.

>>>classmates=[' Michael ', ' Bob ', ' Tracy '

>>> Classmates

[' Michael ', ' Bob ', ' Tracy ']

>>>len (classmates) # Get List Number of elements

3

>>> classmates[0]# Access by index List elements in each of the locations, from 0 Start

' Michael '

>>> Classmates[-1]

' Tracy '

>>> classmates[3]# index out of range Error

Traceback (most recent Calllast):

File "<pyshell#11>", line 1,in <module>

CLASSMATES[3]

Indexerror:list Index Outof Range

>>> Classmates[-2]

' Bob '

>>>classmates.append (' Adam ') # towards List append elements to the end of the

>>> Classmates

[' Michael ', ' Bob ', ' Tracy ', ' Adam ']

>>>classmates.insert (1, ' Jack ') # inserts an element into the specified position

>>> Classmates

[' Michael ', ' Jack ', ' Bob ', ' Tracy ', ' Adam ']

>>>classmates.pop () # Delete List the element at the end

' Adam '

>>>classmates.pop (1) # assign a value to the corresponding index position

' Jack '

>>> Classmates

[' Michael ', ' Bob ', ' Tracy ']

>>>classmates[1]= ' Sarah ' # to replace an element with another element

>>> Classmates

[' Michael ', ' Sarah ', ' Tracy ']

>>> L = [' Apple ', 123, true]# the data type of the element can also be different

>>> s = [' Python ', ' Java ', [' asp ', ' php '], ' scheme ']# The element can also be another List

Tuple

Tuples and lists are similar, but tuples cannot be modified once they are initialized

>>> classmates = (' Michael ', ' Bob ', ' Tracy ')

>>> t = (1,) # only 1 you must add a comma to each element

Special case, "variable" tuple

>>> t = (' A ', ' B ', [' A ', ' B '])

>>> t[2][0] = ' X '

>>> t[2][1] = ' Y '

>>> T

(' A ', ' B ', [' X ', ' Y '])

Conditional judgment

Age =3

ifage >= 18:# if the IF judgment is False , do not execute if the content, execute else

print ' Your age was ', age

print ' adult '

Else

print ' Your age was ', age

print ' teenager '

If statement execution is judged from top to bottom, if it is true on a certain judgment, then the statement corresponding to that judgment is executed, ignoring the remaining elif and else

If < condition judgment 1>:

< Executive 1>

Elif < condition judgment 2>:

< Executive 2>

Elif < condition judgment 3>:

< Executive 3>

Else

< Executive 4>

Cycle

For...in cycle

names= [' Michael ', ' Bob ', ' Tracy ']

Forname in Names:

Print Name

Sum =0

For Xin Range (101):

sum = sum + x

Printsum

While loop

Sum =0

N =99

Whilen > 0:

sum = SUM + N

n = n-2

Printsum

Dict

The Dict key must be an immutable object. The list is mutable and cannot be a key.

>>> d = {' Michael ': Up, ' Bob ': +, ' Tracy ': 85}

>>> d[' Michael ']

95

Compared with list, Dict has the following features:

The speed of finding and inserting is very fast and will not increase with the increase of key;

It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

The time to find and insert increases as the element increases;

Small footprint and little wasted memory.

Set

Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.

To create a set, you need to provide a list as the input collection:

>>> s = Set ([1, 2, 3])

>>> s

Set ([1, 2, 3])

>>> s = Set ([1, 1, 2, 2, 3, 3]) #需要list作为输入集合

>>> s

Set ([1, 2, 3]) #重复元素在set中自动被过滤

>>> S.add (4) #添加元素到set中

>>> s

Set ([1, 2, 3, 4])

>>> S.add (4)

>>> s

Set ([1, 2, 3, 4])

>>> S.remove (4) #删除元素

>>> s

Set ([1, 2, 3])

>>> S1 = set ([1, 2, 3])

>>> s2 = Set ([2, 3, 4])

>>> S1 & s2# Intersection

Set ([2, 3])

>>> S1 | s2# and set

Set ([1, 2, 3, 4])

The only difference between set and dict is that it does not store the corresponding value, but the set principle is the same as the dict, so it is also not possible to put mutable objects, because it is not possible to determine whether the two Mutable objects are equal, and there is no guarantee that there will be no duplicate elements inside the set.

Welcome attention

Python Learning Day 3 string encoding list tuple Loop dict set

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.