详解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编程中用close()方法关闭文件的教程
May 24 Python
python装饰器与递归算法详解
Feb 18 Python
Python的Flask框架中的Jinja2模板引擎学习教程
Jun 30 Python
python线程、进程和协程详解
Jul 19 Python
Python学生成绩管理系统简洁版
Apr 05 Python
用Python实现数据的透视表的方法
Nov 16 Python
python自定义函数实现一个数的三次方计算方法
Jan 20 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
May 28 Python
python 实现12bit灰度图像映射到8bit显示的方法
Jul 08 Python
python实现函数极小值
Jul 10 Python
Python类成员继承重写的实现
Sep 16 Python
pandas统计重复值次数的方法实现
Feb 20 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中extract()函数的定义和用法
2012/08/17 PHP
深入extjs与php参数交互的详解
2013/06/25 PHP
php表单处理操作
2017/11/16 PHP
PHP设计模式之适配器模式定义与用法详解
2018/04/03 PHP
滚动条变色 隐藏滚动条与双击网页自动滚屏显示代码
2009/12/28 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
轻松创建nodejs服务器(5):事件处理程序
2014/12/18 NodeJs
不同编码的页面表单数据乱码问题解决方法
2015/02/15 Javascript
JavaScript简单实现弹出拖拽窗口(二)
2016/06/17 Javascript
浅谈JavaScript对象与继承
2016/07/10 Javascript
jQuery实现鼠标跟随效果
2017/02/20 Javascript
JavaScript实现三级级联特效
2017/11/05 Javascript
详解微信小程序-扫一扫 wx.scanCode() 扫码大变身
2019/04/30 Javascript
JS原型对象操作实例分析
2020/06/06 Javascript
微信小程序swiper组件实现抖音翻页切换视频功能的实例代码
2020/06/24 Javascript
[02:04]2020年夜魇暗潮预告片
2020/10/30 DOTA
Python中MYSQLdb出现乱码的解决方法
2014/10/11 Python
详解在Python的Django框架中创建模板库的方法
2015/07/20 Python
Python中的字典与成员运算符初步探究
2015/10/13 Python
python判断字符串是否是json格式方法分享
2017/11/07 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
python多进程并行代码实例
2019/09/30 Python
python 通过手机号识别出对应的微信性别(实例代码)
2019/12/22 Python
python pyg2plot的原理知识点总结
2021/02/28 Python
德国游戏机商店:Konsolenkost
2019/12/08 全球购物
求职信格式范本
2013/11/15 职场文书
3.15国际消费者权益日主题活动活动总结
2014/03/16 职场文书
授权委托书范本(单位)
2014/09/28 职场文书
幼儿园圣诞节活动总结
2015/05/06 职场文书
小学英语教师研修感悟
2015/11/18 职场文书
Python中使用Lambda函数的5种用法
2021/04/01 Python
Go语言中的UTF-8实现
2021/04/26 Golang
JavaGUI模仿QQ聊天功能完整版
2021/07/04 Java/Android
Python中三种花式打印的示例详解
2022/03/19 Python
nginx 添加http_stub_status_module模块
2022/05/25 Servers