python基础知识整理1

语言类型:

一:解释型:从上到下一行一行解释转换成二进制

      优点:开发效率快,能跨平台。

       缺点:执行速度慢

       编译型:从上到下一次性全部编译转换成二进制

      缺点:开发效率慢,不能跨平台。

      优点点:执行速度快

二:python种类

      cpython  jpython  其他语言python

三:python版本(2.7/3)

python2和python3区别:

一:python2中大量的重复代码,和其他语言陋习。

       python3简单,优美,清晰

二:python2打印不用加括号,python3打印必须加括号

四:变量:

规则:1变量由数字,字母,下划线任意组合

          2 不能以数字开头

          3 不能是python中关键字

          4 变量具有可描述性

          5 不能是中文

          6 驼峰体和下划线

          7 变量名称不要太长

五:常量:一直不会变的就是常量,例如建国日期,节日

六:用户交互:input

三次登陆:

count=0
while count<3:
    count = count + 1
    name = input('>>>请输入您的用户名')
    password = input('>>>请输入您的密码')
    if name=='zhujun'and password=='12345678':
        print ('登陆成功')
        break
    else:
        print('请重新输入')

七:注释

单行注释:#

多行注释:‘’‘’或“”“”

八:if条件语句

if 条件:

        结果

if和else搭配

if/elif/elif/else

九:while循环

小练习:

1、使用while循环输入 1 2 3 4 5 6     8 9 10
count=0
while count<10:
    count=count+1
    if count==7:
        print('')
        continue
    else:print(count)

2、求1-100的所有数的和
count=1
sum=0
while count<101:
    sum=sum+count
    count=count+1
print(sum)
3、输出 1-100 内的所有奇数
count=0
while count<101:
    count=count+1
    if count % 2 == 1:
        print(count)
4、输出 1-100 内的所有偶数
count = 0
while count < 101:
    count = count + 1
    if count % 2 == 0:
        print(count)

5、求1-2+3-4+5 ... 99的所有数的和
第一种:
count=1
sum=0
while count<100:
    if count%2==0:
        sum=sum-count
        count = count + 1
    else:
        sum=sum+count
        count = count + 1
print(sum)

#第二种:
sum=0
for i in range(1,100):
    if i %2==0:
        sum=sum-i
    else:
        i % 2 == 1
        sum = sum + i
print(sum)
1到100所有奇数偶数的和
print(sum([i for i in range(101) if i%2==0]))
print(sum([i for i in range(101) if i%2==1]))

猜你喜欢

转载自my.oschina.net/u/3648651/blog/1802353