初识python ex03-ex15

ex03:数字和数学计算

%求余

#ex03
print ("I will now count my chickens:")
print ("Roosters",100 - 25 * 3 % 4) # % 求余
print ("Now I will count the eggs:")
print (3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 )
print (3 + 2 < 5 - 7)
print ("what is 3 + 2?",3 + 2)
print ("what is 5 - 7?",5 - 7)

ex04 变量

=赋值 ==左右两边相等,返回bool值

cars = 100
space_in_a_car =4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print ("There are","cars available.")
print ("There are only",drivers,"drivers available.")
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.")

ex05 更多的变量和打印

f 格式化字符串

my_name = "zed a. xiao"
my_age = 35 #not a lie
my_height = 175
my_weight = 75
my_eyes = "Black"
my_teeth = 'White'
my_hair = 'Brown'

print (f"Let's talk aboult {my_name}. ")
print (f"He's {my_weight} pounds heavy.")
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_weight
print (f"If I add {my_age},{my_height}, and {my_weight} I get {total}.")

ex06字符串和文本

.format()语法的格式化方式

types_of_pople = 10
x = f"There are {types_of_pople} types of people."

binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."

print (x)
print (y)

print (f"I said: {x}")
print (f"I also said: '{y}' ")   #   ""套""报错

hilarious = "False"
joke_evaluation = "Is't that 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)

ex07 更多的打印

print ("Mary had a little lamb.")
print ("It's fleece was whilte as... {}....".format('snow'))
print ("." * 50) #what'd that do? 打印50个.

end1 = "c"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print (end1 + end2 + end3 + end4 + end5 + end6,end ='......')
print (end7 + end8 + end9 + end10 + end11 + end12)

ex08 打印 打印

对字符串做更复杂的格式化

formater = "{} {} {} {}"
print (formater.format(1 , 2 , 3 , 4))
print (formater.format("One" , "Two" , "three" , "four"))
print (formater.format(True , False , True ,False))
print (formater.format(formater , formater , formater , formater))

print (formater.format(
"try your",
"own text here",
"maybe a poem",
"or a song about fear"
))

ex09 打印,打印,打印

days = "Mon Tue Wed Fir Sat Sun"
months = "
Jan 
Feb 
May 
Jun 
Aug"
print ("here are the days:",days)
print ("here are the months:",months)
print ("""
there's something going on here.
with the three double-quotes.
we'll be able to type as much as we like.
Even 4 lines if we want, or 5 ,or 6.
""") 
#""" """ 多字符串引用

ex10 那是什么

tabby_cat = "	I'm tabbed in."
persian_cat = "I'm spit
on a line."
backslash_cat = "I'm \ a \ cat."

fat_cat = """
I'll do a list:
	* Cat food
	* Fishies
	* Catnip
	* Grass
"""

print (tabby_cat)
print (persian_cat)
print (backslash_cat)
print (fat_cat)

转义序列

转义字符 功能
\ 反斜杠
* 单引号
" 双引号
换行符
水平制表符tab
回车符
a ASCII响铃符
 ASCII退格付符
f ASCII进纸符
N{name} Unicode数据库中的字符名
uxxxx 值为16位进制xxxx的字符
Uxxxxxx 值为32位十六进制xxxxxx的字符
ooo 值为八进制值ooo的字符
xhh 值为十六进制值hh的字符

ex11 提问

input() 函数接受一个标准输入数据,返回为str字符串类型。

raw_input ()直接读取控制台的输入(任何类型的输入它都可以接收*

print ("How old are you?", end = ' ') # 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.")

ex12 提示别人

y = input (""name?")

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 , {weight} tall and {weight} heavy.")

ex13 参数、解包和变量

#import将python模块(一种特性)引入到脚本的方法;
#argv 参数变量(argument variable)
from sys import argv 

# read the WYSS section for how to run this
xxx, first, second, third = argv #argv解包,将其赋值给4个变量

print ("The script is called:", xxx) #打印出脚本文件名,可变
print ("Your fist variable is :", first)
print ("Your second variable is :", second)
print ("Your third variable is :", third)
#执行 python ex13.py 1nd 2nd 3nd  对应 xxx,first, second, third
  • argv和input()的不同点:

​ 用户输入的时机不同。如果参数在用户执行命令时就要有,那就用argv,如果在脚本运行过程中需要用户输入,那就用 input()

ex14 提示和传递

from sys import argv

script, user_name = argv
prompt = '>' 

print (f"Hi {user_name} , I'm the {script} script.")
print ("I'd like ask you a few questions.")
print (f"Do you Like me {user_name} ?")
likes = input(prompt)  #给了个输入提示符>

print (f"Where do you live {user_name} ?")
lives = input(prompt)

print ("What kind of computer do you have?")
computer = input(prompt)

print (f"""
Alright, so you said {likes} about liking  me.
You live in  {lives}. Not sure where that is.
And you have a {computer} computer. nice
""")

ex15 读取文件

#ex15 需要新建一个txt文件
from sys import argv

script, filename = argv  #filename 运行时给与txt 文件名

txt = open (filename) 
# open 接收一个参数并返回一个值,将一个值赋给一个变量,这就是打开文件过程。

print (f"Here's your file {filename}:")
print (txt.read ()) #嘿,txt执行你的read命令!

print ("Type the filename again:")
file_again = input ('>>>')
txt_again = open (file_again)# open (input('>'))
print (txt_again.read())

#处理完后需要将文件关闭。
txt.close () 
txt_again.close ()
原文地址:https://www.cnblogs.com/wyh0717/p/12689707.html