Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1

Overview

A memo of O'Reilly Japan's "Introduction to Mathematics Starting with Python" exercise.

It is just a personal study memo.

First answer

def print_num(number):
#     print([number + 2 for i in range(2, 11) if number % 2 == 0])
    print('Is an even number' if number % 2 == 0 else 'Odd')
    
    for item in range(2, 11):
        number += 2
        print(int(number), end=" ")

if __name__ == '__main__':
    number = float(input('what your number: '))
    if number.is_integer():
        print_num(number)
    else:
        print('Please enter an integer.')

Improvement

Improvement points It is good when an integer or decimal number is entered, but it ends abnormally when an alphabet or the like is entered. So I put try..except.


def print_num(number):
    print('Is an even number' if number % 2 == 0 else 'Odd')
    
    for item in range(2, 11):
        number += 2
        print(int(number), end=" ")

if __name__ == '__main__':
    try:
        number = float(input('what your number: '))
        if number.is_integer():
            print_num(number)
        else:
            print('Please enter an integer.')
    except ValueError:
        print('Please enter an integer.')

Summary

Improvements were made with reference to the answers. There are some differences from the answer, such as how to write an if statement, but this is OK. In the future, for example, I would like to verify what to do from the perspective of performance.

Recommended Posts

Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1
1.1 Getting Started with Python
Getting Started with Python
Getting Started with Python
Getting Started with Python Functions
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Python Web Applications
Getting Started with Python Basics of Python
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting started with Python with 100 knocks on language processing
Machine learning starting with Python Personal memorandum Part2
Machine learning starting with Python Personal memorandum Part1
[Translation] Getting Started with Rust for Python Programmers
Getting started with AWS IoT easily in Python
Materials to read when getting started with Python
Settings for getting started with MongoDB in python
Django 1.11 started with Python3.6
Getting started with Android!
3. 3. AI programming with Python
Getting Started with Golang 2
Getting Started with Golang 1
Getting Started with Django 1
Python starting with Windows 7
Python programming with Atom
Competitive programming with python
GRPC starting with Python
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Programming with Python Flask
Getting Started with Django 2
Getting Started with python3 # 2 Learn about types and variables
Introduction to Mathematics Starting with Python Study Memo Vol.1
Getting Started with Google App Engine for Python & PHP
Translate Getting Started With TensorFlow
Programming with Python and Tkinter
Getting Started with Tkinter 2: Buttons
Python3 programming functions personal summary
Getting Started with Go Assembly
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Getting Started with PKI with Golang ―― 4
Reinforcement learning starting with Python
Get started with Python! ~ ② Grammar ~
Getting Started with Django with PyCharm