• python运算优先级

PEMDAS 括号(parentheses)指数(exponents)乘(multipilication)除(division)加(addition)减(subtraction)

值得注意的是这不是一个绝对优先级,可以这么理解PE(M&D)(A&S)

  • 变量与命名
cars = 100
#有一百辆车
space_in_a_car = 4.0
#一个车有四个空间
drivers = 30
#有三十个司机
passengers = 90
#有九十个乘客
cars_not_driven = cars - drivers
#cars_not_driven没有司机的车
cars_driven = drivers
#cars_driven有司机的车
carpool_capacity = cars_driven * space_in_a_car
#所有车的容量
average_passengers_per_car = passengers / cars_driven
#平均每辆车有几个乘客
print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty car today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car, "in each car.")

poweshell

PS C:\\Users\\ARAN\\lpthw> python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty car today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.
  • 字符串的使用
my_name = 'Zed A. shaw'
my_age = 35
my_height = 74 #m
my_weight = 180 #kg
my_eyes = 'blue'
my_teeth = 'White'
my_hair = 'brown'
my_height2 = 100 * my_height #my_height2==mm
my_weight2 = 2 * my_weight #my_weight2==g
# test print (f"{my_height2}")print(f"Let's talk about {my_name}.")
print(f"He's {my_height2} inches tall")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")total = my_age + my_height + my_weightprint(f"If I add {my_age}, {my_weight2}, and {my_height2} I get {total}."

powershell

PS C:\\Users\\ARAN\\lpthw> python ex5.py
Let's talk about Zed A. shaw.
He's 7400 inches tall
Actually that's not too heavy.
He's got blue eyes and brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 360, and 7400 I get 289.
  • round四舍五入函数,示例
f = round(1.77)
print(f"{f}")

输出2

打印 打印

type_of_people = 10
x = f"There are {type_of_people} type of types of people."
#打印字符
#如果遇到字符里面有{字符串},前面需要加f
binary = "binary"
do_not = "don't"
#定义bianry
#定义do_not
y = f"Those who know {binary} and those who {do_not}."
#
print(x)
#输出定义的字符不用加引号
#也可以使用format()来表示
print(y)print(f"i said: {x}")
print(f"I also said: {y}")
#带{}的前面一定要加fhilarious = False
#false不用加引号
joke_evaluation = "Isn't taht joke so funny?!{}"print(joke_evaluation.format(hilarious))w = "This is the left side of..."
e = "a string with a right side"print(w + e)
a = 1
b = 2
c = a + b
print(c)a_b = 1
a_b_c = a_b + 1
print(a_b_c)a = "I"
b = "LOVE"
c = "CHINA"
d = a + b + c
e = "You"
print(d)
#字符前面加引号,数字不必加引号
print(f"If I told who loves {c}, I hope the one is {e}. ")
f = "If i told who loves {}"
#使用.format而不使用f()的时候应该在前面的语句上末尾加入{}
g = "I hope the one is {}"
print(f.format(c), g.format(e))h = True
print(h)

打印 打印 打印

print("Mary had a little lamb")
print("Its fleece was white as {}.".format('snow'))
print("And everywhere thar Mary went")
print("." * 10) #What's that do?end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"#watch that comma at the end. try removing it to see what happens
print(end1 + end3 + end4 + end5 + end6, end=' ')
print(end7 + end8 + end9 + end10 + end11 + end12)
  • 如果不想python以另起一行的形式打印,可以在末尾加, end=‘ ‘

’_‘中间可以是变量,也可以是字符串,也可以是空格

  • 另外,字符串可以乘数字以输出更多字符

例如 ”.“ * 10

formatter = "{} {} {} {}"print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, True, False))
print(formatter.format(formatter, formatter, formatter, formatter))
print(formatter.format(" Try your\\n","Own text here\\n","Maybe a poem\\n","Or a song about fear "
))
  • 打印 打印 打印
#Here's some new strange stuff, remember type it exactlydays = "Mon Tue Wed Thu Fri Sat Sun"
months = "\\nJan\\nFeb\\nMar\\nMay\\nJUn\\nJul\\nAug"print("Here are the days:", days)
print("Here are the months:", months)print("""There is something going on here.With the three double-quotes.We'll be albe to type as much as we like.Even 4 lines if we want, or 5, or 6.
""")
  • 三个引号”“”代表超长打印,可以放很长很长的字符串
  • 转义字符
tabby_cat = "\\tI'm tabbed in."
persian_cat = "I'm split\\non a line"
backslash_cat = "I'm \\\\ a \\\\ cat"fat_cat ="""
I'll do a list:
\\t* Cat food
\\t* Fishies
\\t* Catnip\\n\\t* Grass
"""print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)

转义序列

  • input函数
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weight?", end=' ')
weight = input()print(f"So, you're {age} old, {height} tall and {weight} heavy.")
  • 可以在input函数中加入问题,因此上面的问法可以替换为
age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weight? ")print(f"So, you're {age} old, {height} tall and {weight} heavy.")
  • 参数、解包和变量
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argvprint(">>>> argv = ",repr(argv))print("The script is called:", script)
print("Your first variable is:", first)
print("Your second cariable is:", second)
print("Your third variable is:", third)
  • 写一个类似《Zork》的文字游戏
