【学习笔记】Python编程:从入门到实践(变量和简单数据类型)

【学习笔记】Python编程:从入门到实践<变量和简单数据类型>


用这一系列文章来记录自学《python编程:从入门到实践》一书中的所有代码,包括书中代码和课后练习。

第2章 变量和简单数据类型

2.1 hello_world

#2.1 hello_world.py
print("Hello Python world!")

2.2 变量

  • 尽量使用小写变量名
#2.2 变量
message = 'Hello Python world!'
print(message)

message = 'Hello Python Crash Course world!'
print(message)

练习

#2-1 simple_message
simple_message = 'My name is Nie Xinyu.'
print(simple_message)

#2-2 simple_messages
simple_messages = 'My name is Nie Xinyu.'
print(simple_messages)
simple_messages = 'I am from China.'
print(simple_messages)

2.3 字符串

#2.3.1 name.py 
name = "ada lovelace"
print(name.title())  #首字母大写
name = "Ada Lovelace"
print(name.upper())  #全部大写
print(name.lower())  #全部小写

#2.3.2 合并字符串
first_name = "ada"
last_name = 'lovelace'
full_name = first_name + ' ' + last_name  #用‘+’表示拼接

print(full_name)
print("Hello, " + full_name.title() + '!')

message = "Hello, " + full_name.title() + '!'
print(message)

#2.3.3 添加空白
print('Python')
print("\tPython")
print("Languages:\nPython\nC\nJavaScript")
print("Languages:\n\tPython\n\tC\n\tJavaScript")

#2.3.4 删除空白
favorite_language = ' python'
print(favorite_language)  #存在空白
print(favorite_language.rstrip())  #暂时消除句尾空白
print(favorite_language.lstrip())  #暂时消除句首空白
print(favorite_language.strip())  #暂时消除句首、尾空白

favorite_language = favorite_language.rstrip()  #永久句尾消除空白
print(favorite_language)

#2.3.5 apostrophe.py
message = "One of Python's strengths is its diverse community."
print(message)

练习

#2-3 个性化消息
name = 'Buranny'
print("Hello " + name + ", would you like to learn some Python today?")

#2-4 调整名字的大小写
name = 'Buranny'
print(name.lower())
print(name.upper())
print(name.title())

#2-5名言
print('Albert Einstein once said, “A person who never made a mistake never tried anything new.”')

#2-6 名言2
famous_person = 'Albert Einstein'
message = '“A person who never made a mistake never tried anything new.”'
print(famous_person + ' once said, ' + message)

#2-7 剔除人名中的空白
name = ' \t\tBuranny\t\nNie\t\n'
print(name)
print(name.lstrip())
print(name.rstrip())
print(name.strip())

2.4 数字

  • 使用两个**表示乘方
  • 函数str()将非字符值转化为字符串

练习

#2-8 数字8
print(5 + 3)
print(10 - 2)
print(2 * 4)
print(int(16 / 2))  #如果不加int(),结果将是8.0

#2-9 最喜欢的数字
number = 1
message = "My favorite number is " + str(number) + "."
print(message)

number = '1'  #也可以直接写成字符串的形式
message = "My favorite number is " + number + "."
print(message)

2.5 注释

  • 对方案编写有意义的注释

2.6 Python之禅

  • 大部分编程工作都是使用常见解决方案来解决简单的小问题,但这些小问题都包含在更庞大、更有创意空间的项目中
  • 漂亮、优雅、简洁、高效、易于理解
import this
'''
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值