zoukankan      html  css  js  c++  java
  • 2-python基础

    1、第一个程序

      新建一个python文件,然后写上代码,运行即可

    1 print("hello world")

    2、变量

      变量就是存东西,供后面来用的

      变量名只能是 字母、数字或下划线的任意组合

      变量名的第一个字符不能是数字

    1 # 变量,存东西的
    2 name = "小黑"  # 字符串 str
    3 age = 18  # int
    4 score = 98.5  # float
    5 word = "单词"
    6 words = "let's go"

      以下关键字不能声明为变量名

    1 import keyword
    2 print(keyword.kwlist)
    1 ['False', 'None', 'True', 'and', 'as', 'assert', 'break',
    2  'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 
    3  'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 
    4  'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

    3、python中的单引号、双引号和三引号(就是三个单引号)

      python中定义变量的时候字符串都用引号括起来,单引号和双引号没有区别,如果说这个字符串里面有单引号的话,那外面就用双引号,里面有双引号的话,外面就用单引号,既有单又有双,那么用三引号,三引号也可以多行注释代码,单行注释

    1 words = "let's go"
    2 words2 = '他长的"真帅"啊'
    3 words3 = ''''他长的"真帅"啊'''

    4、输入输出

      输入:input函数

      输出:print函数

    1 name = input("请输入你的名字:")
    2 print("你输入的是%s" %name)

      input在接收输入的时候,是可以看到你输入的值的,如果是输入密码这样的呢,不想让别人看到你的密码,怎么办呢,就需要用到一个标准库,getpass,什么是标准库呢,就是不需要你再去安装,装完python就有的库,就是标准库,getpass就是一个标准库,导入进来之后,直接使用getpass.getpass方法就可以在输入的时候,不回显了,代码如下:

    1 # 导入getpass标准库
    2 import getpass
    3 
    4 # 定义一个变量passwd来接收输入的银行卡密码
    5 passwd = getpass.getpass("请输入你的银行卡密码:")
    6 print(passwd)

    5、条件判断

      python中条件判断使用if else来判断,多分支的话使用if elif ... else,也就是如果怎么怎么样就怎么怎么样,否则就怎么怎么样

      if else

    1 # 条件判断 if else
    2 # > < >= <= != ==
    3 age = 19
    4 if age >= 18:
    5     print("你已经成年了")
    6 else:
    7 print("未成年")

      if elif ... else

     1 # 使用input接收到的全部是字符串,需要使用int()强制转换成整形
     2 score = input("请输入你的分数:")
     3 score = int(score)
     4 
     5 # 也可以简写为
     6 # score = int(input("请输入你的分数:"))
     7 
     8 # 使用type()函数查看变量的类型
     9 print(type(score))
    10 
    11 if score >= 90:print("优秀")
    12 elif score >= 80 and score<90:
    13     print("良好")
    14 elif score >= 60 and score < 80:
    15     print("及格")
    16 else:
    17     print("不及格")

    6、使用input接收到的全部是字符串,需要使用int()强制转换成整形

      使用type()函数查看变量的类型
    1 # 使用input接收到的全部是字符串,需要使用int()强制转换成整形
    2 score = input("请输入你的分数:")
    3 score = int(score)
    4 
    5 # 也可以简写为  
    6 score = int(input("请输入你的分数:"))

     7 # 使用type()函数查看变量的类型
     8 print(type(score))

    7、字符串格式化

      占位符的使用,%s 字符 %d 整型 %f 小数

    1 import datetime
    2 username = input("请输入你的名字:")
    3 today = datetime.datetime.today()
    4 word = "欢迎"+username+"登录,"
    5 word2 = "欢迎 %s 登录,今天的日期是 %s" %(username,today)
    6 word3 = "你的年龄是 %3d 你的分数是%0.2f" %(18,92.3)
    7 print(word3)

    8、生成一个随机整数

    1 import  random
    2 # 产生一个1-100之间的数字
    3 num = random.randint(1,100)
    4 print(num)

    9、while循环

     1 import  random
     2 # 产生一个1-100之间的数字
     3 num = random.randint(1,100)
     4 print(num)
     5 
     6 count = 0
     7 while count <7:
     8 # 循环体,循环就是在重复执行循环体里面的代码
     9     guess = input("请输入你猜的数字:")
    10     guess = int(guess)
    11     if guess > num:
    12         print("猜大了")
    13     elif guess < num:
    14         print("猜小了")
    15     else:
    16         print("猜对了")
    17         break
    18     count = count + 1
    19 else:
    20     print("次数达到上限")
    1 count = 0
    2 while count < 20:
    3     if count%2 != 0:
    4         continue
    5         print(count)
    6     count = count + 1
    7 else:
    8     print("次数达到上线")  # while循环正常结束后运行

    10、for循环

     1 import  random
     2 # 产生一个1-100之间的数字
     3 num = random.randint(1,100)
     4 print(num)
     5 
     6 for i in range(7):
     7     guess = input("请输入你猜的数字:")
     8     guess = int(guess)
     9     if guess > num:
    10         print("猜大了")
    11     elif guess < num:
    12         print("猜小了")
    13     else:
    14         print("恭喜你,猜对了")
    15         break
    16 else:  # 正常结束循环后,执行else里面的代码
    17     print("次数达到上限")

    循环包含第一值,不包含第二个值

    1 for i in range(1,2):
    2     print("i的值是:",i)  #打印结果是:1

    11、换行

    1 print("abd",end = "")  # 默认换行
    2 print("bcd")
    3 print()  # 换行
    4 print("abc")

    12、九九乘法表

    1 for i in range(1,10):
    2     for j in range(1,i+1):
    3         print('%s * %s = %s  '%(j,i,i*j),end='')
    4     print(end='
    ')
  • 相关阅读:
    读写excel文件
    数据库操作
    django项目搭建
    django基础
    string
    random函数
    vue-typescript入门
    Visual Studio 2019配置vue项目
    js css+html实现简单的日历
    python接口自动化4-绕过验证码登录(cookie)
  • 原文地址:https://www.cnblogs.com/hushaoyan/p/9969829.html
Copyright © 2011-2022 走看看