笨办法学习Python3练习代码1-10
ex1.py
print("hello world!",end = " ")#不换行
print("hello again")
print("I like typing this.")
print("This is fun.")
print("Yay!Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
print("")
ex2.py
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.")# and the comment after is ignored . # you can also use a comment to "disable" or comment out code:
#print("This won't run.")
print("This will run.")
print("hi # there.") # (#)的英文是octothorpe或者 pound character
ex3.py
#数字和数学运算
print("I will now count my chickens:")
print("hens", 25 + 30 / 6)
print("roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:") print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) print("Is it true that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("what is 3 + 2 ?", 3 + 2)
print("what is 5 - 7?", 5 - 7)
print("Oh,that's why it's false.")
print("How about some more.")
print("Is it greater ?", 5 > -2)
print("Is it greater or eequal ?", 5 >= -2)
print("Is it less or equal ?", 5 <= -2)
ex4.py
#变量和命名 #汽车数量
cars = 100 #每辆汽车的运载人数
space_in_a_car = 4 #司机人数
drivers = 30 #乘客人数
passengers = 90 #空车数量
cars_not_driven = cars - drivers #占用的汽车数量
cars_driven = drivers #最大可运输的人数
carpool_capacity = cars_driven * space_in_a_car #capacity=容积,carpool=拼车 #平均每台汽车上的人数
average_passengers_per_car = passengers / cars_driven print("there are ",cars,"cars available.")
print("There are only",drivers,"drivers availlable.")
print("There will be ",cars_not_driven,"empty cars 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.")
ex5.py
name = 'Zed A.Shaw'
age = 35 #not a lie
height = 74 #inches
weight = 180 #lbs
eyes = 'Blue'
teeth = "White"
hair = "Brown" print(f"Let's talk about {name}.") #f代表格式化的意思format
print(f"He's {height} inches tall.")
print(f"He's {weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hair.")
print(f"His teeth are usually {teeth} depending on the coffee.") #this line is tricky, try to get it exactly right
total = age + height + weight
print(f"If I add {age}, {height}, and {weight}. I get {total}.")
ex6.py
tpyes_of_people = 10
x = f"There are {tpyes_of_people} 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 = "Isn't that joke so funny ?!{}"
#输出Isn't that joke so funny ?!False
#.format()括号里面的内容都是格式化输出
print(joke_evaluation.format(hilarious))
w = "This is the left side of ..."
e = "a string with a right side."
print(w + e)
ex7.py
print("Mary had a little lamb.")
print("Its fleece was white as {}.".format('snow'))
print("and everywhere that Mary went.")
print("." * 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'
print(end1 + end2 + end3 + end4 + end5 + end6, end = " ")
print(end7 + end8 + end9 +end10 + end11 + end12)
ex8.py
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",
"Own text here",
"Maybe a poem",
"Or a song about fear"))
#这个习题使用了format函数,让它返回formatter变量到其他字符串中
#当看到formatter.format(....),相当于
#1.取第一行定义的formatter字符串
#2.调用它的format函数
#3.给format传递四个参数,这些参数和formatter中的{}匹配
#4.在formatter上调用format的结果是一个新字符串,其中的{}被四个变量所替换
ex9.py
# Here's some new strange stuf(材料,原料),remember type it exactly.
days = "mon tue wed thu fri sat sun"
months = " jan\nfeb\nmar\napr\nmay\njun\njul\naug"
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 tpye as much as we like.
even 4 lines if we want,or 5, or 6.''')
ex10.py
#\\转义字符\
#\t转义字符tab键缩进,水平制表符,一个制表符8个位置
#\'输出'
#\"输出“ 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)
笨办法学习Python3练习代码1-10的更多相关文章
- 笨办法学习python3练习代码:argv参数变量与文件操作
ex15.py 完成ex15.py需要在ex15.py同文件夹目录下面准备一个txt文件(ex15_sample.txt) 执行ex15.py 如: python ex15.py e ...
- 笨办法学习python3练习代码ex20.py 函数和文件
注意,还要在python3,就是ex20.py的同目录里面直接创建一个ex20.txt的文件.里面至少要有三行内容 #函数和文件 #readline:只读取文本文件的一行 #seek(0):将读写位置 ...
- 笨办法学python3练习代码13-14:argv参数变量的学习
ex13.py argv参数的学习 #argv:参数变量(argument variable),这是一个标准的编程术语,在其他语言中也可可以看到.argument可译为: 参数 #如果参数是用户在执 ...
- 笨办法学习python-ex51自我理解笔记
本章节主要讲的是web的工作原理,先大概熟悉记录一下,为以后写Django web框架打下基础. web工作原理: 1.用户从浏览器输入网址----->browser通过电脑中的网络设备(网卡) ...
- 笨办法学python3练习代码ex21.py
def add(a, b): print(f"ADDING {a} + {b}") return (a + b) def subtract(a, b): #subtract :减去 ...
- 笨办法学python3练习代码ex19.py
定义函数的语法: def 函数名(参数) (语句) #函数和变量 #函数里的变量与脚本里的变量是没有联系的. def cheese_and_crackers(cheese_count,boxes_o ...
- 笨办法学python3练习代码ex18.py
#命名.变量.代码.函数 #this one is like your scripts with argv def print_two(*args): arg1, arg2 = args #将参数解包 ...
- 笨办法学习python之hashmap
#!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi #初始化aMap列表,把列表num_buckets添加到aMap中,num_ ...
- 笨办法学习python-ex41源码加自己注释
#!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi import random from urllib import urlop ...
随机推荐
- day58 bootstrap效果无法显示
在学习bootstrap时直接复制官网的组件的时候,如果效果无法想官网一样显示,最大的可能是类库导入的顺序问题. 打开页面>检查>Console 我们会发现一条报错,导入的js需要jQue ...
- requests接口自动化4-登录后才能访问的get请求,需共享cookie
登录后才能访问的get请求,需共享cookie fiddler里请求响应结果: 代码: import requests import json #form表单形式的post请求,用data传参,Con ...
- 06 drf源码剖析之权限
06 drf源码剖析之权限 目录 06 drf源码剖析之权限 1. 权限简述 2. 权限使用 3.源码剖析 4. 总结 1. 权限简述 权限与身份验证和限制一起,决定了是否应授予请求访问权限. 权限检 ...
- Python之爬虫(七)正则的基本使用
什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符”,这个“规则字符” 来表达对字符的一种过滤逻辑. 正则并不是pyth ...
- JSON基础使用详解
JSON:JavaScript对象表示法 1.1 JSON说明 曾经一段时间,XML是互联网传递数据的统一标准,但是业界一直不乏质疑XML的人士,他们都认为XML过于繁琐,冗长:提取数据也过于麻烦 2 ...
- bzoj1673[Usaco2005 Dec]Scales 天平*
bzoj1673[Usaco2005 Dec]Scales 天平 题意: n个砝码,每个砝码重量大于前两个砝码质量和,天平承重为c,求天平上最多可放多种的砝码.n≤1000,c≤2^30. 题解: 斐 ...
- Ethical Hacking - Web Penetration Testing(11)
SQL INJECTION Preventing SQLi Filters can be bypassed. Use a blacklist of commands? Still can be byp ...
- Alink漫谈(十二) :在线学习算法FTRL 之 整体设计
Alink漫谈(十二) :在线学习算法FTRL 之 整体设计 目录 Alink漫谈(十二) :在线学习算法FTRL 之 整体设计 0x00 摘要 0x01概念 1.1 逻辑回归 1.1.1 推导过程 ...
- 获取字符串指定字符的第n次出现位置
create function uf_findx (@text nvarchar(max),@find_x varchar(200),@find_n int)returns intasbegin -- ...
- JAVA 实现将多目录多层级文件打成ZIP包后保留层级目录下载 ZIP压缩 下载
将文件夹保留目录打包为 ZIP 压缩包并下载 上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为ZIP文件,返回给前台:在数据库中保存的是html标签 ...