栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 小目标4

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 小目标4

Python Day 10

  1. Leap Year

I have a friend Cindy, she was actually born on Feb.29th, and I remember we celebrated her birthday together 2012 in the Maths place!
Leap Year was adapted in Gregorian Calendar.  There are Three Criteria:

  • The year can be evenly divided by 4, is a leap year, unless:

  • The year can be evenly divided by 100, it is NOT a leap year, unless:

  • The year is also evenly divisible by 400. Then it is a leap year. [caption id=“attachment_1848” align=“alignnone” width=“750”]

    pixel2013 / Pixabay[/caption]

Solution:

def is_leap(year):
    if year % 4 != 0:
 print("False")
    elif year % 4 ==0 and year % 100==0 and year % 40==0:
 print("Leap Year")
    elif year % 4==0 and year % 100==0 and year % 40!=0 :
 print("False")
    else:
 print("Leap Year")

Alternative Solutions:

#Alternative Solution
def is_leap1(year):   
return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

Output:

print(is_leap(1900))
False
2. Second Largest Number in a List

Suppose we want to find the Second Largest Number in a List 
Ex: List=[4,6,3,2,2] Output=4

Solution:

arr=[2,5,4,2]
length=len(arr)
arr.sort()
print(arr[length-2])
3. Read an integer N, try to print out 123…N

EX: N=3, Output 123

Solution:

n= int(input())
print(*range(1,n+1), sep="")

n=8
1234567
Summary:

we have been working with the Python commands ***Range, Length, Sort, Module (%), and  If-Then(elif-then, else), df***dAs all the books suggested, the Best way to learn to use the knowledge mindfully, so I organized this note :)

Happy Practice! 女

Reference:
https://www.hackerrank.com/
https://www.geeksforgeeks.org/python-largest-smallest-second-largest-second-smallest-list/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号