Python Basic Course (3 Python Execution)

Program execution

Let's actually write the program. The meaning of each code will be explained in the following chapters, so First, let's execute the program that we actually wrote.

Create a file with the name ** odd_or_even.py ** and write the following program. Note the indentation that is characteristic of Python. Indentation can be tabs or half-width spaces, but be careful of the number when using spaces.

odd_or_even.py


def odd_or_even():
    for i in range(1,6):
        if i % 2 != 0:
            print("{0} is odd".format(i))
        else:
            print("{0} is even".format(i))
        
    
if __name__ == "__main__":
    odd_or_even()

If Python is installed correctly, running the program will produce results similar to the following:

1 is odd 2 is even 3 is odd 4 is even 5 is odd

This program is a program that determines whether the values from 1 to 5 are odd or even. Translated into Japanese, the above program performs the following processing.

From the next page, we'll start with a description of Python notation. But before that, keep in mind the following for future learning:

print The statement to output characters in a program is the print statement. Run the following program to understand how to use the print statement.

print_explain.py


print("display statement")
print("display statement {0}".format("string"))
print("display {0} {1} {2}".format(1,"a","abc"))

comment

A comment is a comment inserted in human language (Japanese or English) in the source code. It is ignored when the computer does the work, so you are free to insert statements. When someone else reads a program you write, the code alone may not tell you what the program is meant to be. If you add a comment at that time, it will help the other person to understand your intention. Also, if there is a program that you do not want to run temporarily, you may comment it. If you want to put a line in a comment state, write "#" at the beginning. The following program outputs "taro" and "saburo", but does not output "jiro".

comment_explain.py


print("taro")
#print("jiro")
print("saburo")

If you want to comment out multiple lines at once, enclose the statement in three double quotes (or single quotes).

multi_comment_explain.py


print("1")
#print("2")
print("3")
print("4")
'''
print("5")
print("6")
'''
print("7")
print("8")
"""
print("9")
print("10")
"""

Make good use of your comments.

Japanese output

Please execute the following program. I think you will get an error.

display_japanese_ERROR.py


print("Hello World")

If you want to output Japanese in Python, add the following code to the first line.

display_japanese.py


# -*- coding: utf-8 -*-
print("Hello World")

I think it will be output correctly. If you want to input / output Japanese with Python, go to Character code and utf-8. You need to specify that you are using /UTF-8.html).

Next: Python Basic Course (4 Numerical Type / String Type)

Recommended Posts

Python Basic Course (3 Python Execution)
Python basic course (12 functions)
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Python basic course (9 iterations)
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python Basic Course (Introduction)
Python basic course (13 classes)
Python basic course (8 branches)
Python basic course (10 inclusion notation)
Python Basic Course (5 List Tuples)
Python Basic Course (1 What is Python)
Python Basic Course (14 Modules and Packages)
RF Python Basic_01
Python3 basic grammar
RF Python Basic_02
Python Basic Course (at the end of 15)
Python basic course (4 numeric type / character string type)
Python I'm also basic
Basic sorting in Python
Function execution time (Python)
[python] class basic methods
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python basic memorandum part 2
Output python execution time
python basic on windows ②
Python basic memo --Part 2
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic if statement
Python Basic --Pandas, Numpy-
Basic Python 3 grammar (some Python iterations)
Python application: Pandas Part 1: Basic
Python execution time measurement memo
Refactoring Learned in Python (Basic)
[Python] Using OpenCV with Python (Basic)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Execution time measurement with Python With
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
[Python] [SQLite3] Operate SQLite with Python (Basic)
External command execution in Python
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
python parallel / asynchronous execution memorandum
Java and Python basic grammar comparison
Python
Scraping with Selenium in Python (Basic)
Python course for data science_useful techniques
[Python] Basic knowledge used in AtCoder