您的位置:首页 > 编程语言 > Python开发

Python入门——A Byte of Python

2016-01-26 14:57 701 查看
I started my Python journey using the introduction book
<A bite of Python>, which is a great book for Python beginners. Note: this book is written for Python 2.

1. What is Python

Python is an object-oriented, open-source, high-level programming language.

2. Why is Python

Python is small and powerful. Programming in Python is easy and fun.

3. Installation

(1) Download Python on https://www.python.org/downloads, and install it like general software.

(2) Set environment variables: add C:\Python27 to PATH.

(3) Type python in cmd line for validation.

4. First Python Program - Hello World!

4.1 PyCharm

PyCharm Educational Edition is a free software that can be used by beginners to write Python source files. Download and install it like general software.

4.2 hello.py

Type the following Python statement, and run it. (Either directly in PyCharm, or in the command line python hello.py.)
print "Hello World!"

5. Python Basics

5.1 Comments

Comments are anything written to the right of #.
#This is a type of comments.
print "Hello World!" #This is another type of comments.

5.2 Literal Constants Vs Variables

A Literal Constant can be a number like 2, 3.3, or a String like "I'm a String!". 
A variable can hold values of different types. The basic data types are numbers and Strings, later we will learn how to create our own data types using classes.

5.3 Numbers

Numbers are mainly of two types - integers and floats. Note: there are no long type, the int type can be an integer of any size.

5.4 Strings

(1) Single quotes works the same way as double quotes. Triple quotes is useful when there is multiple lines or single/double quotes included. 
Note: Strings are immutable, which means once you created a String, you cannot change it.
print '''This is the first line.
This is the second line.
"What is your name?" I asked.
He said "Bond. James Bond."
'''
(2) The Format Method for String:
age = 20
name = 'Smith'
print "{} is {} years old".format(name, age)
(3) Escape Sequences
print "She asked: \'What is your name?\'" #use \' to show ' in a sentence
print "This is a first line.\nThis is a second line." #use \n to start a new line
print "This a first line.\
This line is followed by."#use \ to join two lines when it is too long.

5.5 Logical & Physical lines & Indentation

A good practice of writing Python is never using the semicolon. There is only one exception: when the line is two long you can use \ to join two lines.
Note: white space at the beginning of a line is very important! When you need to indentation, use four spaces.

6. Operators and Expressions

6.1 Operators

+ -
* ** /%<>
<= >===!=NOT
ANDOR
<<: left shift, shift bits of the number to the left by the number of bits specified, e.g. 2<<2=8, 2 in bits is 10, shift 2 bits to the left is 1000, which is decimal 8.
>>: right shift, e.g. 11>>1, 11 in bits is 1101, right shift by 1 bit is 101, which is decimal 5.
& | ^ ~: bit wise and/or/xor/inversion

6.2 Evaluation Order

Don't have to remember that load of crap, always always use parentheses to explicitly specify the order.

7. Control Flow

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: