python学习笔记(一)

python学习笔记(一)

基础数据类型、循环

  语言可以分为编译型语言与解释型语言。

  编译型语言顾名思义就是需要先编译再执行,比如c、c++、c#

  解释型语言不用编译,可直接执行,如python、java、php、js、go、ruby

1、基础数据类型

1) Python3 中有六个标准的数据类型:

  • Number(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Set(集合)
  • Dictionary(字典)

其中数字类型又包括:int、float、bool、complex

2)python的变量定义

python定义变量直接用 变量名 = 值

变量名只能是 字母、数字或下划线的任意组合;变量名的第一个字符不能是数字。

如:myname = "yanyan"

abc = def = 123

3)单引号、双引号、三引号

如果内容里面包括单引号,就用双引号;

如果内容城面包括双引号,就用单引号;

如果内容里面即有单引号又有双引号,则可以用三引号;

三引号还是多行注释。

4)输入、输出

  • 输出

python3里面输出用print(),一定要加括号

如:print(“Hello yanyan”)  输出一个字符串

username = "yanyan"

count = 3

print("The name is {username}, the count is {count}".format({username}, {count}))   格式化输出

print("欢迎" + username + "登录")     不推荐这种方式

print('欢迎%s登录,今天的日期是%s'%(username, date))   %s是string类型,%d整型,%f浮点型,%.2f保留小数点后两位

  • 输入

username = input("请输入用户名: ")

age = int(input(“请输入年龄: ”))    int型的要进行数据类型转换,因为input获取到的数据全是string类型

5)条件判断

使用if...else...进行条件判断,如果多个条件则用if...elif...elif...else....进行判断

score = float(input("请输入你的成绩: "))

if score >= 90:
    print("优秀")
elif 80 <= score < 90:
    print("良好")
elif 60 <= score <80:
    print("及格")
elif 0 <= score <60:
    print("不及格")
else:
    print("输入的成绩不对")

6)循环

一直重复做一件事情,可以叫迭代、循环、遍历

  •  while循环

格式:

while  xxx:    #当条件为真的时候继续循环体,当条件为假时结束循环

  xxx

 1 #猜数字
 2 import random
 3 
 4 count = 0
 5 nmuber = random.randint(1,10)
 6 
 7 while count < 7:
 8     n = int(input("Enter the number: "))
 9     count += 1
10     if n == nmuber:
11         print("you are win")
12         break
13     elif n > nmuber:
14         print("大了")
15     else:
16         print("小了")
17 else:                    #正常结束的循环,执行else,break结束的循环,不会执行else
18     print("次数用尽了")
  • for循环

  for i in range(0,10):

    print("xxxxxxx")   #循环打印10次

 1 import random
 2 
 3 count = 0
 4 nmuber = random.randint(1,10)   #生成一个int型随机数,范围1-10
 5 
 6 for i in range(7):
 7     n = int(input("Enter the number: "))
 8     if n == nmuber:
 9         print("you are win")
10         break    #结束循环
11     elif n > nmuber:
12         print("大了")
13     else:
14         print("小了")
15 else:
16     print("次数用尽了")
1 #循环套循环
2 for i in range(1,10):
3     for j in range(1,10):
4         print(i * j , end=' ')
5     print()
  • break、continue

break:跳出循环体,循环结束

continue:跳出本次循环,循环继续

原文地址:https://www.cnblogs.com/yanyan-/p/10594975.html