Python 基础

基础

  1,python后缀名称可以是任意,建议使用.py作为后缀

  2,python执行方式

    1) python解释器 + python 文件

    2) python 进入解释器: 实时输入并获取到执行结果

  3,解析器路径

    #!/usr/bin/env python

  4,编码

    # -*- coding:utf8 -*-

    unicode 使用两个字节表示中文信息

    utf-8 能用几个字节就用几个字节表示字符信息

    utf-8 使用三个字节表示中文

    Python3 无需关注字符编码, Python2 必须指定编码格式

  5,执行一个输入操作

    

  6, 注释

      单行注释使用 #, 多行注释使用 """(三个引号)

  7,条件语句

   使用4个空格或者制表符来缩进代码段

   a.

n1 = input(">>>")
if "alex" == n1:
    n2 = input(">>>")
    if n2 == "确认":
        print("alex shit")
    else:
        print("alex hello")
else:
    print("error")

 

  b.

    

if 条件1:
    pass // 代指空代码, 无意义, 仅仅用于表示代码块
elif 条件2:
    pass
else:
    pass

print('end')

 

  c. 条件 逻辑操作 and 和 or  

if n1 == "alex" or n2 == "alex123":
    print("ok")
else:
    print("no")

  7,基本数据类型

    字符串(用引号引起来) - n1 = "root" n2 = "eric"

    数字 - age = 12, weight = 64 fight = 5

    加减乘除:

      字符串: 加法 乘法

n1 = "alex"
n2 = "shit"
n3 = "ok"

n4 = n1 + n2 + n3
n5 = n1 * 10
print(n4)
print(n5)

        数字: 加 减 乘 除 取余 取幂 取商

n1 = 9
n2 = 2
print("n1 + n2 = ", n1 + n2) #
print("n1 - n2 = ",n1 - n2) #
print("n1 * n2 = ",n1 * n2) #
print("n1 / n2 = ",n1 / n2) #
print("n1 % n2 = ",n1 % n2) # 取余
print("n1 ** n2 = ",n1 ** n2) # 取幂
print("n1 // n2 = ",n1//n2) # 取商

  8, 循环

  死循环

while 条件:
    print('ok')

  9, 练习题

    if 条件语句,while 循环, 奇数偶数

      1、使用while循环输出 1 2 3 4 5 6 8 9 10

n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n = n + 1
print(' --- end --- ')

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

n = 1
s = 0

while n < 101:
    s = s + n
    n = n + 1
    
print(s)

        3、输出 1 - 100 所有奇数

      

n = 1
while n < 101:
    temp = n %2
    if temp == 0:
        pass
    else:
        print(n)
    n = n + 1

        4、求 1-2+3-4+5-6+7...99的所有数的和

      

n = 1
sum = 0
while n < 100:
    temp = n%2
    if temp == 0:
        sum = sum + n
    else:
        sum = sum - n
    n = n + 1
print(sum)

        5、用户登陆(三次重试机会)

          

n = 3
while n > 0:
n = n - 1
temp = input("please input your passwd")
if temp == "hello":
n = 0
print("welcome your login")
else:
print("error")
else:
print('finished')

 

 

          

posted on 2018-05-15 10:32  牛大黑  阅读(133)  评论(0编辑  收藏  举报

导航