码迷,mamicode.com
首页 > 编程语言 > 详细

python基础学习1

时间:2019-10-12 22:25:21      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:please   条件   简单的   with   span   style   pre   通过   数字   

Python基础学习1

字符串表达形式共四种:

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)

 

python基础学习1

标签:please   条件   简单的   with   span   style   pre   通过   数字   

原文地址:https://www.cnblogs.com/yangjingxuan/p/11664162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!