Python basics: conditions and iterations

Next is about conditions and repetition

if statement

Meaning in Japanese It ’s a pretty important position in programming.

#format
if #First condition

elif #Second to n conditions

else #Other than the nth condition
#Example of use
#After the condition ":Don't forget to indent the output

n = 3
if n == 1: 
    print("No. 1 processing") 
elif n == 2:
    print("No. 2 processing") 
elif n == 3: 
    print("No. 3 processing") 
else: #When all the above conditional expressions are not satisfied
    print("Processing other than the above")

#output:No. 3 processing

Comparison operator

True if basic conditions are met If it does not come out, falsa is returned.

code Contents
a==b equal
a!=b Not equal
a>b a is greater than b
a>=b a is equal to or greater than b
a<b a is less than b
a<=b a is equal to or less than b

Boolean operator (and ・ or ・ not)

A and B #Conditional expressions for both A and B are satisfied
A or B #Either A or B conditional expression is satisfied
not conditional expression#Result reversal

Example of use


A = 1
B = 2

print(A==1 and B==2)
# true
 
print(not B < A)
# true

while statement

Processing that repeats while the condition is met

while


n = 5
while n >2: #If n is greater than 0, perform the following processing
    print(n)
    n -= 2 #n-1

#Output result 5 3

while + if statement

x = 5
while x != 0:
    x -= 1
    if x != 0:
        print(x)
    else:
        print("end")

for statement

I will post more about the list later We will go through the contents of multiple variables.

moji = ["A", "B", "C"]
for ji in moji: #Number of elements contained in animals = Repeat processing 3 times
    print(ji)
#Output result
A
B
C

break End of repetition

break


li = [1, 2, 3]
for n in li:
    print(n)
    if n >= 3: 
        print("Confirm 3 or more")
        break #end of for statement processing

continue Used when you want to skip processing

continue


li = [10, 20, 30, 40] #Number of elements contained in storages=Repeat processing 6 times
for n in li:
    if n < 30: #If n is less than 30, no processing is performed(skip)
        continue
    print(n)

#Output result
3
4

Repeated application

There is also a repeat method that mixes index and key and value.

Repeat with index

It is used when you want to display the index in the for statement.

index display


li = ["iti", "ni"]
for index, value in enumerate(li):
    print(index, value)
#Output result
0 a
1 b

Repeating multiple lists

It can be output by preparing the variable of the assignment destination.

Repeating multiple lists


li = [[1, 2, 3], [4, 5, 6]]
for a, b, c in li:
    print(a, b, c)
#Output result
1 2 3
4 5 6

However, if the original data does not contain the number statement data, an error will occur.

Multiple list repetition error


li = [[1, 2, 3], [4, 5]]
for a, b, c in li:
    print(a, b, c) #Get an error
#Output result
not enough values to unpack (expected 3, got 2)

Dictionary-type repetition

By using a function called items () You can use both keys and values.

Dictionary-style repetition


man = {"name": "yamada", "hair": "black", "arm": "left"}
for hidari, migi in man.items(): 
#The key is stored in hidari and the value is stored in migi.
        print(hidari+" is "+migi)

#Output result
name is yamada
hair is black
arm is left

Recommended Posts

Python basics: conditions and iterations
Python basics ⑤
Python basics
Python basics ④
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Statistical basics and Python, graphing, etc. (memo)
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Python slice basics
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Python basics ② for statement
Python iterators and generators
Python basic course (9 iterations)
Basics of Python scraping basics
Python basics 8 numpy test
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
python input and output
Python and Ruby split
Python basics: Socket, Dnspython
Perceptron basics and implementation
# 4 [python] Basics of functions
Python3, venv and Ansible
Basics of python: Output
Python asyncio and ContextVar
Programming with Python and Tkinter
Basic Python 3 grammar (some Python iterations)
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt