详解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中的id()函数指的什么
Oct 17 Python
Python机器学习之SVM支持向量机
Dec 27 Python
Python中利用aiohttp制作异步爬虫及简单应用
Nov 29 Python
使用python搭建服务器并实现Android端与之通信的方法
Jun 28 Python
Numpy的简单用法小结
Aug 28 Python
基于python监控程序是否关闭
Jan 14 Python
python字符串替换re.sub()实例解析
Feb 09 Python
超全Python图像处理讲解(多模块实现)
Apr 13 Python
在pycharm中创建django项目的示例代码
May 28 Python
基于python实现matlab filter函数过程详解
Jun 08 Python
使用python求斐波那契数列中第n个数的值示例代码
Jul 26 Python
python与js主要区别点总结
Sep 13 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
SONY SRF-40W电路分析
2021/03/02 无线电
php目录管理函数小结
2008/09/10 PHP
PHP 实现类似js中alert() 提示框
2015/03/18 PHP
PHP 实现公历日期与农历日期的互转换
2017/09/13 PHP
PHP判断一个变量是否为整数、正整数的方法示例
2019/09/11 PHP
网易JS面试题与Javascript词法作用域说明
2010/11/09 Javascript
js禁止小键盘输入数字功能代码
2011/08/01 Javascript
在JavaScript中typeof的用途介绍
2013/04/11 Javascript
jQuery根据ID获取input、checkbox、radio、select的示例
2014/08/11 Javascript
JavaScript的9种继承实现方式归纳
2015/05/18 Javascript
javascript实现网站加入收藏功能
2015/12/16 Javascript
基于NodeJS+MongoDB+AngularJS+Bootstrap开发书店案例分析
2017/01/12 NodeJs
微信小程序实现瀑布流布局与无限加载的方法详解
2017/05/12 Javascript
vue 项目常用加载器及配置详解
2018/01/22 Javascript
解决npm管理员身份install时出现权限的问题
2018/03/16 Javascript
Vue 去除路径中的#号
2018/04/19 Javascript
详解nodejs解压版安装和配置(带有搭建前端项目脚手架)
2018/12/06 NodeJs
vue项目打包后上传至GitHub并实现github-pages的预览
2019/05/06 Javascript
JSON 入门教程基础篇 json入门学习笔记
2020/09/22 Javascript
js实现类选择器和name属性选择器的示例步骤
2021/02/07 Javascript
[03:46]显微镜下的DOTA2第七期——满血与残血
2014/06/20 DOTA
[02:07]TI9显影之尘系列 - Vici Gaming
2019/08/20 DOTA
17个Python小技巧分享
2015/01/23 Python
Python 爬虫学习笔记之单线程爬虫
2016/09/21 Python
Python 实现一行输入多个值的方法
2018/04/21 Python
Python生成短uuid的方法实例详解
2018/05/29 Python
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
2019/04/09 Python
python多线程与多进程及其区别详解
2019/08/08 Python
vue.js刷新当前页面的实例讲解
2020/12/29 Python
python Protobuf定义消息类型知识点讲解
2021/03/02 Python
TheFork葡萄牙:欧洲领先的在线餐厅预订平台
2019/05/27 全球购物
前台文员岗位职责
2013/12/28 职场文书
高级工程师英文求职信
2014/03/19 职场文书
学校四风问题对照检查材料思想汇报
2014/09/26 职场文书
离婚协议书样本
2015/01/26 职场文书
Centos7中MySQL数据库使用mysqldump进行每日自动备份的编写
2021/08/02 MySQL