Python从新手到大师——02

进制转换

#! /usr/bin/env python
'''
@Time     : 2018/8/20 22:54
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test1.py
@Software : PyCharm
'''

author = 'damao'

import os
import sys

"""
python中2进制、八进制、16进制的转换
bin()  转2进制方法
int()   转10进制方法
oct()  转8进制方法
hex()  转16进制方法
"""

# 10进制转换成二进制
a = 10
print(bin(a))
# 10进制转8进制
a = 10
print(oct(a))
# 10进制转16进制
a = 10
print(hex(a))

name = 'damao'
print(len(name))
print(name.title())  # 首字母大写
print(name.upper()) # 全部大写
print(name.isupper()) # 判断是不是大写
print(name.startswith("da")) # 是不是以书面字符串开头
print(name.endswith("o")) # 是不是以什么结尾
str1 = b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(sys.getdefaultencoding()) # 获取当前默认的编码格式
# print(str1.encode(encoding='utf-8')) #
print(str1.decode("utf-8"),name.title())

练习一:华氏温度和摄氏度进行换算

#! /usr/bin/env python
'''
@Time     : 2018/8/20 22:55
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test2.py
@Software : PyCharm
'''
"""华氏温度和摄氏度进行换算"""

f = float(input("请输入华氏温度:"))
t = (f - 32) / 1.8
print("%.2f华氏温度=%d.1f摄氏度"%(f,t))  # %.2f 来控制保留小数点后多少位
# 此处可以换一种高级写法
print("{a}华氏温度={b}摄氏度".format(a=f,b=t))

练习二:计算圆的周长和面积

#! /usr/bin/env python
'''
@Time     : 2018/8/20 23:04
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test3.py
@Software : PyCharm
'''

import math

"""计算圆的周长和面积"""
radius = float(input("请输入圆的半径:"))
perimeter = 2 * math.pi * radius  # 计算圆的周长
area = math.pi * radius**2  # 计算圆的面积
print("圆的周长为:%.2f" %perimeter)
print("圆的面积为:%.2f" %area)

练习三:计算年份是不是闰年

#! /usr/bin/env python
'''
@Time     : 2018/8/20 23:15
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test4.py
@Software : PyCharm
'''

"""计算年份是不是闰年"""
year = int(input("请输入年份:"))
# 如果代码太长写成一行不便于阅读 可以使用\或()折行
year_is_leap = (year & 4 ==0 and year % 100 != 0
                or year % 400 == 0)
print(year_is_leap)

查看参数类型

#! /usr/bin/env python
'''
@Time     : 2018/8/20 23:23
@Author   : Damao
@Site     : Life is short. I use python.
@File     : test5.py
@Software : PyCharm
'''

a = 100
b = 1000000000000000000
c = 12.345
d = 1 + 5j
e = 'A'
f = 'hello, world'
g = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容