Python 基础中 变量、分支结构、循环结构 及相关例子

概述

1.python是一种解释型的语言,解释性语言的的优点:轻松愉快的执行跨平台,平台可移植性,执行效率低下
2.今天当计算机硬件已经是足够开发的时候,我们追求的更多的并不是程序的执行效率而是开发效率

说明 如果要在Linux更新到python 3.x版本需要源代码构建(两个*是加粗字体为斜体*)

3.可以从python的官方网站下载python的安装和文件
4.我们可以使用包管理工具pip来安装第三方模块

pip install ipython jupyter

python -m pip install ipyhon jupyter
  1. 第一个Python 程序—hello,world
"""
第一个Python程序
Verson:0.5
Author:卢芹
Date:2017-12-12
Modifier:王大锤
Date:2018-1-31
"""
# 使用了python内置的print函数打印字符串
print('hello ,world')
  • 如果想用交互式环境开发进行Python开发那么可以使用ipthon 或 juopyter的notebook项目

jupyter notebook
  • 如果要做团队开发以及需要使用多文件多模块写作的大型项目,我们推荐使用Pythoncharm的集成开发环境,我们可以从JetBrains[官方网站:(http://jetbrains.com)]

变量

  1. 变量的作用:变量是一种存储数据的载体
  2. 变量的命名
    规则:
    (1)必须以数字,字母,下划线构成,数字不能开头,不能使用特殊字符;
    (2)大小写敏感;
    (3)不要关键字保留字冲突
  3. 变量的类型:
    (1)整型(int):Python中可以处理任意大小的整数,二进制、八进制、十进制、十六进制。
    (2)浮点型(float):浮点数也就是小数。
    (3)布尔型:布尔值只有True、False两种值,要么是True,要么是False。
    (4)复数型:形如3+5j,跟数学上的复数表示一样,唯一不同的是虚部的i换成了

    运算符

    赋值运算符:=
    算术运算符: + - * / // % **
    关系运算符; > >= < <= == !=
    逻辑运算符:and or not

分支结构

要构造分支结构可以使用if、elif和else关键字。
根据Python的缩进规则,如果if语句判断是True,就执行if条件下的语句,否则,什么也不做。也可以给if添加一个else语句,意思是,如果if判断是False,不要执行if的内容,去把else执行了,注意不要少写了冒号:。
if <条件判断1>:
<执行1>
elif <条件判断2>:
<执行2>
elif <条件判断3>:
<执行3>
else:
<执行4>

循环结构

在Python中构造循环结构有两种做法,一种是for-in循环,一种是while循环。
明确的知道循环执行的次数,使用for-in循环;如果要构造不知道具体循环次数的循环结构,使用while循环,while循环通过一个能够产生或转换出bool值的表达式来控制循环,表达式的值为True循环继续,表达式的值为False循环结束。

例题:

1.已知三角形的边长求他的面积和周长

"""
已知三角形的边长求他的面积和周长
Author:卢芹
Date:2017-3-3

"""
import math
a=float(input('a='))
b=float(input('b='))
c=float(input('c='))
if a+b>c and a+c>b and b+c>a:
    d=a+b+c 
    e=(a+b+c)/2 
    f=math.sqrt(e*(e-a)*(e-b)*(e-c))
    print('三角形的周长为:'+str(d))
    print('三角形的面积为:%f' % f)
else:
     print('三条变得长度不能构成三角形')
结果:
a=3
b=4
c=5
三角形的周长为:12.0
三角形的面积为:6.000000

2.猜数字游戏

"""

猜数字游戏(电脑给数字人猜)
Author:卢芹
Date:2017-3-3

"""
from random import randint
you_answer=int(input('请输入:'))
counter=0
x=1
y=100
is_correct=False
while not is_correct:
    counter+=1
    answer=randint(x,y)
    print(answer)
    if answer>you_answer:
        print('小一点')
        y=answer
    elif answer<you_answer:
        print('大一点')
        x=answer
    else:
        is_correct=True
print('恭喜你答对了')
if counter>7:
       print('智商捉急')  
结果:
请输入:56
大一点
请输入:77
小一点
请输入:68
大一点
请输入:74
小一点
请输入:71
小一点
请输入:70
恭喜你答对了

3.工资计算

"""
工资计算
Author:卢芹
Date:2017-3-3

"""
salary=float(input('请输入工资:'))
insurance=float(input('五险一金:'))
diff=salary-insurance-3500
if diff<=0:
    tax=0
    deduction=0
elif diff<=1500:
    tax=0.03
    deduction=0
elif diff<=4500:
    tax=0.1
    deduction=105
elif diff<=9000:
    tax=0.2
    deduction=555
elif diff<=35000:
    tax=0.25
    deduction=1005
elif diff<=55000:
    deduction=2755
    tax=0.33
elif diff<=80000:
    tax=0.35
    deduction=5505
else:
    tax=0.45
    deduction=13505
personal=diff*tax-deduction
print('个人所得税:¥%.2f'%(personal))
print('实际工资:¥%.2f'%(salary-insurance-personal))

结果:
请输入工资:25888
五险一金:555
个人所得税:¥4453.25
实际工资:¥20879.75

4.输入三个数,输出其最大值

"""
输入三个数,输出其最大值
Author:卢芹
Date:2017-3-3

"""
a=int(input('a='))
b=int(input('b='))
c=int(input('c='))
my_max=a>b and a or b
my_max=c>my_max and c or my_max
print(my_max)    
结果:
a=5
b=1
c=78
78

5.给输入的数排序

"""
给输入的数进行从小到大的排序
Author:卢芹
Date:2018-3-3
"""
a=int(input('a='))
b=int(input('b='))
c=int(input('c='))
(a,b)=a>b and (b,a )or (a,b)
(b,c)=b>c and ( c,b) or (b,c)
(a,b)=a>b and (b,a )or (a,b)
print(a,b,c)    
结果:
a=5
b=25
c=66
5 25 66

6.输出2-100内的素数

"""
输出2-100内的素数
Author:卢芹
Date:2018-3-3
"""
from math import sqrt
for num in range (2,101):
    is_prime=True
    for rea in range (2,int(sqrt(num))+1):
        if num%rea==0:
            is_prime=False
            break
    if  is_prime:
        print(num,end='\t')
结果:
2   3   5   7   11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97            

7.打印乘法口诀表

"""Python
打印乘法口诀表
Author:卢芹
Date:2018-3-3
"""
for row in range(1,10):
    for col in range (1,row+1):
        print('%d*%d=%d' % (row,col,row*col),end='\t')
    print()
结果:
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12  4*4=16  
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  
9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  

8.打印各种形状图

"""Python
打印各种形状图
Author:卢芹
Date:2018-3-3
"""
row = int(input('请输入行数: '))
for row in range(1,row):
    for col in range (1,row+1):
        print( (col),end='')
    print()

for i in range(row):
    for _ in range(i + 1):
        print('*', end='')
    print()

for row in range(1,6):
    for col in range(65,65+row):
        print(chr(col),end='')
    print()

for row in range(0,6):
    for col in range(0,row):
        print(chr(row+64),end='')
    print()

for i in range(row):
    for j in range(row):
        if j < row - i - 1:
            print(' ', end='')
        else:
            print('*', end='')
    print()

for i in range(row):
    for _ in range(row - i - 1):
        print(' ', end='')
    for _ in range(2 * i + 1):
        print('*', end='')
    print()  
结果:
请输入行数: 5
1
12
123
1234
*
**
***
****
A
AB
ABC
ABCD
ABCDE

A
BB
CCC
DDDD
EEEEE
    *
   **
  ***
 ****
*****
    *
   ***
  *****
 *******
********* 

9.应用题

"""Python
Author:卢芹
Date:2018-3-3
"""
"""
公鸡5元1只,母鸡3元1只,小鸡一元3只,100元买100只鸡,三种鸡各多少只
x+y+z=100
5*x+3*y+z//3=100
z%3==0
穷举法-穷尽所有的可能性找到真正的答案
"""
for x in range (21):
    for y in range(34):
        for z in range (0,100,3):
            if x+y+z==100 and 5*x+3*y+z//3==100 :
                print (x,y,z)

for x in range (21):
    for y in range(34):
        z=100-x-y
        if 5*x+3*y+z//3==100  and z%3==0:
             print (x,y,z)
结果:
0 25 75
4 18 78
8 11 81
12 4 84
0 25 75
4 18 78
8 11 81
12 4 84

10.打印100-1000内的水仙花数

"""Python
打印100-1000内的水仙花数
Author:卢芹
Date:2018-3-3
"""
for num in range(100,1000):
    ge=num%10
    shi=(num//10)%10
    bai=num//100
    total=ge**3+shi**3+bai**3
    #print(ge,shi,bai)
    if num==total:
        print(num)
结果:
153
370
371
407

11.1-10000之间的完美数

"""Python
1-10000之间的完美数
Author:卢芹
Date:2018-3-3
"""
import time 
from math import sqrt
start=time.time()
for num in range (2,10001):
    sum=0
    for rea in range (1,int (sqrt(num))+1):
        if num%rea==0:
            sum+=rea
            if rea!=1 and rea!=num//rea:
                sum+=num//rea
    if num==sum:
        print(num)
结果:
6
28
496
8128

12.综合应用

"""Python

Author:卢芹
Date:2018-3-3
"""
"""
A,B,C,D,E五个人捕鱼,不计其数,然后休息,
早上A第一个醒来,将鱼均分成五份,把多余的一条鱼扔掉,拿走自己的一份,
B第二个醒来,也将鱼均分为五份,把多余的一条鱼扔掉,拿走自己的一份。
CDE依次醒来,也按同样的方法拿鱼,问他们合伙至少捕了几条鱼。
"""
fish=1
while True:
    is_prime = True
    total = fish
    for _ in range(5):
        if (total - 1) % 5 == 0:
            total = ((total - 1) / 5) * 4 
        else:
            is_prime = False
            break
    if is_prime:
        print(fish)
        break
    fish+=1
结果:
3121

13.craps赌博游戏

"""

craps赌博游戏
Author:卢芹
Date:2017-3-3

"""
from random import randint
face1=randint(1,6)
face2=randint(1,6)
first_point=face1+face2
print('玩家摇出了:%d点'% first_point)
go_on=False
if first_point==7 or first_point==11:
    print('玩家胜')
elif first_point==2 or first_point==3 or first_point==12:
    print('庄家胜')
else:
    go_on=True
    while(go_on):
        face1=randint(1,6)
        face2=randint(1,6)
        current_point=face1+face2
        print('玩家摇出了:%d点'% current_point)
        if current_point==7:
            go_on=False
            print('庄家胜')
        elif current_point==first_point:
            go_on=False
            print('玩家胜')
结果:
玩家总资产:1000
请下注:500
玩家摇出了:7点
玩家胜
玩家总资产:1500
请下注:500
玩家摇出了:5点
玩家摇出了:4点
玩家摇出了:3点
玩家摇出了:11点
玩家摇出了:5点
玩家胜
玩家总资产:2000
请下注:2000
玩家摇出了:12点
庄家胜
你破产了

猜你喜欢

转载自blog.csdn.net/Lq_520/article/details/79428428