python textbook for beginners

What is python

Unlike compiler-type languages (C language, etc.), python is an interpreted-type language that executes at the same time as machine language translation without the need to create an executable file. This article is for those who have basic knowledge of C language. Created for python 2 series. The code has been tested with paiza.io.

Basic matters

The extension of the source file is ".py" Write the comment after # Since line breaks are performed automatically, there is no need to write \ n as in ** C language. ** ** The basic source code description is described below.

python.py



#Specify the character code as follows(UTF-When specifying 8)
# coding:UTF-8

#When defining a variable(No need to specify type)
a = 5
b = 'str'

#The four arithmetic operations and operators are basically the same as in C language.
c = a + 5 

#The print statement is, "When'Either can be used
print'Hello world!!'

Basic grammar

3 major control structures

The three major control syntaxes are ** sequential **, which is executed in order from the top, ** selection **, which is executed by selecting a case according to a condition, and ** iteration **, which is repeated only when a certain condition is satisfied. Here, if statement, for statement, and while statement are described.

if statement (if-else statement)

Execute only when the conditional expression is true. Use else if you want to specify the processing for false cases. It does not require {} as in C language, and is described using indentation.

if.py


# coding:UTF-8

'''syntax
if logical expression:
Execution formula when true
else
Execution expression when false
'''

#Example
x = 10
y = 5

if x>y:
  print 'x is greater than y'
else:
  print 'y is greater than x'

'''Execution result example

x is greater than y
'''

for statement (for-range statement)

The for statement in python can specify the number of repetitions by using the range function. You can also specify a negative number for the ** argument. ** **

for.py


# coding:UTF-8

'''syntax
for var in range():
Execution formula
'''

#Example:-Display numbers from 5 to 5
for i in range(-5, 6):
  print i

'''Execution result
-5
-4
-3
-2
-1
0
1
2
3
4
5
'''

while statement

Repeat the execution expression as long as the conditional expression is true.

while.py


# coding:UTF-8
'''syntax
while conditional expression:
Execution formula
'''

#Example
a= 0
while(a<5):
  print a
  a++

print a

'''Execution result example
0
1
2
3
4
5

'''

function

Use the def statement when defining a function. Specify the return value with return, but if this is omitted, None will be returned. Also, python has methods. This refers to what is defined in the class, and what is defined in the module is a function.

func.py


#coding UTF-8
'''syntax
def function name(Formal argument):
Function execution process description
'''
#Example:Define a function that returns a multiplier
a = 3
def func(a):
    return a**2

print func(a)

'''Execution result example
9
'''

String

Character strings are enclosed in''. Python can handle byte strings and Unicode strings. Unicode is used when dealing with Japanese. Also, if you add a comma at the end of the print statement, line breaks will not occur.

japanese.py


#coding UTF-8

#Character string to display Japanese
print('Japanese')

#Strings can also be read from elements like a list
j = 'japanese'
print j[2]

#The concatenation of letters+Do using
f = 'food'
print(j + f)

#Search for strings
#('The character string you want to search'in Search string)
#The execution result returns True or false
print('japan' in j)


'''Execution result example
Japanese
p
japanesefood
True

'''

list

It is a data structure that corresponds to an array in C language. You don't have to specify the size of the list, and you can arrange different objects such as integers and strings. ** **

list.py


#coding UTF-8

# coding: utf-8
# Here your code !
#Create list(You can save time and effort by using the range function for serial numbers.)
a = range(5)
b = [6, 7, 8]
print a

#Elements start from 0 as in C
print a[4]

#Add element to the end of the list(Can be different objects)
a.append(5.00)
print a

#Substitute the element at the specified position
a[0] = 'zero'
print a

#Add element at specified position
#When adding the nth m list.insert(n, m)
a.insert(0, -1)
print a

#Delete the specified element from the list
del a[0]
print a

#Join list
a.extend(b)
print a

'''Execution result example
[0, 1, 2, 3, 4]
4
[0, 1, 2, 3, 4, 5.0]
['zero', 1, 2, 3, 4, 5.0]
[-1, 'zero', 1, 2, 3, 4, 5.0]
['zero', 1, 2, 3, 4, 5.0]
['zero', 1, 2, 3, 4, 5.0, 6, 7, 8]
'''

dictionary

A dictionary is a list of key-paired value pairs. The value can be of any type, but the key must be an immutable object.

dic.py


#coding utf-8

'''syntax
dic = {Key:value, Key:value、Key:value...}
'''

#Example
age = {'tanaka':20, 'yamada':16, 'eto':33}
print dic[eto]

#Dictionaries can also be combined
age2 = {'wada':45, 'seki':12}
age.update(age2)
print age

#Delete dictionary
del age['wada']
print age

Reference page

Web page of Mr. Masahiro Mizutani, Department of Economics, Faculty of Economics, Daito Bunka University Introduction to python Hopushi Special Feature Learn PC Technology Python

Recommended Posts

python textbook for beginners
OpenCV for Python beginners
A textbook for beginners made by Python beginners
Learning flow for Python beginners
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
<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
2016-10-30 else for Python3> for:
python [for myself]
Roadmap for beginners
Beginners practice Python
Python beginner's note
INSERT into MySQL with Python [For beginners]
[Python] Minutes of study meeting for beginners (7/15)
Let's put together Python for super beginners
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Atcoder standard input set for beginners (python)
[For beginners] Try web scraping with Python
Memo # 4 for Python beginners to read "Detailed Python Grammar"
About Python for loops
Python Beginner's Guide (Functions)
The fastest way for beginners to master Python
Spacemacs settings (for beginners)
Python basics ② for statement
Causal reasoning and causal search with Python (for beginners)
Python beginners organize heapsort
For new students (Recommended efforts for Python beginners Part 1)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Python beginners organize quicksort
About Python, for ~ (range)
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Python beginners touch Pytorch (3)
~ Tips for Python beginners from Pythonista with love ① ~
Refactoring tools for Python
Try to calculate RPN in Python (for beginners)
Easy understanding of Python for & arrays (for super beginners)
Python Dictionary Beginner's Guide
python for android Toolchain
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Python beginners touch Pytorch (1)
Python beginners touch Pytorch (2)
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Dijkstra algorithm for beginners
Introduction to Programming (Python) TA Tendency for beginners
Python Beginner's Guide (Introduction)
Installing TensorFlow on Windows Easy for Python beginners