• Python初识一


    一、Python的第一个程序

    打开notepad++,新建一个.py文件,输入print('Hello World!'),保存

         

    print('Hello World!')

    开始—>运行——>输入cmd——>输入python 空格 文件路径—>回车执行

    二、变量

    2.1、变量定义:变量就是把程序运行的中间结果临时存在内存里,以便后续的代码调用。

    2.2、声明一个变量:

    name = 'Tom'

    name为变量名,Tom为变量

    2.3、变量的命名规则:

      1、变量名只能包含 字母、数字、下划线;

      2、变量名不能以 数字 开头;

      3、变量名不能是 关键字;

        关键字:['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']/

    2.4、变量名命名推荐方式:

      1、可描述性: 让读代码的人能容易理解变量名所代表的含义

           name age 能让人想到 名字和年龄        a b  c  不知所云

      2、易读性:当变量名较长时,推荐用驼峰体和下划线

         

    1 NumStudents = 50  #驼峰体
    2 Num_Students = 50  #下划线隔开
    3 numstudents = 50   #没有格式,连在一起的效果

     3、不要用中文,不要用拼音,不要太长

    三、常量

    d常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量

    python中常量全部用大写表示   如:身份证编号     ID = ‘123456789’          中国建国时间   BIR_OF_CHINA= 1949

    四、注释

    4.1、单行注释: 代码前输入 # 符号

    4.2、多行注释:单引号'''    '''   或   """       """

     1 # print('hello kitty')       单行注释
     2 '''
     3 name = 'tom'
     4 age = 18                         单引号
     5 job =' teacher' 
     6 '''
     7 """
     8 name = 'tom'
     9 age = 18                         双引号
    10 job =' teacher' 
    11 """

    五、基本数据类型

    5.1、int (整数类型)

    32位机器上,整数的位数是32位,取值范围-2**31~2**31-1,即-2147483648~2147483647

    64位机器上,整数的位数是64位,取值范围-2**63~2**63-1,即-9223372036854775808~9223372036854775807

    整型能进行 + - * / % 等运算

    python 2.2后没有 long(长整型),都是整型int

    5.2、str   (字符串类型)

    1 ta="hello world!"  

    Python 里面所有加引号的,都是字符串

    字符串拼接:

     1 相加
     2 s1 = "abc"
     3 s2 = "xyz"
     4 s3 = s1 + s2
     5 print(s3)
     6 
     7 乘法
     8 s1 = "abc"
     9 s2 = s1 * 3
    10 print(s2)

     str:简单少量的存储数据,并进行相应的操作

    5.3、bool  (布尔值)

    True(真) 或 False(假)

    1 或 0

    5.4、列表 list

    list_demo = [1,2,3,4,5]

    5.5、元祖 tupe

    tupe_demo = (1,)    #一个元素,要加逗号

    tupe_demo1 = (1,2,3)  

    5.6、字典 dict

    dic_demo = {'Name' : 'Tom','Age' : 18}

    六、用户交互

    input()        Python 2 里面的是 raw_input()

    1 username = input("请输入用户名")
    2 psw = input("请输入密码")
    3 print(username,psw)

    七、格式化输出

    格式化输出是通过print()等函数向指定地方输入指定的内容

    占位符 %       (%s 字符串  %d  整型 )

     1 name = input('请输入你的名字:')
     2 age = input('请输入你的年龄:')
     3 job = input('请输入你的工作:')
     4 hobbie = input('请输入你的爱好:')
     5 
     6 msg = '''
     7 --------------info of %s ---------------
     8 Name:%s
     9 Age:%d 
    10 Job:%s 
    11 Hobbie:%s
    12 ----------------------------------------
    13 '''%(name,name,int(age),job,hobbie)
    14 print(msg)

    1 #因为%被占位符用了,所以表示百分之多少的时候用两个百分号  1%%
    2 
    3 msg = "我的名字是%s,我的身高是%d,我的学习进度是50%%"%(tom,1804 print(msg)

    八、条件语句 if 

    if 条件:

      只有条件为真,执行

    else:只有条件为假,执行

    说白点就是2选1

    if 1 == 1:
        print("True")
    else:print("False")
    
    if 1>2:
        print("True")
    else:print("False")
    score = int(input("请输入您的成绩:"))
    if score > 100:
        print("我去!一共才100分")
    elif score > 90:
        print("A")
    elif score > 80:
        print("B")
    elif score > 60:
        print("C")
    elif score > 40:
        print("D")
    else:
        print("你太low了...E")

    九、while循环

    while 条件:

      循环判断条件真假,为真执行再循环,为假则跳出循环

    count = 1
    while count < 11:
        print(count)
        count += 1

    十、跳出while循环

    跳出while循环

    break; continue

    break跳出整个循环,while就不执行了;

    continue是跳出本次循环并进入下次while循环

    print("hello")
    while True:
        print("好好学习")
        print("天天向上")
        break
    print("none")
    1 count = 0
    2 while count < 10:
    3     
    4     count += 1
    5     if count == 5:
    6         continue
    7     else:print(count)

    while else

    只有while正常循环完毕后,才会执行else

     当while 循环被breake 打断,不会执行else

    1 count = 0
    2 while True:
    3     count += 1
    4     print("Loop",count)
    5     if count == 3:
    6         break
    7 else:print("循环正常执行完了!")
    8 print("----out of while loop----")
    9         
    count = 0
    flag = True
    while flag:
        count += 1
        print("Loop",count)
        if count ==3:
            flag = False
    else:print("循环正常执行完了!")
    print("----out ot while Loop----")

     Demo:

    用户登录(三次机会)并且每次输错时显示剩余登录机会(用到字符串格式化。)
     1 username = 'boy'
     2 password = '123'
     3 count = 0
     4 while count < 3:
     5     name = input('username:')
     6     pwd = input('password:')
     7     if name == username and pwd == password:
     8         print('Login Successfully!')
     9         break
    10     else:
    11         print("It's wrong! You have %d more chances."%(2-count))
    12         if count == 2:
    13             choices = input('Try again? Yes:')
    14             if choices == 'Yes':
    15                 count = -1
    16             else:break
    17         count += 1

    如果再多给3次机会

     1 username = 'boy'
     2 password = '123'
     3 count = 0
     4 n = 0
     5 while count < 3:
     6     name = input('username:')
     7     pwd = input('password:')
     8     if name == username and pwd == password:
     9         print('Login Successfully!')
    10         break
    11     else:
    12         print("It's wrong! You have %d more chances."%(2-count))
    13         if count == 2:
    14             choices = input('Try again? Yes:')
    15             if choices == 'Yes':
    16                 count = -1
    17             else:break
    18         count += 1
    19         n += 1
    20         if n == 6:
    21             print('傻吊你试了6次了')
    22             break
  • 相关阅读:
    对double数据类型的数据保留两位小数,并且进行四舍五入
    div位置设置
    每天一算法 -- (排序算法总结)
    SQL行转列
    设计模式的六大原则
    每天一算法 -- (插入排序)
    每天一算法 -- (选择排序)
    通用扩展函数--类型转换
    wcf和webservice
    Cookie的介绍及使用
  • 原文地址:https://www.cnblogs.com/zhzhlong/p/7690004.html
Copyright © 2020-2023  润新知