Paiza Python Primer 3: Learn Loop Processing

Python3 is completely free on Paiza, so I summarized it.

Introduction to Paiza Python3

01: Let's display the numerical value repeatedly

lesson.py


'''
for counter variable in repeat range
Iterative processing
'''    
#Counter variable: A variable for work that stores data to be repeated
#Be sure to indent iterative processing


for i in range(5):
    print("hello world" + str(i))
    
for i in range(6,11):#6,7,8,...End at 11
    print("hello world" + str(i))
    
#→ 6 to 10 are displayed

print("last"+str(i))

#is displayed as last10

Exercise

  1. Display "Hello, paiza learning" 5 times

lesson.py


for i in range(5):
    print("Hello, paiza learning")
  1. Display numbers 0 to 15

lesson.py


for i in range(16):
    print(i)
  1. Display from January to December
for i in range(1,13):
    print(str(i) + "Moon" )

02: Let's repeat according to the conditions 1

lesson.py



i=1#Initialize counter variable
while i<=10: #1,2,3...11 Repeated until the while conditional expression is satisfied
    print(i)  #Iterative processing
    i = i + 1 #Update counter variable
    print("next" + str(i))
print("last"+str(i))

Exercise

  1. Display "Hello, paiza learning" 5 times

lesson.py


i=1#Initialize counter variable
while i<=4:
    print("Hello, paiza learning")  #Iterative processing
    i = i + 1 #Update counter variable
  1. Display numbers from 0 to 5

lesson.py


i=0#Initialize counter variable
while i<=5: #1,2,3...Repeated until the while conditional expression is satisfied
    print(i)  #Iterative processing
    i = i + 1 #Update counter variable

03: Let's repeat according to the conditions 2

lesson.py


#Counter variable "i= i+Without "1", it will be an "infinite loop" and time out.

i = 5#Initialize counter variable
while i <= 15:
    print(i)#Iterative processing
    #Update counter variable
print("last:" + str(i))

lesson.py


#Change how to write counter variables "i"+=1」

i = 5#Initialize counter variable
while i <= 15:
    print(i)#Iterative processing
    i += 1#Update counter variable
print("last:" + str(i))

lesson.py



#Change how to write counter variables "i"-=1」
#Operator to reduce inequality sign "while i">=Change to 1 "
#From 10 to 1

i = 10#Initialize counter variable
while i >= 1:
    print(i)#Iterative processing
    i -= 1#Update counter variable
print("last:" + str(i))

lesson.py


#Attack slime
#Damage is random from 1 to 10
#Repeat until the slam's HP reaches 0

import random
hp=30
while hp > 0:
    hit = random.randint(1,10)
    print("To slime" + str(hit) + "Damaged!")
    hp -=hit
print("Defeated slime")

Exercise

An output program that displays numerical values line by line from 1.5 to 15.

lesson.py


i = 5#Initialize counter variable
while i <= 15:
    print(i)#Iterative processing
    i += 1#Update counter variable
  1. Count down the number from 5 to 1
i = 5 #Initialize counter variable
while i >= 1:
    print(i)#Iterative processing
    i -= 1#Update counter variable

Display even numbers from 3.1 to 10

i = 2 #Initialize counter variable
while i <= 10:
    print(i)#Iterative processing
    i += 2#Update counter variable

04: Let's create HTML repeatedly

lesson.py




#Automatic generation of HTML tags
#I have to repeat!
print("<select name=\'age\'>")
print("<option>1 year old</option>")
print("<option>2 years old</option>")
print("<option>3 years old</option>")
print("</select>")

#Use the for statement in such a case

print("<select name=\'age\'>")
for age in range(10):
    print("<option>"+ str(age+1) + "Talent</option>")
print("</select>")

#range with range function(10)Will be output from 1 to 9

Exercises

lesson.py



print("<ul>")
print("<li>1</li>")
print("<li>2</li>")
print("<li>3</li>")
print("</ul>")

