This is the only basic review of Python ~ 1 ~

Will be added at any time. This is the only basic review of Python ~ 2 ~

0. Super basic matters

operator

-// Integer part of the remainder of division --% Remainder of division

1. Main functions used in Python

Basic matters

--print (): Output --input (): Input --int (): Integer --str (): Character string --floating (): Floating point --len (): Number of characters, number of elements

application

--Application of input () function

Useful when entering multiple values for a variable.

a, b, c = (int(x) for x in input().split())
#Enter as 12 14 15
print(a + b + c)
41

--Application of len () function

Used to count the number of values in the list.

spam = ['cat', 'bat', 'rat', 'elephant']
len(spam)
3

Other functions

--abs (): Absolute value --round (): Rounded * Strictly different

round(123.456, 1)
123.5
round(123.456, 2)
123.46
round(123.456, 0)
123.0
round(123.456, -1)
120.0
round(123.456, -2)
100.0

--__range (): Often used for for loops. __

for i in range(5):
    print(i)
0
1
2
3
4

__range () function start, end, step arguments __ __ Start and end __ The first argument represents the start value, and the __second argument represents a number one greater than the end. __

for i in range(12, 16):
    print(i)
12
13
14
15

The third argument represents the value of the variable that is incremented each time it is repeated.

for i in range(0, 10, 2):
    print(i)
0
2
4
6
8

2. Flow control

if statement, else statement, elif statement

name = input()
if name == 'Alice':
    print('Hi, Alice.')
elif age == 12:
    print('Not Alice, young lady.')
else:
    print('Who are you?')

while loop, for loop

--while loop

i=0
while i < 5:
    print(i)
    i = i + 1

--for loop

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

--In the while, for loop, __break statement __ and __continue statement __ can be used.

__break statement __ Break out of the loop. __continue statement __ Return to the beginning of the loop.

import module

sys.exit () Exit the program.

--random.randint The first and second arguments represent the range of values to be randomly output.

import random
for i in range(5):
    print(random.randint(1,10))

--import math Make it possible to use mathematical formulas.

math.floor Gaussian symbol (maximum integer value that does not exceed it)

import math
math.floor(5.95)
5
math.floor(-5.95)
-6

3. Function

Function definition

def statement function can be defined. You can specify the return value using the return statement.

def hello(name):
    print('Hello' + name)
hello('Alice')
Hello Alice

Keyword arguments

-, end ='' Eliminate line breaks.

print('Hello', end='')
print('World')
HelloWorld

-, sep ='' Insert the delimiter. (Initially a space)

print('cats', 'dogs', 'mice', sep=',')
cats,dogs,mice
#Usually it looks like this
print('cats', 'dogs', 'mice')
cats dogs mice

Local scope, global scope

--Local variables cannot be used from the global scope. --Global variables can be used from the local scope. --You cannot use variables in other local scopes in the local scope.

__grobal statement __ Make local variables available in global scope.

def spam():
    global eggs
    eggs = 'spam'
eggs = 'global'
spam()
print(eggs)
spam

Exception handling

For example, when the following program is executed, the following error occurs. You can avoid certain errors by using try and except clauses.

def spam(divide_by):
    return 42 / divide_by
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
#When I run this I get the following error
21.0
3.5
Traceback (most recent call last):
  File "C:\Users\t\Desktop\programming\Python\error.py", line 5, in <module>
    print(spam(0))
  File "C:\Users\t\Desktop\programming\Python\error.py", line 2, in spam
    return 42 / divide_by
ZeroDivisionError: division by zero

To avoid ZeroDivisionError, insert a try clause and an except clause as shown below.

def spam(divide_by):
    try:
        return 42 / divide_by
    except ZeroDivisionError:
        print('Illegal argument.')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
#When executed, it will be as follows
21.0
3.5
Illegal argument.
None
42.0

error

--ValueError When it is a non-integer --ZeroDivisionError Division by zero

Python basic review list

-This is the only basic review of Python ~ 2 ~

Recommended Posts

This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
Review of the basics of Python (FizzBuzz)
[python] [meta] Is the type of python a type?
Python Basic Course (at the end of 15)
The answer of "1/2" is different between python2 and 3
the zen of Python
Basic knowledge of Python
Summary of the basic flow of machine learning with Python
Impressions of taking the Python 3 Engineer Certification Basic Exam
Why is the first argument of [Python] Class self?
[Introduction to Python] Basic usage of the library matplotlib
Towards the retirement of Python2
Basic usage of Python f-string
About the features of Python
The Power of Pandas: Python
Python Basic Course (1 What is Python)
I wrote the basic grammar of Python with Jupyter Lab
What is the default TLS version of the python requests module?
Initial setting of Mac ~ Python (pyenv) installation is the fastest
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Is the probability of precipitation correct?
[Python] What is @? (About the decorator)
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
What kind of Kernel is this Kernel?
[python] What is the sorted key?
Change the Python version of Homebrew
About the basic type of Go
This and that of python properties
[Python] Understanding the potential_field_planning of Python Robotics
Basic grammar of Python3 system (dictionary)
What is the python underscore (_) for?
Science "Is Saito the representative of Saito?"
About the basics list of Python basics
Basic study of OpenCV with Python
Learn the basics of Python ① Beginners
[Python] Organize the basic structure of Flask apps (Aim for de-copying)
March 14th is Pi Day. The story of calculating pi with python
[Python] Display only the elements of the list side by side [Vertical, horizontal]
How to use the asterisk (*) in Python. Maybe this is all? ..
Try running the basic information Python sample problem only in the browser
A memorandum regarding the acquisition of the Python3 engineer certification basic exam
Change the length of Python csv strings
What is the XX file at the root of a popular Python project?
This and that of the inclusion notation.
[Linux] Review of frequently used basic commands 2
(Bad) practice of using this in Python
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
What kind of programming language is Python?
Review the concept and terminology of regression
What is the cause of the following error?
[Python] Understand the content of error messages
Where is the python instantiation process written?
[Python3] Rewrite the code object of the function