First Python 3 ~ The beginning of repetition ~

Introduction

Please refer to here for notes.

Iterative processing

History repeats itself.
Let's repeat a simple program.

Loop processing by for in

Simple iterative process.

for counter variable in repeat range:
Iterative processing

Example

for i in range(3):
    print("hello world") 

============ ※ Execution result

hello world hello world hello world

============

Range function to repeat

The range function creates a range type object that has consecutive numerical values from the start number to the end number specified in the argument as elements.

range(5)
# 0 1 2 3 4

range(0, 5)
# 0 1 2 3 4

range(4,7)
# 4 5 6

range(0, 5, 1)
# 0 1 2 3 4

range(0, 10, 2)
# 0 2 4 6 8

range(10, 0, -1)
# 10 9 8 7 6 5 4 3 2 1

range(0, -8, -2)
# 0 -2 -4 -6
Try to specify with the range function
for i in range(6,11): # 6, 7, 8 ... 11
    print("Hello World:" + str(i)) #Display a numerical value behind
print("last " + str(i)) #For confirmation

============ ※ Execution result

Hello World:6 Hello World:7 Hello World:8 Hello World:9 Hello World:10 last 10

============

Repeat the numbers

Numerical values can also be repeated.

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

============ ※ Execution result

1
2
3
4
5
6
7
8
9

============

(Example) Display from January to December

for i in range(1,13):
    print(str(i) + "Month")

============ ※ Execution result

January
February
March
April
May
June
July
August
September
October
November
December

============

Iterative processing by while

A process that is repeated many times while the conditional expression is satisfied. You need to prepare the counter variables and conditional expressions yourself.

#while conditional expression:
    #Iterative processing
    #Update counter variable
    

Example

Add the numbers and repeat until the number is less than or equal to the specified number or more than the specified number.

i = 1   #Initialization of counter variables
while i <= 10: #Execute if i is 10 or less(1,2,3,4...10)
    print(i)   #Iterative processing
    i = i + 1  #Updated counter variables. i= i +Repeat from 1 to 10
    print("Next:" + str(i)) # Next: 11
print("Last:" + str(i))
Regarding the update expression (assignment operator) of the counter variable
operator Example meaning Another way of writing
+= i += 1 Increase the value of variable i by 1. a = a + 1
-= i -= 1 Increase the value of variable i by 1. a = a - 1

example

Display from 5 to 15
i = 5
while i <= 10:
    print(i)
    i += 1

============ ※ Execution result

5
6
7
8
9
10

============

Countdown from 5 to 1
i = 5
while i >= 1:
    print(i)
    i -= 1

============ ※ Execution result

5
4
3
2
1

============

Attack until the monster'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")

============

  • Execution result (example)

Inflicted 2 damage on slime! Inflicted 9 damage on slime! Inflicted 7 damage on slime! Inflicted 8 damage on slime! Inflicted 4 damage on slime! Defeated slime

============

Repeat until full recovery
import random
hp = 1
while hp <= 150:
    kusa = random.randint(10,30)
    print("Use herbs to increase HP" + str(kusa) + "I recovered." + "Current HP:" + str(hp))
    hp += kusa
print("Full recovery of physical strength")

============

  • Execution result (example)

Recovered 18 HP using herbs. Current HP: 1 Recovered 15 HP using herbs. Current HP: 19 Recovered 29 HP using herbs. Current HP: 34 Recovered 15 HP using herbs. Current HP: 63 Recovered 18 HP using herbs. Current HP: 78 Recovered 12 HP using herbs. Current HP: 96 Recovered 15 HP using herbs. Current HP: 108 Recovered 14 HP using herbs. Current HP: 123 Recovered 14 HP using herbs. Current HP: 137 Full recovery of physical strength

============

reference

Paiza Learning

Recommended Posts

First Python 3 ~ The beginning of repetition ~
Learning notes from the beginning of Python 1
Learning notes from the beginning of Python 2
The beginning of cif2cell
the zen of Python
Towards the retirement of Python2
About the ease of Python
About the features of Python
The Power of Pandas: Python
Learn Nim with Python (from the beginning of the year).
Study from the beginning of Python Hour1: Hello World
Study from the beginning of Python Hour8: Using packages
Why is the first argument of [Python] Class self?
The story of Python and the story of NaN
[Python] The stumbling block of import
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
Get the first element of queryset
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
See python for the first time
The first step in Python Matplotlib
About the basics list of Python basics
Learn the basics of Python ① Beginners
Note: Get the first and last items of Python OrderedDict non-destructively
[Python] How to get the first and last days of the month
First python ② Try to write code while examining the features of python
I want to output the beginning of the next month with Python
Change the length of Python csv strings
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Continuously play the first Python Sukusta MV
Pass the path of the imported python module
The story of making Python an exe
Check the existence of the file with python
About the virtual environment of python version 3.7
Omit BOM from the beginning of the string
Shout Hello, Reiwa! At the beginning of Reiwa
[Python] Understand the content of error messages
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
The result of installing python in Anaconda
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
The story of manipulating python global variables
MongoDB for the first time in Python
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
The Python project template I think of.
In search of the fastest FizzBuzz in Python
Python Basic Course (at the end of 15)
Set the process name of the Python program
[Python] Get the character code of the file
The story of blackjack A processing (python)
Intuitively learn the reshape of Python np
Python Note: The secret role of commas
Japanese translation: PEP 20 --The Zen of Python