简单的python入门语法

简单的python入门语法,第1张

概述简单的python入门语法

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/usr/bin/python3# 开始学习pythonprint("hello,world")# 条件语句a,b = 3,1if a < b:	print('a({}) is less than b({})'. format(a,b))else:	print('a({}) is great than b({})'. format(a,b))# ?: 模仿三元表达式print("foo" if a < b else "bar");# while循环 fabonaccia,b = 0,1 # 赋值 a = 0,b = 1while b < 50:	print(b)	a,b = b,a+bprint("Done.")	# for循环,迭代输出文本信息#lines.txt#01 This is a line of text#02 This is a line of text#03 This is a line of text#04 This is a line of text#05 This is a line of textfh = open("lines.txt")for line in fh.readlines():	print(line,end='')# 计算素数的函数,素数(只能被1和自己整除的数)def isprime(n):	if n == 1:		#print("1 is special")		return False	for x in range(2,n):		if n%x == 0:			#print("{} equals {} x {}".format(n,x,n // x))			return False	else:		#print(n,"is a prime")		return Truefor n in range(1,30):	isprime(n)# 迭代函数 primes,PHPer表示很难理解.# yIEld返回当前的素数,primes下次迭代时,将会从yIEld返回的数字开始。def primes(n = 1):	while(True):		if isprime(n): yIEld n		n += 1for n in primes():	if n > 100: break	print(n)# oop 基本类的定义class Fibonacci():	def __init__(self,a,b):		self.a = a		self.b = b	# 含有yIEld语法的应该都是一个构造器,可以内部迭代	def serIEs(self):		while (True):			yIEld(self.b)			self.a,self.b = self.b,self.a + self.b# 迭代构造器 Fibonacci.serIEs()f = Fibonacci(0,1)for r in f.serIEs():	if r > 100: break	print(r,end=' ')# 一个简单的mvc模式# oop2 继承与多态,高级概念# Duck,Person,Dog都继承AnimalActions# --- VIEW ---class AnimalActions:	def quack(self): return self._doAction('quack')	def feathers(self): return self._doAction('feathers')	def bark(self): return self._doAction('bark')	def fur(self): return self._doAction('fur')	def _doAction(self,action):		if action in self.strings:			return self.strings[action]		else:			return "The {} has no {}".format(self.animalname(),action)	def animalname(self):		return self.__class__.__name__.lower()# --- MODEL ---class Duck(AnimalActions):	strings = dict(		quack = "Quaaaak!",feathers = "The duck has gray and white feathers."			)class Person(AnimalActions): 	strings = dict(		quack = "The person iitates a duck!",feathers = "The person takes a feather from the ground and shows it.",bark = "The person says woof.",fur = "The person puts on a fur coat."	)class Dog(AnimalActions):    strings = dict(                bark = "Arf!",fur = "The dog has white fur with black spots."    )# --- CONTRolLER ---def in_the_doghouse(dog):	print(dog.bark())	print(dog.fur())def in_the_forest(duck):	print(duck.quack())	print(duck.feathers())def main():	donald = Duck()	john = Person()	fIDo = Dog()    # 三个对象都能在不同的地方拥有同样的行为	print("- In the forest:")	for o in ( donald,john,fIDo ):		in_the_forest(o)	print("- In the doghouse:")	for o in ( donald,fIDo ):		in_the_doghouse(o) if __name__ == '__main__': main()# 异常 PHPer表示很强大# 尝试打开一个不存在的文件try:	fh = open("xline.txt")	for line in fh.readlines():		print(line)except IOError as e:	print("something bad happend {}.".format(e))

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的简单的python入门语法全部内容,希望文章能够帮你解决简单的python入门语法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1198700.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存