首页 > 编程语言 >代码卡(python)

代码卡(python)

时间:2022-10-24 00:00:18浏览次数:40  
标签:sayWelcome python 代码 who FriendName print self def

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)

  

 

标签:sayWelcome,python,代码,who,FriendName,print,self,def
From: https://www.cnblogs.com/ypyyds/p/16820112.html

相关文章

  • Python: Iterator Pattern
    DuIterator.py#迭代器模式IteratorPattern#DoublyLinkedListclassNode(object):def__init__(self,value,nextNode,prevNode):self.value=......
  • php代码审计刷题wp
    反斜杠被过滤,利用cd回到根目录<?phperror_reporting(0);if(isset($_GET["cmd"])){if(preg_match('/et|echo|cat|tac|base|sh|more|less|tail|vi|head|nl|env|fl|\|......
  • create-function函数代码注入
    creat_function()代码注入creat_function函数根据传递的参数创建匿名函数,并为其返回唯一名称。语法:create_function(string$args,string$code)string$args声明的函......
  • 如何使用git通过ssh协议拉取gitee上的项目代码——如何正确的免密使用git
    如何在gitee网站上生成/添加SSH公钥见教程:生成/添加SSH公钥    测试公私秘钥是否成功:[email protected]     ===============================......
  • Python入门练习(Question4)
    年份天数题目输入某年某月某日,判断这一天是这一年的第几天?特殊情况,闰年时需考虑二月多加一天解答year=int(input("inputyear:"))month=int(input("inputmont......
  • python中format的详解
    format是字符串内嵌的一个方法,用于格式化字符串。以大括号{}来标明被替换的字符串。它通过{}和:来代替%。1、基本用法1.按照{}的顺序依次匹配括号中的值s="{}isa{}......
  • 关于两行代码让我时间无限拉长这件事------mainServlet
    在再一次尝试实现增删改查的功能时,出现了一些BUG,修改功能一直实现不了,查了好久的资料,这才发现问题如上图所示,这个Servlet里面定义的方法,虽然很短,但是重要的很奥,改了好久......
  • Python入门练习(Question3)
    完全平方数题目一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?解答defsolution3():n=10whileTrue:n+=1......
  • 初学编程三大件之代码管理-->git的使用
    如果想成为一名合格的测试开发/自动化工程师,git知识是必不可少的。为什么这么说呢,因为如果想对代码进行版本管理,git工具是首选。下面说下什么是git :1.Git是一个开源的分......
  • 十分钟带你搞懂Python获取网页信息
    Python爬虫的用处就不需要我多说了吧,今天就来带大家十分钟快速学会Python是如何爬取网页信息的,当然大家在爬取目标网页内容之前一定要遵守该网页的爬虫规则,以免带来不必要的......