Python Learning notes (ii)

Source: Internet
Author: User

One, the output special symbol single quotation mark ( ' ), double quotation marks ( ' " )

(1) when outputting single quotation marks, print "I m Lilei" with double quotation marks

(2) when outputting double quotation marks, print ' You is a ' bad ' man ' with single quotation mark

(3) when both single and double quotation marks are used, use the triple quote "' or " "

Print ""

I ' m Lilei

You are a ' bad ' man

‘’’

(4) You can also use the escape symbol \

print ' i\ ' m \ "Lilei\" '

(5) An escape symbol is also useful for wrapping in code without affecting the output

Print "It s a great \

Day

The output result is It ' s a great day

string Formatting

The plus sign (+) can stitch two strings together, but cannot add other types of data. Other types of data addition need to be formatted into a string first.

A= ' abc '

b= ' EDF '

C=18

D=a+b

Print A+b

Print A + ' my name '

Print A+str # print a+str (c)

Print "I ' m%d years old"%c

print "I ' m%d years old%s"% (c,d) # When there are multiple variables, enclose them in parentheses and separate them with commas

linebreak and no line break

(1) output does not wrap, ending with commas

For I in range (0,5)

print ' * ',

(2) Output line wrapping

For I in range (0,5)

print ' * ',

(3) Only output line wrapping effect, do not print any value

For I in range (0,5)

print ' * ',

Print

(4) print ' \ n ', resulting in two blank lines printed

Conversion of type

Python does not need to restrict the type to a variable when it is defined. The variable automatically determines its type based on the value assigned to it. You can also change its value in the program, so it changes its type. For example:

A=1

A= ' Hello '

A=true

variable A has become an integer, string,bool type.

Although the type can be arbitrarily changed, when you manipulate a particular type of variable, an error occurs if the operation does not match its data type. For example, the following lines of code:

print ' Hello ' +1
print ' hello%d '% ' 123 '

in this case,Python provides several ways to type conversions on numeric values:
int (x) #把x转换成整数
Float (x) #把x转换成浮点数
STR (x) #把x转换成字符串
BOOL (x) #把x转换成bool值

The above two examples can be written as:
print ' Hello ' +str (1)
print ' hello%d '% int (' 123 ')

,True , and False

BOOL (1) ==true

BOOL (0) ==false

BOOL (') ==false

BOOL (None) ==false

BOOL (' Any one character or space ') ==true such as bool (') ==true

in the same vein, there are bool ([]) ==false bool ({}) ==false

definition function def funcName (): 17.1 list

(1) Any data type can be placed in the list, in any order

a=[1,3,8,3, ' Apple ', False]

(2) Accessing elements in a list

Print A[3]3

Print A[-2]apple

print a[1:3][3,8] # indicates an element labeled 1 to subscript 2 , not including a third.

The slice operator is the [] provides a pair of optional numbers, using : split. The number before the colon indicates where the slice begins, and the number after the colon indicates where the slice ends. Likewise, the count starts from 0 .

Note that the start position is included in the slice, and the end position is not included.

Print A[-2,-1] False

(3) Operation of the list

Del List (index): delete the element labeled 2

List.append (obj): Add a new object at the end of the list
List.count (obj): Counts the number of occurrences of an element in a list

List.insert (1, "Jack"): Inserting

List.pop (2): Delete the element labeled 2


List.extend (seq): Appends multiple values from another sequence at the end of the list (expands the original list with a new list)
List.index (obj): Find the index position of the first occurrence of a value from the list
List.insert (index, obj): Inserting an object into a list
List.pop (Obj=list[-1]): Removes an element from the list (the last element by default) and returns the value of the element
List.remove (obj): Removes the first occurrence of a value in a list
List.reverse (): Reverse List of elements
List.sort ([func]): Sort the original list

(4) List of functions

CMP (List1, List2): Compare two elements of a list
Len (list): number of elements
Max: Returns the maximum value of a list element
Min (list): Returns the minimum value of the list element
List (seq): Converting a tuple to a list

17.2 meta-group

The length of the Ganso cannot be changed, and the other is consistent with array operations

to define a tuple with only 1 elements, you need to add a comma after the element to eliminate the mathematical ambiguity

t = (1,)

17.3 Dictionary

in the dictionary, the name is called "key" and the corresponding content information is called "value". A dictionary is a collection of key/value pairs.

D = {key1:value1, key2:value2}

(1) References

Print D[key1] for name in D:print D[name]

(2) Change

Direct Assignment: D[key1]=value3

(3) Increase

D[key4]=value4

(4) Delete

Del D[key4]

(5) Get method

S=d.get (Key)

The Get method of the dictionary class is to find the corresponding item according to the given key , if there is no such key, return null value None .

(6) Judging whether in the dictionary

For I in A.kes ():

Python Learning notes (ii)

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.