python入门 -- 学习笔记1
学习资料:笨方法学Python
准备:
- 安装环境----请自行网络搜索(Windows安装很简单,和其他安装程序一样)
- 找一个自己习惯的编辑器(比如:sublime text 3)
- 创建一个专门的目录,按着资料把所有的代码敲一遍 -- 可了解下DOS基本命令
1、第一个程序
文件命名方式: 文件.py
执行方式:python 文件.py (在该文件路径下执行)
若执行错误,会有报错提示,看不懂可以从搜索引擎中查询
字符串的表示有几种方式,你注意到了吗~,还可以继续搜索是否有其他表示方式
正确代码及结果:
错误代码及结果:可以根据提示进行搜索错误原因,并可通过提示查找对应的错误代码行进行检查
习题2:注释与井号----程序里的注释是很重要的
代码:
# A comment,this is so you can read your program later.
# Anything after the # is ingnored 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 a piece of code:
# print "This won't run."
print "This will run"
运行:
习题3:数字和数学计算
• + plus 加法计算
• - minus 减法计算
• / slash 除法计算(保留商,舍弃余数)
• * asterisk 乘法计算
• % percent 模计算(取余数)
• < less-than 比较运算 小于号
• > greater-than 比较运算 大于号
• <= less-than-equal 比较运算 小于等于号
• >= greater-than-equal 比较运算 大于等于号
代码:建议养成良好的代码习惯,加空格
print "I will 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 egual?", 5 >= -2
print "Is it less or egual?", 5 <= -2
结果:
习题4:变量和命名 -- 某东西的名字,在程序中建议以变量代表的真实含义用英语命名,亦可多个单词以“_”拼接
命名规范:适用字母、数字、下划线,数字不能开头
备注:字符串与变量的区别? 变量不用引号,直接调用变量名
代码:
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, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We van 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."
结果:
习题5:更多的变量及打印
字符串是程序将信息展示给人的方式。可以打印它们,可以将它们写入文件,还可以将它们发送给网站服务器,很多事情都是通过字符串交流实现。本习题引入格式化字符串。
常用:%s 字符串 ; %d 整数 ; %f 浮点数 ; %r 自然字符串 ,其余可搜索看看,另可查查%s和%r有什么不同~( 和 print "%r" % "\n")
格式:"%s" % 对应的变量(变量多个时,用圆括号括起来,与前面一一对应,中间用逗号隔开)
注意:大小写敏感,my_age和My_age代表不同的变量
如果使用了非ASCII字符碰到了编码错误,记得在最顶端加一行 # coding: utf-8
代码:
my_name = 'Zed A. Shaw'
my_age = 35
my_height = 74
my_weight = 180
my_eyes = 'Brown'
my_teeth = 'White'
my_hair = 'Black'
print "Let's talk about %s." % my_name
print "He's %d inches tail." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is trick, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)
结果:
习题6:字符串和文本
备注:字符串拼接可以使用“+”
-- “+” 表示拼接 “,”主要是分割参数的一种方式
代码:
X = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
Y = "those who know %s and those who %s." % (binary, do_not)
print X
print Y
print "I siad: %r." % X
print "I also said: '%s'." % Y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
结果:
习题7:更多打印
备注:代码倒数第二行的打印,最后的逗号注意到了吗?不加的话会有怎样的效果呢?
代码:
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 # what'd 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 + end2 + end3 + end4 + end5 +end6,
print end7 + end8 + end9 + end10 + end11 + end12
结果:
习题8:打印,打印
代码:
formmatter = "%r %r %r %r"
print formmatter % (1, 2, 3, 4)
print formmatter % ("one", "two", "three", "four")
print formmatter % (True, False, False, True)
print formmatter % (formmatter, formmatter, formmatter, formmatter)
print formmatter % (
"I had this thind.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
结果:
习题9:打印,打印,打印
备注:代码中三个连着的双引号有什么作用?使用三个连着的单引号可以吗?\n 的作用?
-- """ 多行
文本
"""
-- \n 转义字符,表示新起一行 是否还有其他类似的转义字符?
代码:
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJuly\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There's something going on here.
With the three double-guotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
结果:
习题10:那是什么?
备注:更多的转义字符 -- \t \\ \' \" \r ...
"I "understand" joe." -- 这样表达会有什么问题?怎样解决呢?
-- 会认为understand左侧的引号为结束引号,将报错,可通过转义字符解决 "I \"understand\" joe."
代码:
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
结果:
python入门 -- 学习笔记1的更多相关文章
- Python入门学习笔记4:他人的博客及他人的学习思路
看其他人的学习笔记,可以保证自己不走弯路.并且一举两得,即学知识又学方法! 廖雪峰:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958 ...
- Python入门学习笔记
了解 一下Python中的基本语法,发现挺不适应的,例如变量经常想去指定类型或者if加个括号之类的.这是在MOOC中学习到的知识中一点简单的笔记. Python的变量和数据类型: 1.Python这种 ...
- python入门学习笔记(三)
10.函数 求绝对值的函数 abs(x) 也可以在交互式命令行通过 help(abs) 查看abs函数的帮助信息.调用 abs 函数:>>> abs(100)100>>& ...
- Python入门 学习笔记 (二)
今天学习了一些简单的语法规则,话不多说,开始了: 二.数据类型 常用数据类型中的整形和浮点型就不多说了. 1.字符串 ...
- Python入门 学习笔记 (一)
原文地址:http://www.cnblogs.com/lujianwenance/p/5939786.html 说到学习一门语言第一步就是先选定使用的工具(环境).因为本人是个小白,所以征求了一下同 ...
- python入门学习笔记(二)
6.6替换元素 7.tuple类型 7.1创建tuple 7.2创建单元素tuple 7.3"可变"的tuple 8.条件判断和循环 8.1,if语句 8.2,if... ...
- python入门学习笔记(一)
写在开头: A:python的交互式环境 ...
- python入门 -- 学习笔记4
习题38:列表的操作 当你看到像 mystuff.append('hello') 这样的代码时,你事实上已经在 Python 内部激发了一个连锁反应.以下是它的工作原理: 1. Python 看到你用 ...
- python入门 -- 学习笔记3
习题21:函数可以返回东西 过程解析: 1.定义函数:如def add(形参)函数 2.调用函数: add(实参) 别忘记加() 3.在函数调用的时候将实参的值传给形参,代入到函数中进行计算,r ...
随机推荐
- Spring Cloud 请求重试机制核心代码分析
场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...
- 20155208徐子涵《网络对抗》Exp2 后门原理与实践
20155208徐子涵<网络对抗>Exp2 后门原理与实践 基础问题回答 (1)例举你能想到的一个后门进入到你系统中的可能方式? 答:当我们在非官方网站上下载软件时,后门极有可能会进入我们 ...
- 15.python并发编程(线程--进程--协程)
一.进程:1.定义:进程最小的资源单位,本质就是一个程序在一个数据集上的一次动态执行(运行)的过程2.组成:进程一般由程序,数据集,进程控制三部分组成:(1)程序:用来描述进程要完成哪些功能以及如何完 ...
- 学习笔记TF039:TensorBoard
首先向大家和<TensorFlow实战>的作者说句不好意思.我现在看的书是<TensorFlow实战>.但从TF024开始,我在学习笔记的参考资料里一直写的是<Tenso ...
- 用我所学去讲C语言指针
文章更新,更加详细的介绍请看这篇:https://www.cnblogs.com/lulipro/p/7460206.html 很多人不敢讲C的指针,有些人讲不清,有些人怕讲错.初生牛犊不怕虎,就让我 ...
- java_basic_基础
变量 类型 运算符 条件 循环 调试 字符串 数组 从键盘输入数据 switch的用法 从变量接收值 从键盘接收值 输出到一个文件 基本类型的赋值与引用类型的赋值是不一样的 是将引用类型的地址 每一个 ...
- Nginx源码结构及如何处理请求
一.源码结构 1:下载安装包后,解压,可以看到目录结构,其中src目录下放的是源码 2:src源码目录下,可以看到这几个目录 mail:mail目录中存放了实现Nginx服务器 ...
- mnist全连接层网络权值可视化
一.数据准备 网络结构:lenet_lr.prototxt 训练好的模型:lenet_lr_iter_10000.caffemodel 下载地址:链接:https://pan.baidu.com/s/ ...
- NotePad++ 配置Python工作环境
下载地址:https://notepad-plus-plus.org/ Current Version: 7.5.3 sss 显示空格和指标符 为什么建议这么作?因为判断Python语句是否在同一层次 ...
- Azure SQL 数据库仓库Data Warehouse (4) 2018 TechSummit 动手实验营
<Windows Azure Platform 系列文章目录> 上传一下之前在2018 TechSummit的动手实验营:Azure数据仓库PaaS项目架构规划与实战入门 包含PPT和Wo ...