python入门 (一)

发布时间:2020-09-29 18:35:03 作者:兔子也会飞
来源:网络 阅读:262

什么是编程?

准备python环境

第一个Python程序

2.交互式环境使用场景---问题

3.脚本方式运行代码

练习:

print('Hello World'+'+'+ str(2))
print(2*3.1415926*10)
print(3.1415926*10*10)
print('100+2=',100 +2)
print('1-5 = ',1-5)
print('1*5 = ',1*5)
print('1/5 = ',1/5)

-》结果输出

Hello World+2
62.831852
314.15926
100+2= 102
1-5 =  -4
1*5 =  5
1/5 =  0.2

变量&语句

变量定义&赋值

a_pi = 3.1415926
ra = 10
print (2*a_pi*ra)
print(a_pi*ra*ra)

输出结果:
62.831852
314.15926

定义变量

pi=3.1415926
area = pi * radius ** 2

注:变量命名规则:

3. 数据类型

整数、浮点数、正数、负数

type结果:
<class 'int'>
<class 'float'>
<class 'str'>

* 类型相互转化
    - int/str => float
    - float/str => int
    - int/float => str
```python
print(type(int(1.9)))
print(type(int(2)))
print(type(float(1)))
print(type(str(1)))
print(type(str(1.8)))

类型输出结果
<class 'int'>
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>

4. 数字字符串、布尔类型

字符串类型
使用单引号、双引号、三个单引号或三个双引号引起来的一些字符
name = 'dxy'
job = "linux"
特殊字符
\ 转义符
\r 回车
\n 换行
\t tab键
\f 换页

print("i 'm dxy")
print('i\'m dxy')
print('a \nb \tc ')
print('a\\nb\\tc\\')
练习
name = str('dxy')
age = int('20')

input('please name and age->:')
print('My name is',name,'Im,',age,'years old')

5. 流程控制

6. 条件语句

#妻子的想法
momeny = 100
prompt = input('看到卖西瓜的了吗?(Y/N)')

if prompt =='Y':
    print('买一斤包子需要花费:10元')
    momeny -= 10
if prompt =='Y':
    print('买一个西瓜需要花费:20元')
    momeny -= 20
print('剩余金额'+ str(momeny))

#老公想法
momeny =100
prompt1 = input('看到卖西瓜的了吗?(Y/N)')

if prompt1 == 'Y':
    print('买一个包子需要:3元')
    momeny -= 3
else:
    print('买一斤包子需要:10元')
    momeny -= 10
print('剩余金额'+str(momeny))

7. 循环语句

根据表达式的真假控制代码的是否结束子语句循环执行,如果为真则继续循环执行

print(total)

练习
1. 循环提示用户在控制台上输入数字或者exit,当用户输入exit后结束程序,并打印所有输入数字的和与平均数
```python
total = 0
count = 0
input_number = ''

while input_number !='exit':
    input_number = input('请输入一个数字--:')
    if input_number != 'exit':
        total += float(input_number)
        count += 1

if count !=0:
    print('total',total,'avg',total/count)
else:
    print('total:', total, ', avg:', 0)

8. continue&break

break 跳出循环
continue 跳过本次循环,继续下一次循环条件判断

idx = 0
while idx <= 10:
    idx += 1
    if idx == 4:
        continue
    else:
        if idx ==9:
            break
    print(idx)
import random
num_random = random.randint(0,100)
count = 1
while True:
    input_num = int(input('游戏限制输入5次结束,请慎重输入>>'))
    if input_num ==num_random:
        print('高手!猜对了')
        break
    elif input_num > num_random:
        print('猜大了!!小伙伴')
    else:
        print('猜小了!!小伙伴')
    count =count+1
    if count > 2:
        print('太笨了,下次再来,正确的数字是',int(num_random))
        break
推荐阅读:
  1. 我的小程序入门笔记(一)目录结构
  2. OSPF网络入门级路由协议超详细介绍(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 基础总结 (一)

上一篇:用python3 返回鼠标位置的实现方法(带界面)

下一篇:详解spring boot应用启动原理分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》