我的python学习之路----语法

来源:互联网 发布:网络创业项目策划书 编辑:程序博客网 时间:2024/06/01 08:25

1、变量

python变量不需要声明变量类型,通过=号进行赋值,如

str = 'hello'

div = 20/3


2、数学计算

 >>> 10/3
3.3333333333333335
>>> 10//3
3
>>> 10//-3
-4
>>> 10%3
1
>>>

3、字符串

使用单引号或者多引号定义,使用""" """定义多行文本。

>>> """hello
world"""
'hello\nworld'


4、注释

python的单行注释以#号开始,如:

str = 'hello' #strng variable

python本身并不提供多行注释,如果要使用多行注释,可以使用

if 0:

包含多行文字

5、打印

打印字符串:

print(value1,value2,...),如:

>>> print(s,"hello")
fdsfa hello

格式化输出值:

>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print(strHello)
the length of (Hello World) is 11


原创粉丝点击