Python1-基础

Philosophy is “Simplicity is the best”.

需要一个解释器来运行Python程序:

网页版在线解释器;

Windowns下的IDLE(Integrated Development Environment);

Linux系统一般预装了Python解释器,输入”python”即可启动

1>>> print ("Hellow World!")
2Hellow World!

#开头的为注释行,会被解释器忽略

print函数打印了一句话,并默认在文本最后加上了换行符(不同于C)

print在Python2中不是函数,只是一个关键字,但在Python3中是函数,因此必须加圆括号

在C、C++和JAVA等程序中需要指定变量类型,但Python可自动识别变量类型,包括整数型、浮点型、字符型甚至是字符串

在Python中定义变量并赋值,只需要=即可

1>>> myNumber=3
2>>> print(myNumber)
33
4>>> myNumber="helloworld"
5>>> print(myNumber)
6helloworld

Python中有4种内置的数据结构,分别是List、Dictionary、Tuple和Set

List是Python中最基础的数据结构,被创建后可以添加项目,用append()函数来往已有的list中添加数据。

1>>> nums=[]
2>>> nums.append(21)
3>>> print(nums)
4[21]
5>>> nums.append("String")
6>>> print(nums)
7[21'String']

Python中单行注释用#开头,多行注释用”’或者”””括住需要注释的内容,注释的内容不会被解释器解释,如

 1>>> ##this is a comment
2>>> """this is a comment
3and this is another comment"""

4'this is a commentnand this is another comment'
5>>> 
6>>> """
7one comment
8two comment
9three comment
10"""

11'none commentntwo commentnthree commentn'
12>>> #command

Input()函数可以读取用户输入的程序

1>>> name=input("Enter your name: ")
2Enter your name: harssh
3>>> print("hello",name)
4hello harssh
1>>> num1=int(input("Enter num1: "))
2Enter num1: 5
3>>> num2=int(input("Enter num2: "))
4Enter num2: 4
5>>> num3=num1*num2
6>>> print("Product is: ",num3)
7Product is:  20

在Python中常用关键字”if”和”elif”和”else”来进行选择,此处特别注意缩进、if、elif和else都应该无缩进,其后面跟的执行语法应当缩进

1>>> num1=34
2>>> if(num1>12):
3    print("Num1 is good")
4elif(num1>35):
5    print("Num2 is not goooo...")
6else:
7    print("Num2 is great")
8
9Num1 is good

Python中用关键词”def”来定义函数,def的语法如下

1def function-name(agruments):
2    #function body
 1>>> def hello():
2    print("hello")
3    print("hello again")
4
5>>> 
6>>> hello()
7hello
8hello again
9
10###当你不小心写出个无限循环函数
11>>> def hello():
12    print("hello")
13    print("hello again")
14    hello()
15
16>>> hello()

没看懂的地方:main函数

 1###没看懂
2>>> def getInteger():
3    result = int(input("Enter integer: "))
4    return result
5
6>>> def Main():
7    print("Started")
8    output = getInteger()     
9    print(output)
10
11
12>>> if __name__=="__main__":
13    Main()
14
15
16Started
17Enter integer: 5
185

for循环

1>>> for i in range(3):
2    print(i)
3
4
50
61
72

Python中有很多module,用import来调用模块

 1###依旧看不懂的main函数
2>>> import math
3>>> def Main():
4    num=-85
5    num=math.fabs(num)
6    print(num)
7
8
9>>> if __name__=="__main__":
10    Main()
11
12
1385.0


原文始发于微信公众号(BioInfo):Python1-基础

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/238244.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!