2021-10-16

笨办法学Python3 (Learn Python3 The Hard Way)


官网
国内相关贴子

安装Python与编译器*

  • 查看之前安装的python的版本号
  • python --version
  • 查看python 安装目录
python 
import sys
sys.path
  • Pycharm 对代码进行多行注释
  • Ctrl +/
    如何给Pycharm加上头行 # *coding:utf-8 *

File->Setting->Editor->Code Style->File and Code Templates->Python Script 后面加上 # *coding:utf-8 * 即可
2021-10-16

练习3 数字和数学

+ plus,加号
- minus,减号
/ slash,斜杠
* asterisk,星号
% percent,百分号
< less-than,小于号
> greater-than,大于号
<= less-than-equal,小于等于号
>= greater-than-equal,大于等于号

练习 6 字符串和文本

# -*- coding:utf-8 -*-
'''练习6'''
types_of_people = 10         # 定义一个变量并赋值
x = f"There are {types_of_people} types of people "
# 定义变量x,并在赋值时引用另一个变量且使用.format()函数使其能被其他函数引用

binary = "binary"       # 为 binary 赋值
do_not="don't"          # 为do_not赋值
y = f"Those who know {binary} and those who {do_not}."
# 为变量"y" 赋值时引用"binary","do_not"且用.format()函数使能被其他函数引用
print(x)  #打印变量'x'的值
print(y)   #打印变量'y"的值

print(f"I said:{x}")
print(f"I also said: '{y}'")

hilarious = False
Joke_evaluation = "Isn't that joke so funny?!{}"
w = "This is the left side of..."
e = "a string with a right side."

print(w + e)

知识点总结:
1.当为一个变量赋值,且希望之后可以引用它,可以用.format()函数;
2.当一个新的变量引用之前的变量时,使用{};

练习 7 更多打印

'''练习7'''
print("Mary had a little lamb.")
print("Its fleece was white as {}.".format('snow'))
print("And everywhere that Mary went.")
print("."* 10) # what"d that do ?   答:打印10个"."
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 removeing it to see what h
print(end1 + end2 + end3 + end4 + end5 +end6,end= ' ' )
print(end7 + end8 + end9 + end10 + end11 +end12)

知识点总结:

  1. ".format()“函数有两种格式:
x = f("test1")
y = test 2 {x}test3 
y=("test2{}test2”.format('test1'))	 
  1. 字符串也可以用 算数符号“*”,“+” 连接

练习8 打印 + ".format()"函数

'''练习8'''

formatter = "{}{}{}{}"
print(formatter.format(1,2,3,4))
print(formatter.format("one,","two,","three,","four"))
print(formatter.format(True,False,False,True))
print(formatter.format(formatter,formatter,formatter,formatter))
print(formatter.format(
    "Try your\n",
    "Own tex here\n",
    "Maybe a poem\n",
    "Or a song about fear"
))

知识总结:见练习6

练习10 转义符

'''练习10'''
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)

知识点总结:

  1. 在特殊符号前加转义符"",使其成为普通字符
  2. 打印时可以用"""开始,"""结尾,进行段落打印,
    print(""" 也是一种转义字符""")

练习11 "input()"函数

'''练习11'''
print("How old are you?")
age = input()
print("How tall are you ?")

height = input()
print("How much do you weigh?")
weight = input()
print(f"So, you're {age} old, {height} tall and {weight} heavy.")

2021-10-16
2021-10-16
知识点总结:
print("一串字符串",end=' ') 时,打印结果与input内容在同一行换行,
print("一串字符串")时,打印结果换行后再进行input

'''练习11'''
age = input ("How old are you?")
height = input("How tall ara you?")
weight = input("How much do you weigh?")
F = f"So,you're {age} old,{height} tall and {weight} heavy."
print(F)

知识点总结:
可以直接在函数input() 中输入提醒,同时将输入的值赋予一个函数;
如果想用输入的值做数学运算,可以使用"int"整形函数或其他类型的函数将输入字符变为数字,例:

x = int(input("请输入一个整形数值"))
y = float(input("请输入一个小数"))
print("他们的乘积为{:.3f}".format(x*y))

P= x*y
print("他们的乘积为%.2f" %P)

注:注意以上代码中指定浮点函数输出小数点后位数的方式有两种

上一篇:2-备考环境准备


下一篇:ElasticSearch学习(四)ElasticSearch基本操作