Python基础知识

  • 评论/注释
print('hello world') #注意在python3版本上print成为函数
  • 字符串常量
"It's a string"
  • 数字(主要包括整数和浮点数,没有long类型,int类型可以包含任意长度的整数)

>>> a = 2
>>> b = 1.23e3
>>> c = 1.23e-3
>>> a
2
>>> b
1230.0
>>> c
0.00123

  • 字符串(你编写的Python程序,几乎不可避免的包含字符串,所以请注意接下来的内容)

>>> string_a = 'hello world'
>>> string_b = "hello world"
>>> string_a
'hello world'
>>> string_b
'hello world'

>>> string_c = "It's you!"
>>> string_c
"It's you!"
>>> string_d = 'It"s you!'
>>> string_d
'It"s you!'

  • 格式化输出
>>> age = 20
>>> name = 'Swaroop'
>>> print('{0} was {1} years old when he wrote this book'.format(name, age))
Swaroop was 20 years old when he wrote this book
>>> print('Why is {0} playing with that python?'.format(name))
Why is Swaroop playing with that python?
>>>
>>>
>>> print('{} was {} years old when he wrote this book'.format(name, age))
Swaroop was 20 years old when he wrote this book
>>> print('Why is {} playing with that python?'.format(name))
Why is Swaroop playing with that python?
>>>
>>>
>>> print('{name} was {age} years old when he wrote this book'.format(name=name, age=age))
Swaroop was 20 years old when he wrote this book
>>> print('Why is {name} playing with that python?'.format(name=name))
Why is Swaroop playing with that python?
>>>
>>>
>>> print(f'{name} was {age} years old when he wrote this book')
Swaroop was 20 years old when he wrote this book
>>> print(f'Why is {name} playing with that python?')
Why is Swaroop playing with that python?

 

posted @ 2019-01-11 08:26  我来捣乱滴  阅读(59)  评论(0)    收藏  举报