Python Second play Python basics

Source: Internet
Author: User

Python Basics
1. Statements and syntax
# #: The string that follows is a Python comment
\ nthe line break is the standard delimiter
\ (backslash) to continue on line
;(semicolon) Links two statements to a single row, allowing multiple statements to be written on the same line, separated by semicolons, and these statements cannot start a new block of code on this line, reducing the readability of the program.
:(colon) separates the header and body of the block, such as if, while, Def, class, and so on, the first line begins with the keyword and ends with a colon:
Statements (blocks of code) are implemented in an indented manner
Different indentation depths represent different blocks of code, and Python uses indentation to split the code groups. The hierarchical relationship of code is manifested by the same depth of space or tab indentation.
Python files are in the form of modules

2. Module
The Python script file can be used as a module and the module exists as a disk file. The code in the module can be either a script executed or a bunch of code similar to a function, which can be called by another module import (import).

3. Assigning values to variables
In the Python language, the equals sign = is the primary assignment operator.

Increment assignment
+=
-=
*=
/=
%=
**=
<<=
>>=
&=
^=
|=

Multiple assignments
x=y=z=

Multi-value Assignment
x,y,z=1,2, ' PYTHONCN '

4. Identifiers
A valid Python identifier:
The first character must be a letter or an underscore
The remaining characters can be letters and numbers or underscores
Case sensitive

Key words

Built

5.python Structure and layout
__author__ = ' Xudongqi '
"This is a test module"

Import Sys
Import OS

debug = True

Class Fooclass (object):
"Foo class"

Pass

def test ():
"Test Function"
foo = Fooclass ()
If debug:
print ' Run test () '

if __name__ = = ' __main__ ':
Test ()

Focus:
If the module is imported, the value of __name__ is the module name
If the module is executed directly, the value of __name__ is ' __main__ '

Create a file Python script
#!/usr/bin/env python
#-*-Coding:utf-8-*-
' maketextfile.py--Create text file '

Import OS
ls = Os.linesep #为os. Linesep took an alias.

# get filename

While True:
fname = raw_input (' Enter file name: ') #需要添加的语句 and needs to be indented, and the following four statements also need to be indented
If Os.path.exists (fname):
Print "ERROR: '%s ' already exists"% fname
Else
Break

# Get file content (text) lines
all = []
Print "\nenter lines ('. ' by itself to quit). \ n"

#loop until user terminates input
While True:
Entry = Raw_input (' > ')
If entry = = '. ':
Break
Else
All.append (Entry)

# write lines to file with proper line-ending
Fobj = open (fname, ' W ')
Fobj.writelines (['%s%s '% (x, LS) for X "all])
Fobj.close ()
print ' done! '


File Read and display
#/usr/bin/env python
#-*-Coding:utf-8-*-

#get filename
filename = raw_input (' Enter filename: ')
Print # Printing Blank lines
#attempt to open file for reading
Try
fobj = open (filename, ' R ')
Except Ioerror,e:
print ' * * * File Open error: ', E
Else
For Eachline in Fobj:
Print Eachline
Fobj.close ()





Python Second play Python basics

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.