Python Getting Started notes

Source: Internet
Author: User

Creating variables

Python variables do not need to declare a data type.

Print (Fred)100
>>> Fred100

Create a string

Strings need to be enclosed in "," "single quotation marks, or double quotes.

>>> sstr='How isyou! '>>> sstr'How isyou! ' Print (SSTR) how is you!

Embed a value in a string

Where%s is a placeholder, and the following variable myscore represents the substituted variable

>>> myscore =100>>> message='I scored%s points! ' Print (Message%points!

If there are multiple placeholders, the following variables are enclosed in parentheses, separated by commas

List

The list is somewhat equivalent to an array, but much more powerful than a normal array.

List created by []

The elements in the list are not necessarily of the same data type

List elements can be modified by subscript

>>> mylist=[9,13,421,'abc']print  (myList) ['  ABC']>>> mylist[0]='ok'print  (myList) ['ok'abc']

You can use Print to output part of the list content, noting that the range is left closed right.

Print (Mylist[2:4]) ['abc']

Adding new elements to the list requires append. It is wrong to set the value directly by subscript, and it will be out of bounds.

>>> mylist[4]=1111indexerror:list Assignment indexout of range >>> Mylist.append (1111)  Print (mylist[4])1111

Delete the specified element

del Mylist[0] Print (myList) ['abc', 1111] del mylist[1:3]print  (myList) [13, 1111]

The list supports + and * operations.

+ can connect two lists, * can repeat the specified list n times.

Meta-group

Tuples are more like arrays in C, which cannot be changed once they are created, including the size and value of tuples. So tuples are equivalent to a constant array.

Tuples are created with ()

>>> f= (1,2,3,4)>>> f[0]=10'tuple' not Support Item Assignment

Dictionary

Each element of the dictionary has a key and value, and the value is found by the key.

Dictionary with {} to create

>>> mymap={'a':'A','XXX':'FSADFSG'}>>>Print (MYMAP) {'XXX':'FSADFSG','a':'A'}>>> Print (mymap['XXX']) FSADFSG

Cannot access the element corresponding to a nonexistent key

>>> print (mymap['b'b '

Both the modified value of the dictionary and the deleted element are through the keys.

The addition of the dictionary is meaningless.

If and ELSE statements

Indenting is very important in python. A quotation mark is required after the IF condition.

if age>20:    print ('you'retoo old! ' ) You are    too old!

If there are multiple conditions after if, the and, or, can be used depending on their relationship.

If it is multiple if:

>>> age=if age>:    print ('you'retoo old! ' ) elifage >:    print ('ok! ' )else:    print ('you aretoo young! ' )    you is too old!

Note that the code for the same statement block should have the same indentation! Otherwise you will get an error.

Using a For loop

This function is used in the range, which is obviously left-to-right. It returns an iterator saved to X.

 for  in range (0,5):    print (x)    0123  4

If you save the range iterator to the list

>>> Print (list (range (0,6)) [01234  5]

Output list with For loop

>>> mylist=['zzz','qweqw','AVC ','qweqw','iuyo']for   in myList:    print (i)    Zzzqweqwavcqweqwiuyo

While loop

This is easier.

>>> a=0 while a<:    a=a+1    >>> Print (a)  -

Using functions

Use the range function to create a list

>>> mylist=list (Range (0,5))print1, 2, 3, 4]

Defining functions

Use the keyword def to define it, and note that there is a following:

def TestFunc (myname):     Print ('Hello%s' % myname)     >>> testfunc ('jack') Hello Jack

Note that variables within a function cannot be used outside of a function, and variables outside the function can be used within a function

Module

Import modules via Import

Import  Time Print  17 16:34:26 2014

Sys.stdin.readline () reads a line of string

>>> Import sys>>> a=int(sys.stdin.readline ())+ >> > Print (a)>>>123123  xxx>> > x,y=sys.stdin.readline (). Split ()312>>> Print (x)  one >>> print (y)312

Split is a function of a string, and the returned result can exist in a list, but the result is str. If you need an integer, you need to convert it with Int.

Classes and objects

The following are the creation of base classes, derived classes, and creation of objects

class Animals:    passclass  Mammals (Animals):    pass>>> p=mammals ()

The self parameter can be used to invoke another function from one of the functions in the class. Self is actually referring to the object.

class Animals:    def move:        pass    def dance (self):        self.move ()        Pass

Initializing objects

Initializes the object with __init__. Note that both front and back are two _

class Animals:    def __init__ (self,age):        self.age=Age        pass>>> a=animals (100  )>>> Print (a.age)

= No new object created

>>> a=animals >>> print (a.age)>>> b =a>>> b.age=>>> print (a.age)

Plainly, B and a are pointing to the same object.

The same is true for list

>>> mylist1=[111,222,333]
>>> Mylist2=mylist1
>>> mylist2[0]=999
>>> Print (MYLIST1)
[999, 222, 333]
>>> Mylist2.append (555)
>>> Print (MYLIST1)
[999, 222, 333, 555]

It will be found here that the two variables are still pointing to the same object after they are actually assigned.

Copy using the Copy module.

>>> Import Copy>>> a=animals >>>b=copy.copy (a) >>> b.age=5>>> print (a.age)>>> print (b.age) 5

So that A and B point to different objects respectively.

But for list.

>>> mylist1=[111,222,333]>>> mylist2=copy.copy (myList1)>>> mylist2[0]=999>>>print (myList1) [111,222,333]>>>print (MYLIST2) [999,222,333]>>> Mylist2.append (1324)>>>print (MYLIST2) [999,222,333,1324]

But note that int belongs to the basic data type

>>> a=animals (1)>>> b=animals (2)>>> mylist1=[A , b]>>> mylist2=copy.copy (myList1)>>> mylist2[0].age=>> > Print (mylist1[0].age)>>> print (mylist2[0].age)   About

It can be found that copy is a shallow copy, that is, it copies the list but does not copy the list of objects, the two list is actually the same object. But this is a list of two different.

For this there is a deepcopy that corresponds to copy, which creates a new object.

>>> a=animals (1)>>> b=animals (2)>>> mylist1=[A , b]>>> mylist2=copy.deepcopy (myList1)>>> mylist2[0].age=> >> print (mylist1[0].age)1>>> print (mylist2[0].age)   About

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.