#在Python脚本中定义变量,Python的解释器会在内存中给变量开辟对应数据类型的存储空间。 #所以Python中定义变量不需要声明数据类型,不论是整型数据类型还是浮点数数据类型或者字符串。 #Python是通过等号=来给变量赋值,变量赋值以后才会被创建。 #Python变量赋值等号左边是变量名,右边是存储在变量中的值。
数值类型(整型、浮点型)、浮点数、字符串、布尔类型、列表、元组(tuple)、字典等
Python中变量的赋值
在Python脚本中定义变量,Python的解释器会在内存中给变量开辟对应数据类型的存储空间。
所以Python中定义变量不需要声明数据类型,不论是整型数据类型还是浮点数数据类型或者字符串。
Python是通过等号=来给变量赋值,变量赋值以后才会被创建。
Python变量赋值等号左边是变量名,右边是存储在变量中的值。
import calendar
import time
# 数值类型,整型变量
age = 18
# 数值类型,浮点型
price = 10.5
# 数值类型,浮点型
money = 1000.012
# 使用单引号(')字符串
domain_one = 'fxzbcn.com'
# 使用双引号(")字符串
domain_two = "fxzbcn.com"
# 使用三引号(''')字符串
domain_three = '''fxzbcn.com
fxzbcn.com
fxzbcn.com
'''
# 布尔类型
no = False
# 布尔类型
yes = True
#列表
exampleLists = [
'fxzbcn.com', 300.25, 'fxzbcn', 100, ['fxzbcn.com', 'fxzbcn', 200, 250.25]
]
# 元组
tupExample = ('fxzbcn', '300', 2000, 1964)
# 字典
dictionaryExample = {'one': 100, 'two': 200}
#当前时间戳
timestamp = time.time()
#本地时间为
localtime = time.localtime(time.time())
#格式化本地时间
formatlocaltime = time.asctime( time.localtime(time.time()) )
# 格式化成2016-03-20 11:45:39 年月日时分秒
ymdhms = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 格式化成Sat Mar 28 22:24:24 2016 周月日时分秒年
wmdhmsy = time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
# 将时间字符串Sat Mar 28 22:24:24 2016 转换成时间戳
strwmdhmsy = "Sun Jan 23 10:24:49 2020"
inttimestamp = time.mktime(time.strptime(strwmdhmsy,"%a %b %d %H:%M:%S %Y"))
# 以下输出2020年1月份的日历
cal = calendar.month(2020, 1)
print(age)
print(price)
print(money)
print(domain_one)
print(domain_two)
print(domain_three)
print(no)
print(yes)
print(exampleLists)
print(exampleLists[0])
print(exampleLists[1])
print(exampleLists[2])
print(exampleLists[3])
print(exampleLists[4])
print(exampleLists[4][0])
print(exampleLists[4][1])
print(exampleLists[4][2])
print(exampleLists[4][3])
print(exampleLists[0:3])
print(exampleLists[3:])
print(tupExample)
print(tupExample[0])
print(dictionaryExample)
print(dictionaryExample['one'])
print("当前时间戳为:", timestamp)
print("本地时间为:", localtime)
print("格式化本地时间为:", formatlocaltime)
print("年月日时分秒:", ymdhms)
print("周月日时分秒年:", wmdhmsy)
print("日期字符串转换时间戳:", inttimestamp)
print("以下输出2020年1月份的日历", cal)
打印输出
18
10.5
1000.012
fxzbcn.com
fxzbcn.com
fxzbcn.com
fxzbcn.com
fxzbcn.com
False
True
['fxzbcn.com', 300.25, 'fxzbcn', 100, ['fxzbcn.com', 'fxzbcn', 200, 250.25]]
['fxzbcn.com', 300.25, 'fxzbcn', 100, ['fxzbcn.com', 'fxzbcn', 200, 250.25]]
fxzbcn.com
300.25
fxzbcn
100
['fxzbcn.com', 'fxzbcn', 200, 250.25]
fxzbcn.com
fxzbcn
200
250.25
['fxzbcn.com', 300.25, 'fxzbcn']
[100, ['fxzbcn.com', 'fxzbcn', 200, 250.25]]
('fxzbcn', '300', 2000, 1964)
fxzbcn
{'one': 100, 'two': 200}
100
当前时间戳为: 1648523852.4708664
本地时间为 : time.struct_time(tm_year=2020, tm_mon=1, tm_mday=23, tm_hour=10, tm_min=13, tm_sec=18, tm_wday=6, tm_yday=23, tm_isdst=0)
格式化本地时间为: Sun Jan 23 10:16:48 2020
年月日时分秒: 2020-01-23 10:20:46
周月日时分秒年: Sun Jan 23 10:24:49 2020
日期字符串转换时间戳: 1579746289.0
以下输出2020年1月份的日历 January 2020
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
多个变量同时赋值
创建一个整型对象变量 其value:100,四个变量同时指向同一个内存空间。
a = b = c = d = 100
print(a)
print(b)
print(c)
print(d)
打印输出
100
100
100
100
多个对象指定多个变量
age, price, domain = 88, 250, "fxzbcn.com"
整型对象88和250分别分配给变量age和price,字符串对象分配给变量domain。
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论