[GO] Introduction to Python For, While

For statement


for seireki in range(1989, 2017):
    print("Year" + str(seireki) + "Year", end="")
    heisei = seireki - 1988
    print("Heisei" + str(heisei) + "It's the year.")
<output>
The year 1989 is Heisei 1.
The year 1990 is 1990.
It is 1991, 1991.
It is 1992 and 1992 in the Christian era.
....

Add all the elements of the array and output the total value


nums = [1, 3, 5, 6, 3, 2, 5, 23, 2]
# print(nums)
sum = 0
for i in nums:
    # print(i)
    sum += (i)
    # print(sum)
print(sum)
>>50

While statement


import random
hp = 20
while hp > 0:
    hit = random.randint(1, 10)
    print("To slime" + str(hit) + "Damaged!")
    hp -= hit
    print("The remaining HP" + str(hp) + "is!")
print("Defeated slime")
<Output value>
Inflicted 1 damage on slime!
The remaining HP is 19!
Inflicted 9 damage on slime!
The remaining HP is 10!
Inflicted 5 damage on slime!
The remaining HP is 5!
Inflicted 3 damage on slime!
The remaining HP is 2!
Inflicted 2 damage on slime!
The remaining HP is 0!
Defeated slime

Recommended Posts

Introduction to Python For, While
An introduction to Python for non-engineers
Introduction to OpenCV (python)-(2)
An introduction to Python for machine learning
An introduction to Python for C programmers
[Introduction to Udemy Python3 + Application] 43. for else statement
Introduction to Programming (Python) TA Tendency for beginners
Understand Python for Pepper development. -Introduction to Python Box-
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
~ Tips for beginners to Python ③ ~
[Python] Iterative processing (for, while)
[Introduction to Python] How to use while statements (repetitive processing)
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
[Introduction to Python] How to write repetitive statements using for statements
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
An introduction to Mercurial for non-engineers
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Easy Python to learn while writing
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
Record of Python introduction for newcomers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Python] Introduction to graph creation using coronavirus data [For beginners]
2016-10-30 else for Python3> for:
Introduction to Scrapy (1)
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
python [for myself]
Introduction to Scrapy (3)
Updated to Python 2.7.9
[Introduction to Python] How to use class in Python?
Introduction to Supervisor
Introduction of Python
Introduction to Tkinter 1: Introduction
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement