Will be added at any time. This is the only basic review of Python ~ 2 ~
-// Integer part of the remainder of division --% Remainder of division
--print (): Output --input (): Input --int (): Integer --str (): Character string --floating (): Floating point --len (): Number of characters, number of elements
--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
--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
name = input()
if name == 'Alice':
print('Hi, Alice.')
elif age == 12:
print('Not Alice, young lady.')
else:
print('Who are you?')
--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.
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
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
-, 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 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
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
-This is the only basic review of Python ~ 2 ~
Recommended Posts