Basic syntax for first-time python experience (1): first-time python experience

Source: Internet
Author: User

Basic syntax for first-time python experience (1): first-time python experience

All test statements are based on Python 2.7.3 and Ubuntu 12.04.

Learn Python by yourself. please correct me if anything is wrong. Thank you.

The vast majority of examples are python tutorials from vamei in the blog Park.

1. print ()
#! /Usr/bin/env python # coding = UTF-8 # default python encoding is ASCII encoding to support Chinese # plus coding = UTF-8 statement # output statement print ('Hello Python !! ')

Print a statement. Double quotation marks (single quotation marks) in brackets are acceptable,

You do not need to add;The end is a semicolon, and the addition of the semicolon does not affect.

 

2. Condition Selection statement if elif else

The following code is no longer repeated #! /Usr/bin/env python # coding = UTF-8

# Condition selection statement if (3> 3): a = "Are you happy ??? "Elif (2> 3): a =" bbbbbbbbbbb "else: a =" cccccccc "print;

The indent syntax that is most distinctive in python. It uses four spaces to indicate the link.:Contains statement Blocks Abandon most programming languages {}

You do not need to set a type for the element. Python automatically sets a type for you.ElifElse if.

Regarding whether to use the Tab key or four spaces, I think there is no dispute over setting the tab key to four spaces? I don't know, right.

 

3. Loop statements
# Loop statement for a in [, 3, []: print

Loop statements, similar to the foreach loop of C # and Java. The sub-element in the list can also be list, and the index subscript also starts from 0.

 

4. Function Definition def
# Whether the function is defined as a leyear def isLeafYear (year): return year % 400 = 0 or (year % 100! = 0 and year % 4 = 0) print isLeafYear (2004)

Function Definition, with keywordsDefAs the identifier. WhereOrSimilar keywords|,AndSimilar keywords&&

 

# Function Definition return can return multiple values def square_sum (a, B): c = a ** 2 + B ** 2 return c, a, bprint square_sum (1, 3)

Return can return multiple values, which is quite characteristic. It should not be possible in Java.

 

# Pass table B as a parameter to function B = [1, 2, 3] def change_list (B): B [0] + = 1 return bprint change_list (B) print B

Operate the table through the table index, and the value of Table B changes.

 

# The value passed to the original integer a does not change. Try to replace the function parameter with B or c. A = 1def change_a (a): a = a + 1 return aprint "the return value of the function is:", change_a (a) print "the value of the original integer a is:",

 

5. Class Definition object-oriented
# Defining a parent bird # The first parameter of the member method in the class must be the object that calls the method # used to use self, you can also use it, this, and so on. # Is it true that all classes are inherited from objects ?? Class Bird (object): # a bit similar to the constructor. There are two underscores init def _ init _ (self, something): print 'I am a bird. ', something have_feather = True way_of_reproduction = 'egg' def move (self, dx, dy ): position = [0, 0] position [0] = position [0] + dx position [1] = position [1] + dy return position # generate an object called Summer Bird. you need to use the new keyword summer = Bird ('Call me summer! ') Print summer. way_of_reproductionprint summer. move (1, 2)

 

6. Inheritance

Subclass inherits the parent class:

# Subclass chicks inherit parent birds (single inheritance) class Chicken (Bird): way_of_move = 'walk 'possible_in_KFC = True # subclass geese inherit parent birds (single inheritance) class DaYan (Bird): def _ init _ (self, name): self. name = name way_of_move = 'fly 'possible_in_KFC = False # override the parent class move method def move (self, dx, dy ): position = [0, 0] position [0] = position [0] + dx ** dx position [1] = position [1] + dy ** dy return position def print_name (self): print "My name is", self. name # summe now R is DaYan summer = DaYan ("doudou! ") Print summer. print_name () print summer. have_featherprint summer. move (2, 3) print dir (summer) # dir () is used to query all attributes of a class or object.

 

7. Enhanced class support for operations
# Added support for operations similar to [1, 2, 3]-[2, 3] In the custom list subclass # This method with two _ underscores is a special method class SuperList (list ): def _ sub _ (self, B): # self is a SuperList object. You can use a method similar to list [:] to represent the entire list object a = self [:]. B = B [:] # If the list contains elements in B, remove it until the end of the loop len () returns the number of elements in the list while len (B)> 0: element_ B = B. pop () # pop () pops up the last element value from the list and returns its value if element_ B in a:. remove (element_ B) return a # Here, the minus sign operator represents the _ sub _ () method. Similarly, the + operator represents the _ add () method () __# print SuperList ([1, 2, 3, 4])-SuperList ([1, 2])

 

8. Use of dictionary classes
# Dictionary class container class uses the empty dictionary print dic, type (dic) dic = {'name': 'zhaowuji ', 'age ': 21, 'wife ': 'zhaommin'} # The element is that key-value pairs are unordered and the value print dic ['name'] is obtained using the key value. dic ['wife '] # add new elements. Reference a new key and assign values. From Yi Tian tu Longji dic ['from'] = 'yitiantulongji 'print dic # loop call of dictionary elements in dic: print key ,':', dic [key] # common dictionary method print (len (dic) # query the number of dictionary elements print dic. keys () # Return the value of all keys listprint dic. values () # Return the value of all values listprint dic. items () # Return All dic elements ### Delete method ### del dic ['name'] # del is a python keyword used to delete an object print dic ### delete an element ### print dic. clear () # clear the dic and change it to an empty dictionary {}

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.