Python基础一(格式化输出、流程控制)

(1)格式化输出(%% 第一个% 转译)

 # 格式化 输入 输出
name = input("Name:")
age = input("Age:")
job = input("Job:")
hobbies = input("Hobbies:") info = '''
---------index %s -------------
Name : %s
Age : %s
job : %s
Hobbies: %s
''' % (name, name, age, job, hobbies) print(info) # 如果输入的时候必须需要输入% 则输入%% 可以实现转义功能
msg = "我是%s,年龄%d,目前学习进度为80%%" % ('shine_rainbow', 18)
# result 我是shine_rainbow,年龄18,目前学习进度为80%
print(msg)

(2)基本数据类型1

 # int
a = 2 ** 3
print(type(a))
b = 2 ** 60
print(b)
# str
name = "my name is alex"
age = ""
msg = name+"\n"+age+"岁了\n"+"学习python 非常开心 开 心 开 心 心 "
print(msg)
# 对于input 从键盘上获取的内容,均为字符型
# name = input("请输入贵姓")
# print(name)
# print(type(name))
# 注意 在字符串拼接中str + str 为字符串拼接 str * int 为将这个内容复制多少次,在拼接在一起
print("hello world\n" * 10) # 打印10次hello world
# 字符串的拼接只能是双方都是字符串, 不可以跟数字和其他类型拼接
# print("hello"+2)
# 布尔类型 print(3 > 4) # 格式化输出
"""
需求:询问用户的姓名、年龄、工作、爱好,然后打印成如下格式:
----------------index of Alex Li----------------
Name : Alex Li
Age : 22
job : Teacher
Hobbies : girl
---------------end-------------------
"""
# name = input("please input name")
# age = int(input("please input age")) # 将str--->int
# job = input("please input job")
# hobbies = input("please input hobbies")
# msg =
# """
# ----------------index of %s----------------
# Name : %s
# Age : %s
# job : %s
# Hobbies : %s
# ---------------end-------------------
# """ % (name, name, age, job, hobbies)
# print(type(age))
# print(msg)

(3)基本逻辑语句

 # 逻辑运算 1.在没有()的情况下,not 优先级高于and and 高于or 即优先级()>not > and >or 同一优先级从左到右计算
print(3 > 4 or 4 < 3 and 1 == 1)
# f or f print (false)
print(1 < 2 and 3 < 4 or 1 > 2)
# t or f print(true)
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# t or f print(true)
# 求出下列逻辑语句的值
# x or y if x is false then y else x
print(8 or 4) print(0 and 3)
# if x is false then true else false
# x and y ,x true then y ,x false x
print(0 or 4 and 3 or 7 or 9 and 6)
# 0 or 3 or 7 or 6
# 3 or 7 or 6
# 3 or 6
#
# 判断子元素是否在原字符串(字典、列表、集合中)
print('喜欢' in 'jdljs;k;as喜欢')
print('a' in 'asdfds')
print('y' not in 'avd')
# true true true

(4)流程控制

 # if :
if 3 > 4:
print("")
else:
print("")
# if 多分支测试
score = int(input('请输入您的分数'))
if score > 100:
print("神童")
elif score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 60:
print("C")
elif score > 10:
print("D")
else:
print("成绩低于10 太难受了。")

(5)循环控制

 # while循环
# 1-100的求和
# count = 0
# result = 0
# while count < 100:
# result += count
# count += 1
# print(result)
# 只要求出5-95之前的数字 注意使用continue
count = 0
result = 0
while count < 10:
count += 1
if count > 3 and count < 6:
continue
result += count print(result)

第一天测试题:

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

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

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

4.1-2+3-4+5-...+99

5.三次登录

 # 使用while 循环输出 1 2 3 4 5 6 8 9 10
# count = 0
# while count < 10:
# count += 1
# if count == 7:
# continue
# print(count)
# print("----end----")
# 求1-100的所有数的和
# result = 0
# while count < 100:
# count += 1
# result += count
# print(result)
# 输出1-100 内的所有奇数
# result = 0
# while count < 100:
# count += 1
# if count % 2 == 0:
# continue
# result += count
# print(result)
# 或
# count = 1
# while count < 100:
# result += count
# count += 2
# print(result)
# 1-2+3-4+5-...+99
# count = 1
# sum1 = 0
# flag = 1
# while count < 100:
# if count % 2 == 0:
# flag = -1
# else:
# flag = 1
# sum1 = sum1 + count * flag
# count += 1
# print(sum1)
# 三次登录
username = "root"
password = ""
count = 3
while count > 0:
count -= 1
input_username = input("请输入用户名:")
input_password = input("请输入密码:")
if input_username.__eq__(username):
if input_password.__eq__(password):
print("登录成功,当前登录用户%s"%(username))
break
else:
print("登录失败,你还有%d次机会" %(count))
else:
print("登录失败,你还有%d次机会" %(count))
上一篇:C#获取周的第一天、最后一天、月第一天和最后一天


下一篇:python的学习笔记01_3 基本运算符 流程控制if while 字符串常用办法