目录
python介绍
1.介绍
1.1第一个程序
1.2注释
2.变量(Variables)
3.字符串(Strings)
4.数据结构(Data structures)
5.条件表达式(Condition expressions)
6.循环(Loops)
7.函数(Functions)
8.类与对象(Classes and objects)
9.模块和包(Modules and packages)
10.文件输入/输出(File input/output)
1.介绍
1.1第一个程序hello_world.py
print("Hello, world! My name is dudu")
1.2注释(comments)
# This is the comment for the comments.py file
print("Hello!") # this comment is for the second line
print("# this is not a comment")
# new comment
2.变量
常用的数字数据类型(Common used numeric data types)
Name | Notation | Declaration e.g. |
---|---|---|
Integers | int | a = 10 |
Floating | float | b = 3.14 |
Complex | complex | c = 1 + 2j |
String | str | d = 'Python' |
- 备注:
- 在Python 2.7中,int与另一个int运算将导致int结果。 但是,一个浮点运算与int会导致浮点数。
- 在Python 3.x中,int与另一个int运算将导致浮点数。
2.1定义变量
a = b = 2 # This is called a "chained assignment". It assigns the value 2 to variables "a" and "b".
print("a = " + str(a)) # We'll explain the expression str(a) later in the course. For now it is used to convert the variable "a" to a string.
print("b = " + str(b))
greetings = "greetings"
print("greetings = " + str(greetings))
greetings = 2333
print("greetings = " + str(greetings))
2.2未定义的变量
variable = 1
print(other_variable)
2.3变量类型
number = 9
print(type(number)) # print type of variable "number"
float_number = 9.0
print(type(float_number))
2.4变量类型转变
number = 9
print(type(number)) # print type of variable "number"
float_number = 9.0
print(float_number)
print(int(float_number))
2.5算术运算符(arithmetic operator)
number = 9.0 # float number
result = number / 2
remainder = number % 2
print("result = " + str(result))
print("remainder = " + str(remainder))
2.6赋值(Assignments)
number = 9.0
print("number = " + str(number))
number -= 2
print("number = " + str(number))
number += 5
print("number = " + str(number))
2.7逻辑算子/布尔操作符(boolean oper