Python数据工程数据工程——No.2基本语法元素的格式输出(一次性基本语法元素的格式输出(一次性
彻底入门彻底入门python))
一份一份Python代码彻底入门代码彻底入门Python,,Simple and Rough!!
注释详细~
参考代码下载链接
from operator import itemgetter
from datetime import date, time, datetime, timedelta
from math import exp, log, sqrt
import re
#Print a simple string
print("Output #1 : I'm exited to learn Python.")
#两个数值相加
x = 4
y = 5
z = x + y
print("Output #2 : Four plus five equals {0:d}.".format(z))
'''
输出格式解释
{} : 一个占位符,表示这里将要传入print语句中的一个具体的值
0 : 指向format()方法中的第一个参数
: : 用来分隔传入的值和它的格式
'''
#两个列表相加
a = [1, 2, 3, 4] b = ["first", "second", "third", "fourth"] c = a + b
print("Output #3 : {0}, {1}, {2}".format(a, b, c))
#整数
x = 9
print("Output #4 : {0}".format(x))
print("Output #5 : {0}".format(3 ** 4))
print("Output #6 : {0}".format(int(8.3) / int(2.7)))
#浮点数
print("Output #7 : {0:.3f}".format(8.3 / 2.7)) #保留3位小数
y = 2.5 * 4.8
print("Output #8 : {0:.1f}".format(y)) #保留一位小数
r = 8 / float(3)
print("Output #9 : {0:.2f}".format(r)) #保留2位小数
print("Output #10 : {0:.4f}".format(8.0 / 3)) #保留4位小数
#调用math库中的函数
print("Output #11 : {0:.4f}".format(exp(3))) #e^3
print("Output #12 : {0:.2f}".format(log(4))) #ln4
print("Output #13 : {0:.1f}".format(sqrt(81))) #平方根
#字符串
print("Output #14 : {0:s}".format('I\'m enjoying learning Python.'))
print("Output #15 : {0:s}".format("This is a long string. Without the backslash it \
would run off of the page on the right in the text \
editor and be very difficult to read and edit. \
By using the backslash you can split the long string \
into smaller strings on separate lines so that the whole string \
is easy to view in the text editor."))
print("Output #16 : {0:s}".format('''You can use triple single quotes for
multi-line comment strings.'''))
print("Output #17 : {0:s}".format("""You can also use triple double quotes for
multi-line comment strings."""))
string1 = "This is a " #定义字符串string1
string2 = "short string." #定义字符串string2
sentence = string1 + string2 #字符串连接
print("Output #18 : {0:s}".format(sentence))
print("Output #19 : {0:s} {1:s}{2:s}".format("she is", "very "*7, "beautiful."))
m = len(sentence) #字符串长度
print("Output #20 : {0:d}".format(m))
#split函数
string1 = "My deliverable is due in May"
string1_list1 = string1.split() #split默认用空格对字符串拆分