详解Python中的变量及其命名和打印


Posted in Python onMarch 11, 2016

在程序中,变量就是一个名称,让我们更加方便记忆。

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 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."

提示:下划线一般用在变量名中表示假想的空格。让变量名的可读性高一点。

运行结果:

root@he-desktop:~/mystuff# python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
root@he-desktop:~/mystuff#


更多的变量和打印
现在我们输入更多的变量并打印他们,通常我们用""引住的叫字符串。

字符串是相当方便的,在练习中我们将学习怎么创建包含变量的字符串。有专门的方法将变量插入到字符串中,相当于告诉Python:“嘿,这是一个格式化字符串,把变量放进来吧。”

输入下面的程序:

# -- coding: utf-8 -- 
my_name = 'Zed A. Shaw' 
my_age = 35 # 没撒谎哦 
my_height = 74 # 英寸 
my_weight = 180 # 磅 
my_eyes = 'Blue' 
my_teeth = 'White' 
my_hair = 'Brown'

  

print "let's talk about %s." % my_name 
print "He's %d inches tall." % 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

# 下面这行比较复杂,尝试写对它。 
print "If I add %d, %d, and %d I get %d." % ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight)

提示:如果有编码问题,记得输入第一句。

运行结果:

root@he-desktop:~/mystuff# python ex5.py
let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
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, 74, and 180 I get 289.
root@he-desktop:~/mystuff#
Python 相关文章推荐
python执行shell获取硬件参数写入mysql的方法
Dec 29 Python
python图像处理之镜像实现方法
May 30 Python
举例讲解Python设计模式编程中的访问者与观察者模式
Jan 26 Python
python中如何使用正则表达式的非贪婪模式示例
Oct 09 Python
python使用matplotlib画饼状图
Sep 25 Python
selenium 安装与chromedriver安装的方法步骤
Jun 12 Python
Python代码生成视频的缩略图的实例讲解
Dec 22 Python
使用Tkinter制作信息提示框
Feb 18 Python
Python MySQLdb 执行sql语句时的参数传递方式
Mar 04 Python
服务器端jupyter notebook映射到本地浏览器的操作
Apr 14 Python
python实现与redis交互操作详解
Apr 21 Python
Python  lambda匿名函数和三元运算符
Apr 19 Python
Python基本语法经典教程
Mar 11 #Python
Python使用PIL库实现验证码图片的方法
Mar 11 #Python
Python2.x利用commands模块执行Linux shell命令
Mar 11 #Python
Python实现列表转换成字典数据结构的方法
Mar 11 #Python
python中enumerate函数遍历元素用法分析
Mar 11 #Python
python实现class对象转换成json/字典的方法
Mar 11 #Python
Windows下Python的Django框架环境部署及应用编写入门
Mar 10 #Python
You might like
PHP中return 和 exit 、break和contiue 区别与用法
2012/04/09 PHP
JavaScript 序列化对象实现代码
2009/12/18 Javascript
javascripit实现密码强度检测代码分享
2013/12/12 Javascript
JS模拟键盘打字效果的方法
2015/08/05 Javascript
jquery实现像栅栏一样左右滑出式二级菜单效果代码
2015/08/24 Javascript
javascript倒计时效果实现
2015/11/12 Javascript
AngularJS教程之MVC体系结构详解
2016/08/16 Javascript
AngularJs基于角色的前端访问控制的实现
2016/11/07 Javascript
angular2+nodejs实现图片上传功能
2017/03/27 NodeJs
微信小程序网络请求的封装与填坑之路
2017/04/01 Javascript
使用Xcache缓存器加速PHP网站的配置方法
2017/04/22 Javascript
JavaScript实现的冒泡排序法及统计相邻数交换次数示例
2017/04/26 Javascript
js Array.slice的8种不同用法示例
2019/07/10 Javascript
python动态加载变量示例分享
2014/02/17 Python
关于你不想知道的所有Python3 unicode特性
2014/11/28 Python
Python 中 list 的各项操作技巧
2017/04/13 Python
Python通过matplotlib画双层饼图及环形图简单示例
2017/12/15 Python
Python读取stdin方法实例
2019/05/24 Python
numpy linalg模块的具体使用方法
2019/05/26 Python
numpy下的flatten()函数用法详解
2019/05/27 Python
python 多进程并行编程 ProcessPoolExecutor的实现
2019/10/11 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
2020/06/02 Python
python中的错误如何查看
2020/07/08 Python
Pycharm添加虚拟解释器报错问题解决方案
2020/10/13 Python
CSS3制作酷炫的条纹背景
2017/11/09 HTML / CSS
使用HTML5的表单验证的简单示例
2015/09/09 HTML / CSS
Clarks英国官方网站:全球领军鞋履品牌
2016/11/26 全球购物
澳大利亚首屈一指的在线购物目的地:Kogan.com
2017/02/02 全球购物
J2SDK1.5与J2SDK5.0有什么区别
2012/09/19 面试题
工厂门卫岗位职责范本
2014/04/04 职场文书
学生夜不归宿检讨书
2014/09/23 职场文书
教师个人自我剖析材料
2014/09/29 职场文书
董事长秘书工作总结
2015/08/14 职场文书
学习新党章心得体会2016
2016/01/15 职场文书
Django Paginator分页器的使用示例
2021/06/23 Python
PostgreSQL数据库创建并使用视图以及子查询
2022/04/11 PostgreSQL