python
学习一门编程语言,最有效的方式就是敲代码。
请打开(系统)命令行,输入py,等提示符(>>>)出现后,逐行输入下面的内容(每行结束时记得按回车),观察结果。
(不必输入从“#”开始到该行末尾的内容)
简单字符串
请忽略我的英文水平
python不在意所用的是单引号还是双引号,只要成对就好:
print('Hello,', "World!")
但假如内容本身就包含引号,得注意一下:
print("I'm python,", 'a "glue language". ')
一对儿三个连续的同类引号可以用一个print打印多行内容:
print('''
I was born in 1989 and known by the public in 1991.
My father is Guido van Rossum.
''')
print("""
I am friendly. So many people love me and help me to grow stronger.
I am smart. That's means I can do lots of things in a very short time.
And I am easy to learn, though become a master is hard anyway.
""")
数字及其运算
print不只能打印被引起的内容(所谓“字符串”),它能打印数字:
print(5)
print(16, -1, 1.57)
还能进行运算(2.x版本会出现的结果略有不同):
print(4 + 3)
print(7.5 - 8)
print(9 * 105)
print(2 / 7)
print(25 % 7) # 取余运算,支持小数、负数
print(3 ** 4) # 指数运算
print(15.2 ** -3.1) # 支持小数
print(1.3 + 5/6 * (7.19 - 7) - 8 * 9 ** 1.7) # 运算顺序与数学上约定一致,推荐使用圆括号以清楚标识
print("2 + 6 * 8") # 如果有强迫症想加引号试试,那么会得到“2+6*8”这个字符串,而非一个“数”
字符串运算
而且不只是对数字进行运算:
print("Hello " + "World!")
print("Hello World!" * 6)
print("Hello World!" + 4) # 这么做会出错的原因是,虽然5个梨加3个苹果等于8个水果没错,但python总结不出来“水果”这个概念
有任何问题请回复提出。然后欢迎关注微信公众号格物致愚:
格物致愚