Core Python programming notes, note

Source: Internet
Author: User

>>> Logfile = open ('/tmp/mylog.txt', 'A ')

>>> Print> logfile, 'fatail error: invalid arguments'

>>> Logfile. Close ()

>>>

User = raw_input ("Please input your name :")

Numbers:

Int

Long

Bool(True, false)

Float

Complex(6.23 + 1.5j)

Tuple

1. tuples and arrays (linear structures, which can be indexed using integers)

Lists [] and tuples ():

Hold any arbitrary number of arbitrary Python objects, the items are ordered

And accessed via index offsets, similar to arrays, doesn't that lists and tuples can store

Different types of objects.

Tuples can be thought of for now as "read-only" lists.

Subsets can be taken with the slice operator ([] and [:]) in the same manner as strings.

Dictionaries

Dictionaries (or "dicts for short) are Python's mapping type and work like associative arrays

Or hashes found in Perl.

>>> Adict = {'host': 'global '}

>>> Adict

{'Host': 'global '}

>>> Adict ['Port'] = 8080

>>> Adict

{'Host': 'earth', 'Port': 8080}

>>> Adict. Keys ()

['Host', 'Port']

>>> For key in adict:

... Print key, adict [Key]

...

Host Earth

Port 8080

If expression1:

If_suite

Elif expression2:

Elif_suite

Else:

Else_suite

>>> While CNT <3:

... Print 'loop % d' % (CNT)

... CNT + = 1

...

Loop 0

Loop 1

Loop 2

>>> For item in range (10 ):

... Print item

>>> For item in ['e-mail ', 'Net-surfing', 'homework']:

... Print item

...

Email

Net-surfing

Homework

List comprehensions:

>>> Squared = [x ** 2 for X in range (4)]

>>> Squared

[0, 1, 4, 9]

>>> For I in [x ** 2 for X in range (6)]:

... Print I

...

0

1

4

9

16

25

Useful built-in functions for new Python programmers

Dir ([OBJ]), help ([OBJ]), INT (OBJ), Len (OBJ), open (FN, mode), range ([start,] Stop [, step]),

Rraw_input (STR), STR (OBJ), type (OBJ)

Typical Python file structure

 
#! /Usr/bin/ENV Python (1) startup line (UNIX) "This is a test module" (2) module documentationimport sysimport OS (3) module importsdebug = true (4) (global) variable declarationsclass fooclass (object): "foo class" Pass (5) Class declarations (if any) def test (): "Test function" foo = fooclass () if debug: Print 'run test () '(6) funcation declarations (if any) If _ name _ =' _ main _ ': Test ();

P 115/1155

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.