#Iterative processing of for statement

print("<ul>")
for age in range(100):
    print("<li>"+ str(age+1) + "</li>")
print("</ul>")

05: Data reading (standard input)

・ Program flow

Enter data
↓
Processed programmatically
↓
Output the result



・ Types of data entry

----------------------
Web service/API

Database

File

Specified when executing the keyboard
----------------------

↓ Input

Program execution


・ What is standard input?
Originally how Unix works

----------------------
File

keyboard

Specified at runtime

----------------------
↓
Standard input
↓
Program execution


What is standard input </ strong> Originally, it is a mechanism prepared by Unix-like OS such as LINUX. If you create a program that supports standard input, you can switch the input destination, such as reading a file, reading data from the keyboard, or specifying parameters when the program is executed.

input () function </ strong> Read one line from standard input

lesson.py


input
paiza

line=input()

print("hello" + line)

output
hellopaiza

lesson.py



input
123

#Add int for numbers

line=int(input())

print(line*10)

output
1230

Exercise

  1. Get text from standard input

lesson.py


input
Hello Paisa

string=input()

print(string)

Output Hello Paiser
  1. Get a number from standard input and calculate

lesson.py



input
1234

line=int(input())

print(line*100)

output
123400

06: Let's read multiple data

lesson.py


input
paiza
python


line = input()
print("hello " + line)
line = input()
print("hello " + line)

output
hello paiza
hello python

#I want to input multiple lines as standard

input
3
paiza
python
world

count=int(input())
print("Number of data" + str(count))
for i in range(count):
    line = input ().rstrip()
#Delete line endings of data with rstrip
    print("hello " + line)

output
Number of data 3
hello paiza
hello python
hello world

rstrip () function </ strong> Remove line breaks at the end of lines of data. If line breaks remain, it may adversely affect the subsequent processing, so it is deleted here. You can remove the line break in the return value of the input by writing it after the input and following the dot.

lesson.py



input
4
Brave
Warrior
merchant
Wizard

count=int(input())
print("Number of data" + str(count))
for i in range(count):
    line = input ().rstrip()
    print(line + "Attacked the slime")

output
Number of data 4
The brave attacked the slime
The warrior attacked the slime
The merchant attacked the slime
The wizard attacked the slime

Exercise

  1. Output the same text a specified number of times

lesson.py


input
8

count=int(input())
for i in range(count):
    print("There was a slime")

output
There was a slime
There was a slime
There was a slime
There was a slime
There was a slime
There was a slime
There was a slime
There was a slime
  1. Combination of standard input and for statement

lesson.py


input
5
12

#Standard input and loop processing
num1 = int(input())
num2 = int(input())

for i in range(num1, num2 + 1):
    print(i)

5
6
7
8
9
10
11
12

07: Let's make a correspondence table between the year and the year of Heisei

lesson.py


for seireki in range(1989,2017):
    print("Year"+ str(seireki) +"Year", end=" ")
    heisei = seireki -1988
    print("Heisei"+ str(heisei)+"It's the year.")

output
1926 AD is 1926
1927 AD is 1927
:
:
1988 AD is 1988

Exercise

  1. Let's make a correspondence table between the year of the Christian era and the year of Showa

lesson.py


for seireki in range(1926,1989):
    print("Year"+ str(seireki) +"Year is",end="")
    heisei = seireki -1925
    print("Showa"+ str(heisei)+"Year")

  1. Let's make a correspondence table between the year of the Christian era and the year of Showa for a specific period

lesson.py


input
1975
10

start = int(input())
term = int(input())

for seireki in range(start, start + term):
    print("Year" + str(seireki) + "Year is", end = "")
    shouwa = seireki - 1925
    print("Showa" + str(shouwa) + "Year")

output

1926 AD is 1926
1927 AD is 1927
:
:
1987 is 1987

Recommended Posts