详解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删除指定类型(或非指定)的文件实例详解
Jul 06 Python
Python+selenium实现截图图片并保存截取的图片
Jan 05 Python
python实现聚类算法原理
Feb 12 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
Aug 16 Python
python django框架中使用FastDFS分布式文件系统的安装方法
Jun 10 Python
基于python实现把图片转换成素描
Nov 13 Python
浅谈python多线程和多线程变量共享问题介绍
Apr 17 Python
Python使用matplotlib绘制圆形代码实例
May 27 Python
python文件编写好后如何实践
Jul 07 Python
Python基于mediainfo批量重命名图片文件
Dec 29 Python
Python 实现RSA加解密文本文件
Dec 30 Python
教你怎么用Python监控愉客行车程
Apr 29 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
评分9.0以上的动画电影,剧情除了经典还很燃
2020/03/04 日漫
php项目开发中用到的快速排序算法分析
2016/06/25 PHP
php根据用户名和手机号查询是否存在手机号码
2017/02/16 PHP
select组合框option的捕捉实例代码
2008/09/30 Javascript
jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
2011/06/28 Javascript
jquery获得option的值和对option进行操作
2013/12/13 Javascript
浅谈Javascript数组(推荐)
2016/05/17 Javascript
AngularJS中$http的交互问题
2017/03/29 Javascript
手写Node静态资源服务器的实现方法
2018/03/20 Javascript
angular异步验证防抖踩坑实录
2019/12/01 Javascript
js实现时钟定时器
2020/03/26 Javascript
javascript实现计算器功能
2020/03/30 Javascript
JavaScript内置对象之Array的使用小结
2020/05/12 Javascript
Node.js API详解之 readline模块用法详解
2020/05/22 Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
2020/06/23 Javascript
详解node.js创建一个web服务器(Server)的详细步骤
2021/01/15 Javascript
在python的WEB框架Flask中使用多个配置文件的解决方法
2014/04/18 Python
python监控网站运行异常并发送邮件的方法
2015/03/13 Python
浅谈django中的认证与登录
2016/10/31 Python
Python基于回溯法子集树模板解决数字组合问题实例
2017/09/02 Python
Python反转序列的方法实例分析
2018/03/21 Python
Python3.6.0+opencv3.3.0人脸检测示例
2018/05/25 Python
Python线程之定位与销毁的实现
2019/02/17 Python
Python列表(list)所有元素的同一操作解析
2019/08/01 Python
Tensorflow进行多维矩阵的拆分与拼接实例
2020/02/07 Python
浅谈django 重载str 方法
2020/05/19 Python
仿酷狗html5手机音乐播放器主要部分代码
2013/05/15 HTML / CSS
业务员岗位职责
2013/11/16 职场文书
小学毕业感言50字
2014/02/16 职场文书
工会主席事迹材料
2014/06/03 职场文书
商铺门面租房协议书
2014/10/21 职场文书
2015年党员个人剖析材料
2014/12/18 职场文书
酒店办公室主任岗位职责
2015/04/01 职场文书
2015年服务员个人工作总结
2015/05/27 职场文书
礼仪培训心得体会
2016/01/22 职场文书
OpenCV-Python模板匹配人眼的实例
2021/06/08 Python