python学习1-字符串数字基本运算以及if条件和while循环
字符串表达形式共四种:
name = "string" name = 'string' name = """string""" name = '''string'''
数字基本运算方式:
a = 39 b = 4 c = a + b c = a - b c = a*b c= a**b #次幂 c = a/b c = a%b #取余数 c = a//b #取除数
条件判断:
in1 = input('please input the rank:') print(in1) if in1 == '1': print('hello world!') elif in1 == '2': print('HELLO WORLD!') elif in1 == '3': print('hello world 3') else: print('hello world 4')
上面代码中,通过input输入的数字并没有被python视为数字,而是视为了字符串,故下面在条件判断时需要使用引号将数字引起。
循环判断:
while count < 10: print(count) count = count + 1
使用循环完成简单的算法:
# output numbers: 1 to 10 without 7 n = 1 while n < 11: if n != 7: print(n) n = n + 1 print('---end---') # find sum of numbers from 1 to 100 n = 1 sumofn = 0 while n < 101: sumofn = sumofn + n n = n + 1 print(sumofn) # find sum of numbers 1-2+3-4+...-100 n = 1 sumofnn = 0 while n < 101: if n%2 == 0: sumofnn = sumofnn - n else: sumofnn = sumofnn + n n = n + 1 print(sumofnn)