Python基础-变量和简单数据类型


print("Hello Python world") #print() 函数

message = "Hello Python world" #message 变量
print(message)

message = "Hello Python world again" #message 变量值可以修改
print(message)

#变量命名规则
#字母、数字、下划线组成;只能以字母或下划线开头
#不能包含空格
#不能用关键字和函数名
#简短,且具有描述性 如 student_name
#慎用小写字母l和大写字母O,区分1和0
print("--------------------------------------------")
#字符串 单引号或双引号
name = "i love china"
#单词首字母大写: 方法title()
print(name.title())
#全部大写 upper()
print(name.upper())
#全部小写 lower()
print(name.lower())
print("--------------------------------------------")

#字符串合并 输出
first_name = "zhang"
last_name = "san"
full_name = f"{first_name} {last_name}"
print(full_name)
#f是format的简写,python3.6及以上版本支持
#3.5版本及以下使用方法如下
# full_name = "{} {}".format(first_name, last_name)
print(f"Hello,{full_name.title()}!")
#f字符串 创建变量
message = f"Hello,{full_name.title()}!"
print(message)
print("--------------------------------------------")

# \n 换行 \t 制表符
print("\tPyhon")
print("hello\nhow are you today\nfine,thank you!")
print("--------------------------------------------")

# 删除空格
# 末尾空格 rstrip()
# 开头空格 lstrip()
message = " hello "
message = message.rstrip()
print(message)
message = message.lstrip()
print(message)
print("--------------------------------------------")

# 整数 + - * /
print(2 + 3)
print(3 - 2)
print(2 * 3)
print(3 / 2)
# ** 乘方
print(3 ** 2) #3的2次方
print(3 ** 3) #3的3次方
print("--------------------------------------------")

# 浮点数 结果包含的小数位数可能是不确定的
print(0.1 + 0.1)
print(0.2 + 0.1)
print(2 * 0.1)
print(3 * 0.1)
print("--------------------------------------------")

# 任意数相除,结果总为浮点数;
# 有浮点数参与的运算,结果也为浮点数
# 数值很大,可用下划线给数字分组
value = 1_000_000_000_000
print(value)
print("--------------------------------------------")

# 同时给多个变量赋值,用逗号隔开
x, y, z = 0,1,2
print(x,y,z)
print("--------------------------------------------")

# 常量,一般定义时使用全大写字母
MAX_VALUE = 1000
print("--------------------------------------------")

练习:

message = "I love China"
print(message)

message = "I love China"
print(message)

message = "I love China very much"
print(message)

name = "Eric"
print(f"Hello {name},would you like to learn some Python today?")

name = "zhang san"
print(name.title())
print(name.upper())
print(name.lower())

print('Albert Einstein once said, "A person who never made a mistake')
print('never tried anything new."')

name = "\tEric Matthes\n"
print("Unmodified:")
print(name)
print("\nUsing lstrip():")
print(name.lstrip())
print("\nUsing rstrip():")
print(name.rstrip())
print("\nUsing strip():")
print(name.strip())

fav_num = 8
msg = f"My favorite number is {fav_num}."
print(msg)

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
发出的红包

打赏作者

菜鸟程序员__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值