I can handle Japanese, but write in UTF-8. I get an error such as SJIS.
Create the script you have done so far as a text file (test_01.py)
print(1)
print('2')
a = 1 + 2
print(a)
print('Hello Python-san')
Try to run
C:\test_01.py
1
2
3
Hello Python-san
C:\
* Try to raise an error using an undeclared variable (test_03.py)
```python
print(1)
print('2')
a = 1 + 2
print(b)
Try to run
C:\Users\>test_03.py
1
2
Traceback (most recent call last):
File "C:\Users\test_03.py", line 4, in <module>
print(b)
NameError: name 'b' is not defined
It will be executed halfway, but an error will occur.
b is not defined, so it's easy to understand with the error as it is.
The if statement looks like this. It's easy, but I didn't remember it because I've only copied it before, but once I write it, I'll never forget it.
if conditional expression: Processing when the conditional expression is satisfied elif conditional expression: Processing when the second conditional expression is satisfied else: Other processing
* Example sentence
```python
a=17
if a % 2 == 0:
print('2')
elif a % 3 == 0:
print('3')
elif a % 5 == 0:
print('5')
else:
print('nothing')
After this, loop processing will come out, but since loop processing and list are closely related, I will explain here
The list is like this. List with 3 elements
[ 'taro', 'jiro', 'kotaro']
How to use the list
Assign a list to a variable / Display a list / Extract one data of a list
Swap list values (overwrite) / Get list length (len function) / Get out of list (error)
Example sentence
a = ['taro', 'jiro', 'kotaro', 10]
print(a)
print(a[0])
a[0]='hogeo'
print(a)
print(len(a))
print(a[4])
Try to run
C:>test05_list.py
['taro', 'jiro', 'kotaro', 10]← Display list
taro ← Always the first
['hogeo', 'jiro', 'kotaro', 10]← Replace and display
4 ← Get list length
Traceback (most recent call last):
File "C:\test05_list.py", line 11, in
## Loop processing
* For loop. It's faster to see the sample
```python
name_list = ['taro', 'jiro', 'kotaro', 'hogeo']
for name in name_list:
print(name)
Try to run
>test06_for_loop.py
taro
jiro
kotaro
hogeo
If you think that you want to take the length of the list with len (), don't take it.
Put the 0th to the end of the list in the name variable and then output
break & continue
When you want to exit in the middle of processing
Break example. A program that determines if the list contains even numbers
nubber_list = [1, 3, 11, 15, 7, 9]
has_even = False
for i in number_list:
print(i)
if i % 2 == 0
has_even ~ True
break
print ('has_even=' has_even)
Try to run
>test07_break.py
1
3
11
15
7
10
has_even=True
Use when you don't know how many times to loop
A program that determines if the list contains even numbers
number_list = [1, 3, 11, 15, 7, 9]
length =len(number_list)
i = 0
while i < length:
print('List No.' + str(i) + '=' + str(number_list[i]))
i += 1
Try to run
>test08_while.py
List No.0=1
List No.1=3
List No.2=11
List No.3=15
List No.4=7
List No.5=9
Recommended Posts