Wang Chengpeng's Python learning note one

Source: Internet
Author: User

This Learning note organizes the contents of list and tuple, conditional judgments and loops, dict and set, functions, slices, list generation, generator seven, and is very basic, but a bit trivial. Well, let's get started.

(1) List and tuple

One of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time. Use it as an array in the process of actual use. It is worth noting that the list has a Append method to add elements to the end of the list.

classmates = [‘Michael‘, ‘Bob‘, ‘Tracy‘]

>>> classmates.append (' Adam ')

>>> Classmates

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

Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed.

classmates = (‘Michael‘, ‘Bob‘, ‘Tracy‘)

(2) Condition judgment and circulation

For example, make conditional judgments, and note the colon at the end of the IF statement line

if age >= 18:

    print ‘your age is‘, age

    print ‘adult‘

Or join the Else statement

if age >= 18:

    print ‘your age is‘, age

    print ‘adult‘

else:

    print ‘your age is‘, age

    print ‘teenager‘

The loop here involves an iterative object, all of which can be called for loop statements to facilitate all of the elements. For example, Dict is an iterative object, and the following code iterates over a dict (traversal)

>>> d = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}

>>> for key in d:

...     print key

 

a

c

b

(3) Dict and set

Python built-in dictionary: dict support, Dict full name dictionary, in other languages also known as map, using key-value (Key-value) storage, with a very fast search speed. For example, to find students results, the code is as follows:

>>> d = {‘Michael‘: 95, ‘Bob‘: 75, ‘Tracy‘: 85}

>>> d[‘Michael‘]

95

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, and the repeating elements are automatically filtered:

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

You can use & and | To do the intersection and operation of collections.

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
set([2, 3])
>>> s1 | s2
set([1, 2, 3, 4])

(4) function

Unlike functions in the C language, functions in Python can return multiple values. For example: In the game often need to move from one point to another point, given the coordinates, displacements and angles, you can calculate a new new coordinates:

import math

def move(x, y, step, angle=0):

    nx = x + step * math.cos(angle)

    ny = y - step * math.sin(angle)

    return nx, ny

Note that the function is defined with a colon at the end of the first line.

(5) Slicing

A slice operation is used when taking an element of a list or tuple, and the common usage is as follows:

>>> L = [‘Michael‘, ‘Sarah‘, ‘Tracy‘, ‘Bob‘, ‘Jack‘]

No. 0 to the 3rd before the first one.

>>> L[0:3]

[‘Michael‘, ‘Sarah‘, ‘Tracy‘]

>>> L = range(100)

所有数,每5个取一次

>>> L[::5]

[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

Don't even write anything, just write [:] to copy a list.

(6) List-generated

The list generation, which is the comprehensions, is a very simple and powerful build of Python built-in that can be used to create lists.

If you want to generate a list of all the full squares from 1 to 100.

>>> [x * x for x in range(1, 11)]

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

一行代码即可解决问题

(7)生成器

和列表生成式不同的是,把最外面的[]改为()即为生成器,它也是可迭代的。

>>> g = (x * x for x in range(10))

>>> for n in g:

...     print n

The final output is 1 to 100 total of 10 complete squares.

Wang Chengpeng's Python learning note one

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.