Python成长之路 第一篇 《Python基础》

1.python文件命名

         - 后缀名可以是任意的,但为规范便于识别,后缀名应为 .py

2.两种执行方式

     python解释器   py文件路径      python   进入解释器: 实时输入并获取到执行结果

3.解释器路径

         在Linux系统中应添加  #!/user/bin/env python    , windows系统中可不添加

4.编码

         # -*- coding:utf8 -*-  (在python3中可不加,python只要出现中文头部必须加)

         ascill  只能编译英文

         unicode 万国编码,至少2个字节

         utf-8   能用多少字节表示就用多少表示

5.变量名

         由字母,数字,下划线组成

         PS:  数字不能开头,不能是关键字,最好不和python内置的东西重复

6.条件语句

          缩进用4个空格

          a.  input : 永远等待,知道用户输入了值,就会将输入的值赋值给一个东西

          b.  n1 = “liu”  单等于号是赋值

               n1 ==“liu”  双等号是比较

          c.  if 条件1:

                        pass

               elif 条件2:

                        pass

               elif 条件3:

                        pass

                print("end")

                每个条件依次判断

             d. and 表示和,or 表示或

                 if n1 =="liu" and n2 =="xue"

                            print("OK")

                  else:

                             print("OK")

              PS :  pass代指空代码,无意义,表示过

7.基本数据类型

              字符串: n1 = "liu"   n2 = "xue"  引号之中的东西称为字符串

              数字: age=19       weight=60    height=178

              算法:

                    字符串: 

                                加法:

                                          n1 = "liu"   n2 = "xue"    n3 = n1+n2

                                          #n3=="liuxue"

                                 乘法:

                                           n1 = 1    n2 = n1*10

                      数字:

                                           n1 = 5     n2 = 2

                                           n3 = n1+n2

                                           n3 = n1-n2

                                           n3 = n1*n2

                                           n3 = n1 / n2

                                           n3 = n1 % n2  (取余)

                                           n3 = n1 ** n2 (乘方)

8.循环

          死循环

          while 1==1:

                   print("OK")

9.练习题

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

                 

count = 1
while count <= 10 :
    num = count
    if count == 7 :
        pass
    else:
        print(num)
    count += 1

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

     

n = 1
s = 0
while n <= 100:
    s = s + n
    n += 1
print(s)

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

count = 1
while count <= 100 :
    if count%2 == 0:
        pass
    else:
        print(count)
    count += 1

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

count = 1
while count <= 100 :
    if count%2 == 0:
        print(count)
    else:
        pass
    count += 1

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

n = 1
s = 0
while n < 100:
    num = n % 2
    if num == 0:
        s = s - n
    else:
        s = s + n
    n = n + 1

print(s)

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

_username = 'Liu'
_password = 'Xyt'
count = 0
while count < 3:

username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("欢迎登陆")
break
else:
print("用户名密码错误!")
count += 1

                                          

    

原文地址:https://www.cnblogs.com/xyt521/p/6087383.html