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.
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!!'
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.
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
'''
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
'''
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
'''
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
'''
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
'''
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]
'''
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
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