变量、输入与输出
#交换
a = 1
b = 2
a , b = b , a
print("a的值:", a , "b的值:" , b)
#格式化输出
print(type(a))
hear_name = '德玛西亚'
level = 15
print('您选择的英雄是%s当前等级%d'%(hear_name,level))
#\n回车
print('您选择的英雄是{hear_name}\n当前等级{level}'.format(hear_name = hear_name,level = level))
#多变量赋值
name , age , sex = '的' , 12 , 'man'
print(name , age , sex
特殊运算符
1.取整数除 //
2.指数 **
数据类型转换
name = input('请输入')
print(type(name))
name = int(name)
print(type(name))
str = "1 + 10"
#可计算字符串中的表达式
res = eval(str)
print(str)
猜拳游戏
import random
player = input('请输入:剪刀(0)石头(1)布(2)')
player = int(player)
#生成0到2的随机整数
computer = random.randint(0,2)
if (player - computer == 1)or(player - computer == -2):
print('您获胜了')
elif (computer - player == 1)or(player - computer == -2):
print('您输了')
else:
print('平局')
输出九九乘法表
i = 1
while i <= 9:
j = 1
while j <= i:
print(j,"*",i,"=",i*j,' ',end='')
j += 1
print('\n')
i += 1
字符串常规操作
#访问字符串
word = 'hello,"c"'
#下标
print(word[2])
print(word*2)
#切片
#对象[起始:终止:步长] 不包括结束位置(左闭右开)
#步长的正负决定了取的方向:正右负左
name = "abcdefgh"
print(name[0:3])
print(name[0:-2])
print(name[0:])
print(name[::3])
print(name[5:1:-2])
print(name[::-1])(省略的值为[-1:-(length+1):-1])
#字符串常规操作
my_str = 'hello world neuedu and neueducpp'
#find() 检查str中是否包含在 my_str中,如果在返回开始的索引值,否则返回-1
index1 = my_str.find('neuedu')
print(index1)
#index() 和find()一样 只不过str不在my_str中要报异常
index2 = my_str.index('neuedu',0,20)
print(index2)
#count 返回 目标字符串出现的次数
index3 = my_str.count('n')
print(index3)
for循环循环
''''
for 循环
for 临时变量 in 可迭代的对象:
循环满足时要执行的事情
'''
'''
company = 'neusoft'
for x in company:
print(x)
if x == 's':
print('这个s要大写')
'''
#range(起始值,终止值,步长)
'''
for i in range(1,100,2):
print(i)
#break 结束循环
#continue 结束当前次数的循环,并进入下一循环
company = 'neusoft'
for x in company:
if x == 's':
#break
continue
print(x)
if循环
age = input('请输入年龄:')
age = int (age)
if age >= 18:
print('ajds')
if age == 20:
print('kjncsk')
elif age == 12:
print('sjhqakjsa')
else:
print('dkjh')
# elif 必须和 if 一起使用,else 在最后出现
if循环的简便写法
score = 79
score = '你的分数是77' if score ==77 else print(score)
#判断为真时执行if前的语句,否则执行else语句