接上一篇文章 Python速成-基础语法
去下一篇文章 Python速成-数学、时间、字符串内置函数
注意!注意!我这个是速成笔记!
如果要看详细的教程,建议去网上找视频看
一、运算符(我只讲一些主要的,毕竟速成)
1.1逻辑运算符
这个就和java写的不一样,python使用的是 and or not
if a and b :
print " a 和 b 都为 true"
else:
print "a 和 b 有一个不为 true"
if a or b :
print "a 和 b 都为 true,或其中一个变量为 true"
else:
print "a 和 b 都不为 true"
if not( a and b ):
print "a 和 b 都为 false,或其中一个变量为 false"
else:
print "a 和 b 都为 true"
1.2身份运算符
python使用的是 is 和 is not
a = 20
b = 20
if ( a is b ):
print "a 和 b 有相同的标识"
else:
print "a 和 b 没有相同的标识"
if ( a is not b ):
print "a 和 b 没有相同的标识"
else:
print "a 和 b 有相同的标识"
1.3位运算符
1.4成员运算符
使用的是in 和 not in
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "a 在给定的列表中 list 中"
else:
print "a 不在给定的列表中 list 中"
if ( b not in list ):
print "b 不在给定的列表中 list 中"
else:
print "b 在给定的列表中 list 中"
二、条件和循环语句(if,while,for速成)
python不使用类似于java的大括号 { } 或者(),利用缩进表达不同逻辑块
2.1 if 语句
# if语句,对于代码的缩进要求很高
flag = False
name = 'luren'
if name == 'luren':
flag = True
print( 'welcome boss,flag =',flag )
else:
print (name)
2.2 while 语句
#while语句,break和continue都是和java使用一样
count = 0
while (count < 5):
print( 'The count is:', count)
count = count + 1
#python中有while … else语句
count = 0
while count < 5:
print(count, " is less than 5")
count = count + 1
else:
print(count, " is more than 5")
2.3 for 语句
# 循环遍历字符串
for i in 'Python':
print("当前字母: " +i)
# 根据值循环遍历列表
fruits = ['banana', 'apple', 'mango']
print("总共有", len(fruits), "种水果")
for fruit in fruits:
print('当前水果: ' + fruit)
# 根据下标循环遍历列表
for index in range(3):
print(fruits[index])
# 根据下标循环遍历
count = 1;
for i in range(7):
print(count);
count = count + 1;