常量、变量;基本数据类型;input()、if、while、break、continue

一、编译型语言和解释型语言区别:
编译型:一次性将所有程序编译成二进制文件
缺点:开发效率低,不能跨平台
优点:运行速度快。
例如:C,C++等
解释型:当程序执行时,一行一行的解释
优点:开发效率高,可以跨平台
缺点:运行速度慢
例如: python,php等

二. python是一门动态解释性的强类型定义语言

cmd运行py文件:python 文件路径
python2 python3 区别:python2默认编码方式是ascii码
                                      解决方式:在文件的首行:#-*- encoding:utf-8 -*-
                                      python3默认编码方式utf-8

 

变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
1. 必须由数字,字母,下划线任意组合,且不能数字开头。
2. 不能是python中的关键字:
and, as, assert, break, class, continue,
def, del,elif,else,except,exec,finally,for,
from,global,if,import,in,is,lambda,not,or,
pass,print,raise,return,try,while,with,yield
3. 变量具有可描述性。
4. 不要用中文命名变量。
常量:一直不变的量;例如:1、2、3、π等
BIR_OF_CHINA = 1949

注释:方便自己、他人理解代码。
单行注释:#
多行注释:'''被注释内容'''

      """被注释内容"""

数据类型

数字:int 12,3,45
+ - * / **
% 取余数

字符串:str,python当中凡是用引号括起来的都是字符串;可以是单/双引号
可相加:字符串的拼接
可相乘:str * int

bool:布尔值    True/False

数据类型:type()

print(1,type(1))
print('str',type('str'))
print(True,type(True))

字符串转化成数字:int(str) 条件:str必须是数字组成的
数字转化成字符串:str(int)

if:能够检查程序的当前状态,并据此采取相应的措施

while:使用while循环来数数

break:要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句

continue:要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环。

练习题:

1.使用while循环输入1 2 3 4 5 6 8 9 10

count = 0
while count <= 9 :
    count = count + 1
    if count == 7:    #第一次运行到这里时,count=1。if条件不成立,则print;若执行到count=7,if条件成立,接着执行continue:返回到while循环开头
        continue                    #if下面有continue则:if条件成立时才执行continue
    print(count)

2.求1-100的所有数的和

count = 1
sum = 1 
while count <= 99:
    count = count + 1
    sum = sum + count
print(sum)

3.输出1-100内的所有的奇数

count = 0
while count <= 99:
    count = count + 1
    if count % 2 == 0:
        continue
    print(count)
for i in range(1, 100):
    if i % 2 == 0:
        continue
    else:
        print(i)

4.输出1-100内的所有的偶数

count = 0
while count <= 99:
    count += 1
    if count % 2 == 0:
        print(count)
    else:
        continue
for i in range(1, 101):
    if i % 2 != 0:
        continue
    else:
        print(i)

5.求1-2+3-4+5...99等于多少

count = 1
s = 0
while count <= 99:
    x = count % 2
    if x == 0:
        s = s - count
    else:
        s = s + count
    count = count + 1
print(s)
sum = 0
for i in range(1, 100):
    if i % 2 != 0:
        sum = sum + i
        i = i + 1
    else:
        sum = sum - i
print(sum)

6.用户登录(两次重试机会)

i=0
while i<3:
    u=input("请输入账号:")
    p=int(input("请输入密码:"))
    if u=="pd" and p==123:
        print("登录成功")
        break
    else:
        print("登录失败请重新登录")
    i=i+1

 

posted @ 2018-08-23 20:01  就俗人一个  阅读(341)  评论(0编辑  收藏  举报