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.
(2) Set environment variables: add C:\Python27 to PATH.
(3) Type python in cmd line for validation.
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.
Note: Strings are immutable, which means once you created a String, you cannot change it.
Note: white space at the beginning of a line is very important! When you need to indentation, use four spaces.
* ** /%<>
<= >===!=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
<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
相关文章推荐
- python全局变量
- Pillow-PIL的完全Fok替代库--完美解决报错The _imagingft C module is not installed的解决方案
- python调用R语言
- 景岁的Leetcode解题报告:297. Serialize and Deserialize Binary Tree(Python)
- Python正则表达式
- python文件操作小总结
- Python 6.2 调试
- 我开始学python了(5)
- python pip
- 自学python笔记
- [Leetcode]@python 80. Remove Duplicates from Sorted Array II
- Python 编码规范(Google)
- python读取剪切板内容
- [Leetcode]@python 79. Word Search
- Python正则表达式
- python基本图像操作
- Python 字符串的删除和替换
- ubuntu下python的虚拟环境设置
- Python模块安装: pip常见的使用方法
- Python 中的 classmethod 和 staticmethod 有什么具体用途?