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

作者:lixiang0522 时间:2023-07-23 11:31:45 

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


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,变量
0
投稿

猜你喜欢

  • javascript生成大小写字母

    2024-04-17 10:26:30
  • Python实现简单的文本相似度分析操作详解

    2023-07-18 11:16:12
  • ActiveMQ:使用Python访问ActiveMQ的方法

    2022-04-19 01:48:30
  • FSO中的SubFolders 属性介绍

    2008-01-05 13:57:00
  • 利用CSS改善网站可访问性

    2010-10-20 20:12:00
  • Mysql判断表字段或索引是否存在

    2024-01-24 00:25:39
  • 教你用python3根据关键词爬取百度百科的内容

    2023-12-28 16:30:24
  • Perl命令行应用程序详解

    2023-08-09 19:01:18
  • 用伪类:hover实现提示效果

    2008-05-29 12:59:00
  • python画一个圣诞树实现示例

    2021-06-12 03:54:53
  • 浅谈dataframe中更改列属性的方法

    2022-12-08 02:00:06
  • golang简单读写文件示例

    2024-05-22 10:13:21
  • Python在后台自动解压各种压缩文件的实现方法

    2022-10-04 17:59:59
  • Python实现批量生成,重命名和删除word文件

    2022-12-03 05:51:33
  • MySQL数据库中对前端和后台进行系统优化

    2009-01-04 13:39:00
  • Mysql存储过程学习笔记--建立简单的存储过程

    2024-01-23 14:41:21
  • W3C网页内容无障碍指南2.0(WCAG)

    2008-11-20 13:40:00
  • Python用dilb提取照片上人脸的示例

    2021-07-04 23:34:47
  • Django中的FBV和CBV用法详解

    2023-09-15 10:41:06
  • Vue前端表格导出Excel文件的图文教程

    2024-04-09 10:46:45
  • asp之家 网络编程 m.aspxhome.com