详解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中的语句和语法
Aug 10 Python
Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法
Mar 13 Python
使用Python读取安卓手机的屏幕分辨率方法
Mar 31 Python
Python二叉树定义与遍历方法实例分析
May 25 Python
Python使用gRPC传输协议教程
Oct 16 Python
Linux下安装python3.6和第三方库的教程详解
Nov 09 Python
浅谈Python中的全局锁(GIL)问题
Jan 11 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
Python爬取阿拉丁统计信息过程图解
May 12 Python
Python自带的IDE在哪里
Jul 01 Python
python爬虫智能翻页批量下载文件的实例详解
Feb 02 Python
Python查找算法的实现 (线性、二分,分块、插值查找算法)
Apr 24 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 dirname(__FILE__) 获取当前文件的绝对路径
2011/06/28 PHP
PHP下载远程文件到本地存储的方法
2015/03/24 PHP
ThinkPHP在Cli模式下使用模板引擎的方法
2015/09/25 PHP
详解php设置session(过期、失效、有效期)
2015/11/12 PHP
php实现的二叉树遍历算法示例
2017/06/15 PHP
多次注册事件会导致一个事件被触发多次的解决方法
2013/08/12 Javascript
jquery判断元素是否隐藏的多种方法
2014/05/06 Javascript
JavaScript判断手机号运营商是移动、联通、电信还是其他(代码简单)
2015/09/25 Javascript
javascript实现拖动元素交换位置
2015/11/29 Javascript
理解js对象继承的N种模式
2016/01/25 Javascript
关于vue.js v-bind 的一些理解和思考
2017/06/06 Javascript
JavaScript设计模式之构造函数模式实例教程
2018/07/02 Javascript
Angular6封装http请求的步骤详解
2018/08/13 Javascript
Vue中多个元素、组件的过渡及列表过渡的方法示例
2019/02/13 Javascript
vue中template的三种写法示例
2020/10/21 Javascript
Python基于smtplib实现异步发送邮件服务
2015/05/28 Python
python+selenium实现登录账户后自动点击的示例
2017/12/22 Python
快速了解Python开发中的cookie及简单代码示例
2018/01/17 Python
python实现对求解最长回文子串的动态规划算法
2018/06/02 Python
Django框架实现的简单分页功能示例
2018/12/04 Python
使用python3实现操作串口详解
2019/01/01 Python
PyQt弹出式对话框的常用方法及标准按钮类型
2019/02/27 Python
python快速编写单行注释多行注释的方法
2019/07/31 Python
python实现宿舍管理系统
2019/11/22 Python
美国杂志订阅折扣与优惠网站:Magazines.com
2016/08/31 全球购物
瑰珀翠美国官网:Crabtree & Evelyn美国
2016/11/29 全球购物
新闻专业推荐信范文
2013/11/20 职场文书
临床医师专业个人自我评价
2014/01/08 职场文书
运动会开幕式邀请函
2014/01/22 职场文书
会议欢迎标语
2014/06/30 职场文书
2014年卫生工作总结
2014/11/27 职场文书
2014年终个人总结报告
2015/03/09 职场文书
财务出纳岗位职责
2015/03/31 职场文书
惊涛骇浪观后感
2015/06/05 职场文书
总结高并发下Nginx性能如何优化
2021/11/01 Servers
MySQL实现配置主从复制项目实践
2022/03/31 MySQL