A textbook for beginners made by Python beginners

3 major control syntax

if, elif, else Control syntax for branching. By setting a conditional expression, it is possible to use the processing to be performed properly. The if statement in Python can be interpreted in almost the same way as in C language. When writing a control statement, be careful of ** the number of blanks at the beginning of the statement **, not just the if statement!

if age >= 20:

print "adult" elif age >= 12: print "Youth" else: print "boy"

for, in Syntax for iterative processing. The target to be repeated is different from C language, and the process is repeated for each element of list, tuple, dictionary key, each character of character string, each line of file, etc. If you want to specify the number of times you want to repeat, use the range () function.

for a in [1, 2, 3]
   print a

Execution result 1 2 3

for b in (1, 2, 3)
   print b

Execution result 1 2 3

for c in {'one' : 1, 'two' : 2, 'three' : 3}
   print c

Execution result three two one

for d in range(3)
   print d

Execution result 1 2 3 When targeting a dictionary with a for statement, note that the order in which keys are retrieved depends on the dictionary implementation method, so it is not retrieved in order! while A syntax for conditional iterative processing. It's almost the same as C language, and there is no difference that needs special mention.

eto = [u'child', u'丑', u'tiger', u'卯', u'dragon', u'巳', u'noon', u'not', u'monkey', u' Rooster', u'戌', u'亥',] n = 0 while n < 3: print eto[n] n += 1

Execution result Child Ox Tiger

Character string (Japanese)

When using Japanese in Python, u or U must be added before "..." or'...'. By doing this, the character string can be used as Japanese (Unicode).

a = len ('aiueo') # It is recognized as 15 characters in the byte string because it is not recognized as Japanese. a = len (u'aiueo') #Recognized as 5 Japanese characters

Lists, tuples, dictionaries

list

A collection of elements surrounded by [...]. Write as [element 1, element 2, element 3, ...] It can be used like an array in C language, but it is also possible to put different elements in the same list, and by being able to specify the range from what number to what number, it is strengthened compared to the array in C language. Are

a = [1, 2, 3]
b = [1, 'a', 'A']
a = [10, 20, 30, 40, 50]

x1 = a [2] # a third [30] x2 = a [1: 3] # a 2nd to 4th [10, 20, 30] x3 = a [0: 4: 2] Skip the 1st to 5th #a [10, 30, 50] x4 = a [-2] # 2 from the back [40]

y = [1, 2, 3] + [4, 5, 6]   

Lists can be concatenated using the # operator #[1, 2, 3, 4, 5, 6]

Tuple

A collection of elements surrounded by (...). Write as (element 1, element 2, element 3, ...) Same usage as list, but cannot change elements Also, if the tuple has one element, you must add a, (comma) after the element.

a = (10, 20, 30, 40, 50)

a [1] = 60 # Cannot be changed and results in an error

a = ('a') # be the letter'a' instead of a tuple a = ('a',) # Tuple ('a',)

You can also change a list to a tuple and a tuple to a list.

list ((1, 2, 3)) # Becomes [1, 2, 3] in the list tuple ([1, 2, 3]) # Tuple (1, 2, 3)

dictionary

A collection of elements surrounded by {...}. Write as {key 1: value 1, key 2: value 2, key 3: value 3, ...} Elements are made up of key / value pairs, and there is no order between the elements, so you need to specify the key to call the element.

a = {"sato" : 10, "tanaka" : 20, "suzuki" : 30}

x = a ["tanaka"] # Substitute the element value 20 of "tanaka" into x

In addition to numerical values, character strings and tuples can be used as keys.

a = {"pen" : "apple", "pine apple" : "pen"}
a = {(1, 2) : "x", (3, 4) : "y"}

Recommended Posts

A textbook for beginners made by Python beginners
python textbook for beginners
I made a python dictionary file for Neocomplete
Procedure for creating a LineBot made with Python
A memo for creating a python environment by a beginner
Learning flow for Python beginners
I made a VM that runs OpenCV for Python
Python3 environment construction (for beginners)
Python #function 2 for super beginners
[Python] I made a classifier for irises [Machine learning]
Basic Python grammar for beginners
I made a python text
Made a command for FizzBuzz
100 Pandas knocks for Python beginners
Python #list for super beginners
Building a Python environment for programming beginners (Mac OS)
~ Tips for beginners to Python ③ ~
kabu Station® API-I made a Python wrapper for REST API
[For beginners] Basics of Python explained by Java Gold Part 2
[VSCode] I made a user snippet for Python print f-string
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[For beginners] Basics of Python explained by Java Gold Part 1
I made a Line-bot using Python!
Python Exercise for Beginners # 2 [for Statement / While Statement]
Step by Step for creating a Dockerfile
Python for super beginners Python # dictionary type 1 for super beginners
Machine learning summary by Python beginners
I made a fortune with Python.
Python #index for super beginners, slices
Typing automation notes by Python beginners
<For beginners> python library <For machine learning>
Python #len function for super beginners
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
I made a daemon with Python
Python> I made a test code for my own external file
I made a lot of files for RDP connection with Python
[For beginners] How to register a library created in Python in PyPI
I made a scaffolding tool for the Python web framework Bottle
I made a Python wrapper library for docomo image recognition API.
An app made by a newcomer who loves music games by learning python for only one day
I made a dash docset for Holoviews
INSERT into MySQL with Python [For beginners]
I made a payroll program in Python!
Programming environment for beginners made on Windows
I made a character counter with Python
Let's create a virtual environment for Python
Let's put together Python for super beginners
[Mac] Building a virtual environment for Python
Make Qt for Python app a desktop app
3 Config editing mistakes made by Linux beginners
A tool for easily entering Python code
[Python] Read images with OpenCV (for beginners)
I made a Hex map with Python
WebApi creation with Python (CRUD creation) For beginners
After studying Python3, I made a Slackbot
Django tutorial summary for beginners by beginners ⑤ (test)
Atcoder standard input set for beginners (python)