from sys import argvscript, user_name = argv
prompt = '> '
print(f"Hi {user_name}, I'm the {script} scripter")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)print(f"Where do you live {user_name}")
live = input(prompt)print(f"What kind of computer do you have?")
computer = input(prompt)print(f"""
Alright, so you said {likes} about liking me.
You live in {live}.
Not sure where that is.
And you have a {computer} computer.
Nice.
""")

值得注意的是在里面把提示用户输入的>定义为了prompt(提示),到时候直接修改一个自定义即可完成全局修改

  • 尽量不要把代码中的内容写死(hardcode),让用户来决定写哪个文件
  • 读取ex15_sample.txt中的文本,代码如下
from sys import argvscript, filename = argvtxt = open(filename)print(f"Here's your file {filename}:")
print(txt.read())
print("\\n")print("Type the filename again:")
file_again = input("> ")txt_again = open(file_again)print(txt_again.read())
  • 只用input读取并打印代码
filename = input("please typing your filename\\n >>>")
txt = open(filename)
print(txt.read())

Python3学习笔记(二)by Learn Python 3 the HARD WAY相关推荐

  1. 【Python3学习笔记】之【Python基础——注释与运算符】

    python3 注释 python 中的注释有单行注释和多行注释. 单行注释以 # 开头,例如: # 这是一个注释 print("HelloWorld!") 多行注释用三个单引号 ...

  2. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0. zfill()方法语法: ...

  3. python基础第三章选择结构答案-python3 学习笔记(二)选择结构、循环结构

    python3 学习笔记 python 优雅 明确 简单 1.选择结构 (1)简单判断 if else 使用格式: if  条件: 表达式1 else: 表达式2 (2)多条件判断 elif 使用格式 ...

  4. python学习笔记(二) 基本运算

    python学习笔记(二) 基本运算 1. 条件运算 基本语法 if condition1: do somethings1elif condition2: do somethings2else: do ...

  5. (Python入门)学习笔记二,Python学习路线图

    (Python入门)学习笔记二,Python学习路线图 千里之行始于足下,谋定而后动,冰冻三尺非一日之寒.之所以说这三句话,就是对于下面整理的路线图,即不让自己感觉路途的遥远而感到达到巅峰神界的遥遥无 ...

  6. pythonsze_python学习笔记二 数据类型(基础篇)

    Python基础 对于Python,一切事物都是对象,对象基于类创建 不同类型的类可以创造出字符串,数字,列表这样的对象,比如"koka".24.['北京', '上海', '深圳' ...

  7. Python3学习笔记之-学习基础(第三篇)

    Python3学习笔记之-学习基础(第三篇) 文章目录 目录 Python3学习笔记之-学习基础(第三篇) 文章目录 一.循环 1.for循环 2.while循环 3.break,continue 二 ...

  8. A Byte of Python3 学习笔记

    A Byte of Python3 学习笔记 第七章 控制流 1.if语句(带输入函数) 2.while语句 3.for循环 4.break.continue.return的区别 第八章 函数 8.1 ...

  9. 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP

    原文地址可以查看更多信息 本文主要参考于:Multilayer Perceptron  python源代码(github下载 CSDN免费下载) 本文主要介绍含有单隐层的MLP的建模及实现.建议在阅读 ...

  10. 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-03-基于Python的LeNet之LR

    原地址可以查看更多信息 本文主要参考于:Classifying MNIST digits using Logistic Regression  python源代码(GitHub下载 CSDN免费下载) ...

最新文章

  1. Java学习总结:57(Properties子类)
  2. 如何养成一个习惯(持续更新)
  3. 数据结构之归并排序图文详解及代码(C++实现)
  4. 变成一列_Excel中将多列,快速变成1列,困惑了多年,今天总算学会了
  5. 介绍两款WordPress文章转移插件
  6. 教程 | 叮咚!答应你们的文件上传教程,到货了!
  7. 关于listener
  8. Beekeeper Studio:一款高颜值且免费的 SQL 开发工具
  9. 实用干货:电放提单详解,与海运单、一般提单到底有什么区别?
  10. 物料编码在PDM与ERP集成中的应用研究
  11. 接口测试及常用接口测试工具
  12. android词根词缀,词根词缀记忆字典 - 好担心你们因为它的界面丑,而错过这款背单词神器 - Android 应用 - 【最美应用】...
  13. java 缓存文件_java实现酷狗音乐临时缓存文件转换为MP3文件的方法
  14. android WPS中设置目录标题和目录引用
  15. 天津大学2020年考研考前公告
  16. leaflet实现风场图
  17. 使用MySQL管理工具-SQLyog 9.63报错号码2058,超详细解析
  18. 数字增长动画-uniapp插件
  19. Android实现第三方登录并获取到头像、名字
  20. 外卖平台乱象迭出!究竟谁该负责?

热门文章

  1. 简单电子产品的蓝牙电路设计和PCB设计
  2. 【论文】联邦学习区块链 论文集(三)
  3. 【JAVA Reference】Finalizer 剖析 (六)
  4. 探索TiDB Lightning源码来解决发现的bug
  5. 微信小程序 发送模板消息的功能实现
  6. 评价页面html代码,HTML5 评论列表界面模板
  7. Unity translucent SSS 次表面散射 皮肤材质研究
  8. 【论文泛读62】HybridQA:通过表格和文本数据进行多跳问答的数据集
  9. 如何设置计算机自动连接宽带,宽带自动连接设置,教您电脑怎么设置宽带自动连接...
  10. Python 查看显存大小