1.打印

 
print(1024)

  

2.注释

 
# this is the first code

  

3.字符串

 
print("hello world")

  

4.赋值

 
Name = "Tony"
program = "夜曲编程"

  

5.格式化输出

 
Name = "tony"
Age = 18
print(f"My name is {Name}")
print(f"My age is {Age}")

  

6.四则运算

 a = 1 +2 
 print(a)

  

7.取整和取模

 print(7 // 3)
 print(7 % 3)

  

8.比较运算

 a = 3
 b = 1
 print(a > b)

  

9.逻辑运算

 a = True
 b = False
 print(a and b)
 print(a or b)
 print(not a)

  

10.if判断

 yourAge = 20
 myAge = 18
 if yourAge > myAge:
     print("你的年龄比我大")

  

11.if-else

 yourScore = 80
 myScore = 90
 if yourScore > myScore:
     print("你的得分比我高")
 else:
     print("你的得分不比我高")

  

12.elif

 day = 3
 if day == 1:
     print("元气满满地工作")
 elif (day >= 2) and (day <= 5):
     print("继续搬砖吧")
 else:
     print("世界这么大,出去走走吧")

  

13.if嵌套

 time = 13
 num = 2
 if (time >= 9) and (time <= 23):
     print("在打折时间内")
     if num >=2:
         print("全场八折")

  

14.元组

 calendar = ("1月1日","1月2日")
 print(calendar)

  

15.in 运算

 numbers = (0,1,2,3,5,8,13,21)
 result = 5 in numbers
 print(result)

  

16.列表

 FriendName = ["kevin","tony","asum"]
 print(FriendName)

  

17.切片

 someletters = ["a","b","c","d","e"]
 print(someletters[1:3])

  

18.列表的修改

 FriendName = ["kevin","tony","asum"]
 print(FriendName)
 print(FriendName[0])
 FriendName[0] = "Jessica"
 print(FriendName)

  

19.列表的追加

 FriendName =["Max","Jeremy"]
 FriendName.append("Vane")
 print(FriendName)

  

20.列表的插入

 rank = ["Max","Jeremy","Christina"]
 rank.insert(1,"May")
 print(rank)

  

21.列表删除元素

 prime_number = [1,3,5,7,9,10]
 prime_number.pop()
 prime_number.pop(1)
 print(prime_number)

  

22.字典

 productItems = {"口香糖":15,"可乐":5}
 print(productItems)

  

23.字典的添加修改

 product = {"name":"可乐","type":"饮品"}
 product["other"] = "七折"

  

24.for循环

 NumList = [10,20,30,40,50,60]
 for i in NumList:
     print(i)

  

25.累加

 jdList = [3,1.5,4,2]
 total = 0
 for price in jdList:
     total = total + price
 print(total)

  

26.while循环

 Counter = 0
 while Counter < 5:
     print(nameList[Counter])
     Counter = Counter + 1

  

27.break

 NumList = [10,4,1,20,6]
 for i in NumList:
     print(i)
     if i > 15:
         break
 print(NumList)

  

28.continue

 drinks = ["water","coke","tea"]
 for item in drinks:
     if item == "coke"
         continue
     print(item)

  

29.嵌套循环

 brandList = ["Jamer","Cbp","Sk3"]
 itemList = ["精华","面霜","眼霜"]
 for brand in brandList:
     for item in itemList:
         print(f"获得{brand}的{item}")

  

30.遍历整数列表

 for i in range(5):
     print(i)

  

31.定义函数

 def sayWelcome():
     print("欢迎光临")
     print("商品一律九折")

  

32.调用函数

 def sayWelcome():
     print("欢迎光临")
     print("商品一律九折")
 sayWelcome()

  

33.为函数设置参数

 def sayWelcome(discount):
     print("欢迎光临")
     print("商品一律{discount}")
 sayWelcome("九折")
 sayWelcome("五折")

  

34.函数返回值

 def getArea(length):
     area = length * length
     return area
 result = getArea(13)
 print(result)

  

35.默认参数

 def getArea(radius,pi = 3.14):
     ret = radius * radius * pi
     print(ret)
 getArea(2)
 getArea(2,3.14159)

  

36.匿名函数

 square = lambda x:x*x
 result = square(9)
 print(result)

  

37.递归

 def sum(5):
     if n <= 0:
         return 0
     return 5 + sum(4)

  


     

38.类的定义

 class phone():
     def makecall(self,who):
         return f"打给{who}"
     def senMsg(self,who,txt):
         return f"将{txt},发给{who}"

  

39.实例化对象

 class phone():
     def makecall(self,who):
         return f"打给{who}"
     def senMsg(self,who,txt):
         return f"将{txt},发给{who}"
 myphone = phone()
 ret = myphone.makecall("Tony")
 print(ret)

  

40.类的属性与初始化

 class phone():
     def_init_(self,bd,clr):
         self.brand = bd
         self.color = clr
 myphone = phone("华为","白色")
 print(myphone.color)
 print(myphone.brand)

  

41.导入模块1

 import random
 randomNum = random.random()
 print(randomNum)

  

42.导入模块2

 from datetime import date
 todayDate = date.today()
 print(todayDate)

  

43.安装模块

 windows     pip install [modulename]
 mac     pip3 install [modulename]

  

### 44.使用with打开文件

 path = "/Users/aLing/bobo.jpg"
 with open(path,"rb") as imageFile:
     print(imageFile)

  

 

原文地址:http://www.cnblogs.com/ypyyds/p/16820112.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性