[Python] Sample code for Python grammar

Introduction

I wrote the following program to study Python, I think there is a more efficient way to write, so I made this article so that I would like to hear the opinions of experts. If you are an expert who says that you can write in shorter lines, please comment.

Japanese calendar conversion

Japanese calendar conversion


#Years(Year)Enter with
year=int(input())

Syear=[1868, 1912, 1926, 1989, 2019, 9999]
wareki=['Meiji', 'Taisho', 'Showa', 'Heisei', 'Reiwa', '']

i=0

#Japanese calendar conversion
while Syear[i]<=year:
   Wyear=year-Syear[i]+1
   Wname=wareki[i]
   i+=1

#output
print(str(year)+'Year is'+Wname+str(Wyear)+'Year')

Age judgment

Age judgment


import datetime

#Birthday(Year)Enter
Tymd=input()

#Divided by date
param=Tymd.split('/')
Tyear=int(param[0])
Tmonth=int(param[1])
Tday=int(param[2])

#Get the current date
Today=datetime.datetime.now()
year=Today.year
month=Today.month
day=Today.day

#Age calculation
age=int(year)-int(Tyear)
if int(month) < int(Tmonth):
    age-=1
else:
    if month==Tmonth:
        if day < Tday:
            age-=1

#output
print(Tymd+'People'+str(age)+'age')

Day of the week judgment

Day of the week judgment


import math

#Input value(Example: 1997/9/11)To receive
x=input()

#Divide the input value into year, month and day
y=x.split('/')
year = int(y[0])
month=int(y[1])
day=int(y[2])

#output
print(str(year)+'Year'+str(month)+'Month'+str(day)+'day', end='')

if month < 3:
    month=month+12
    year=year-1

#Ask for the day of the week
weekday = (year + math.floor(year / 4) - math.floor(year / 100) + math.floor(year / 400) + math.floor((13*month+8) / 5) + day) % 7
week = ['Day', 'Month', 'fire', 'water', 'wood', 'Money','soil']

#output
print(str(week[weekday])+'It's the day of the week')

Multi-digit digit decomposition

Multi-digit digit decomposition


#Receive input value
x=int(input())

num=[]

#Decompose every digit
while x>0:
    num.append(x%10)
    x//=10
num.reverse()

#output
for i in range(len(num)):
 print(num[i], end=' ')

Leap year judgment

Leap year judgment


#Years(Year)Enter
year=int(input())

#Leap year judgment
if year%4==0:
    if year%100==0:
        if year%400==0:
            print(str(year)+'The year is a leap year')
    else:
        print(str(year)+'The year is a leap year')
else:
    print(str(year)+'The year is not a leap year')

Zodiac judgment

Zodiac judgment


#Years(Year)Enter
year=int(input())

#All zodiac signs
Alleto =["Monkey(Monkey)", "Rooster(Bird)", "Dog(Dog)", "亥(Wild boar)", "Child(mouse)"
            , "Cow(Ushi)", "Tiger(Tiger)", "Rabbit(Rabbits)", "Dragon(Tatsu)", "Snake(Snake)", "Horse(Horse)", "Not yet(sheep)"]

#Zodiac judgment&output
print(str(year)+'The zodiac of the year'+Alleto[year%12])

Primality test

Primality test


#Enter a number greater than or equal to 2
num=int(input())

# 0:Prime number
# 1:Not a prime number
sosu=0

#Primality test
for i in range(2, num):
    if num%i==0:
        sosu=1
        break

#output
if sosu==0:        
    print(str(num)+'Is a prime number')
elif sosu==1:
    print(str(num)+'Is not a prime number')

Recommended Posts

[Python] Sample code for Python grammar
Python code memo for yourself
Specific sample code for working with SQLite3 in Python
Python parallel / parallel processing sample code summary
I just wrote the original material for the python sample code
2016-10-30 else for Python3> for:
python [for myself]
Google Cloud Vision API sample for python
python character code
python> coding guide> PEP 0008 --Style Guide for Python Code
Python grammar notes
Sample for handling eml files in Python
[Python] Algorithm-aware code
Python3 basic grammar
A tool for easily entering Python code
R code compatible sheet for Python users
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Instant method grammar for Python and Ruby (studying)
Dart grammar for those who only know Python
Is there a good sample code for nosetests?
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Code for checking the operation of Python Matplotlib
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Settings for Python coding in Visual Studio Code
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
About Python for loops
Python basic grammar / algorithm
Python basics ② for statement
Python ~ Grammar speed learning ~
Python code acceleration approach
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
Before writing Python code
Ajax + Python + PostgreSQL sample
About Python, for ~ (range)
Techniques for code testing?
Python basic grammar (miscellaneous)
About Python3 character code
Refactoring tools for Python
python for android Toolchain
Python basic grammar note (4)
Python basic grammar memo
Python Requests status code
OpenCV basic code (python)
OpenCV for Python beginners
Install Python (for Windows)
[Python] for statement error
Python environment for projects
Python --Simple multi-thread sample
List of sample program distribution sites for python books
VS Code settings for developing in Python with completion
Tips for speeding up python code correctly with numba
Make Visual Studio Code autocomplete for python external libraries
[For beginners] Learn basic Python grammar for free in 5 hours!
Expose settings.json for efficient Python coding in VS Code
[Python] Code for measuring ambient light RGB of APDS9960
Python code for k-means method in super simple case