Python basics

module

In python, you can use the function programmatically by importing the module.

sample.py


import math
print(math.cos(0))

display

You can print the characters on the console by using the print function. Also, by using the end argument, you can freely set the place where line breaks occur by default.

sample.py


print("HelloWorld")
print("Hello",end=" ")
print("World")

OutPut

HelloWorld
Hello World

comment

You can write a comment in the code by using "#comment" or "" "" comment "" "".

sample.py


"""A program that displays cosine values"""
import math #Import math
print(math.cos(1))

variable

You can clean the code by deciding the variable name (box name to put the value).

sample.py


"""A program that displays cosine values"""
import math #Import math
x = math.cos(1)
print(x)

Data type

Programming languages can be treated differently by defining data types.

sample.py


"""A program that displays cosine values"""
import math #Import math
x = math.cos(1)
print(x)

"""Program that defines the type"""
print(type(x))
intx = int(x)#Decimal point truncation
strx = str(x)#Defined as a character
floatx = float(x)#Floating point type

print(intx)
print(strx)
print(floatx)

list

Create a list when storing and handling multiple data.

sample.py


"""A program that displays cosine values"""
import math #Import math
x = math.cos(1)
print(x)

"""Program that defines the type"""
print(type(x))
intx = int(x)#Decimal point truncation
strx = str(x)#Defined as a character
floatx = float(x)#Floating point type

print(intx)
print(strx)
print(floatx)

"""Programs to store in the list"""
cos_list = [intx,strx]
cos_list.append(floatx)

print(cos_list)

Getting a partial list using slices

A slice is easy to understand if you imagine the wall between the elements.

 0   1   2   3   4   5
 | A | B | C | D | E |
slice1 = cos_list[1:2]    
slice2 = cos_list[1:-1]   
slice3 = cos_list[1:]     
slice4 = cos_list[:2]     
slice5 = cos_list[:]      

Control syntax

Indent

Notice the for statement program. There is a space before the print statement. This space is the indent. Indentation is used in python to execute code within the process one line before the code.

In most cases, add a ":" at the end of the pre-indent code.

if statement

After describing "if", describe the conditional expression, and if the condition is True, the processing under the condition will be executed.

if_sample.py


value = 2

if value == 1:
    print ('The value of value is 1')
elif value == 2:
    print ('The value of value is 2')
elif value == 3:
    print ('value value is 3')
else:
    print ('There is no applicable value')

Use "and", "or", etc. for the combination of conditions.

if_sample2.py


value_1 = 'python'
value_2 = 'izm'

if value_1 == 'Python':
    print(" ")
elif value_1 == 'python' and value_2 == 'izm':
    print("The second conditional expression is True")
elif value_1 == "IZM" or value_2 == "PYTHON":
    print("The third conditional expression is True")

for statement

Used when implementing repetitive behavior.

sample.py


"""A program that displays multiple cosine values"""
for x in range(0,10):#0~Processing to put the value of 9 into x in the order()
    print(math.cos(x))#Cosine value that reflects the processing of the previous code

Untitled Diagram (2).jpg

function

You can define a function by using def. def Function name (): Defined by. The function name is arbitrary.

sample.py


"""A function program that displays multiple cosine values"""
def cos():#cos function
    for x in range(0,10):#0~Processing to put the value of 9 into x in the order
        print(math.cos(x))#Cosine value that reflects the processing of the previous code

Full code

sample.py


"""A program that displays cosine values"""
import math #Import math
x = math.cos(1)#Substitute cosine value for x
print(x)

"""Program that defines the type"""
print(type(x))
intx = int(x)#Decimal point truncation
strx = str(x)#Defined as a character
floatx = float(x)#Floating point type

print(intx)
print(strx)
print(floatx)

"""A function program that displays multiple cosine values"""
def cos():#cos function
    for x in range(0,10):#0~Processing to put the value of 9 into x in the order
        print(math.cos(x))#Cosine value that reflects the processing of the previous code
cos()

Thank you for your hard work.

Practice problem

Problem 1: Use two for and display the multiplication table. SampleOutput

1 2 3 4 5 6 7 8 9 
2 4 6 8 10 12 14 16 18 
3 6 9 12 15 18 21 24 27 
4 8 12 16 20 24 28 32 36 
5 10 15 20 25 30 35 40 45 
6 12 18 24 30 36 42 48 54 
7 14 21 28 35 42 49 56 63 
8 16 24 32 40 48 56 64 72 
9 18 27 36 45 54 63 72 81

Problem 2: Write a program that prints numbers from 1 to 100. However, if it is a multiple of 3, print "Fizz" instead of a number, if it is a multiple of 5, print "Buzz", and if it is a multiple of both 3 and 5, print "FizzBuzz". (FizzBuzz problem)

SampleOutput

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Problem 3: Complete the following program.

calendar.py


「X1」 calendar
calendar = calendar.Calendar()
「X2」 = []
「X3」 year 「X4」 range(int(2016),int(2017)+1): #Hint repeat sentence
「X3」 month 「x4」 range(1,13):
「X3」 week 「X4」 calendar.monthdayscalendar(year,month):
「X3」 day 「X4」 week:
「X5」 day != 0:  #Conditional branch
「X2」.「X6」("%d-%02d-%02d" %(year,month,day))
「X7」(date)

This program is a program that stores and displays date data from 2016 to 2017 in the list "date". A certain word is entered in "x1 ~ 7". Also, this code does not consider indentation. You have to indent yourself.

Sample program

1.Answer

9x9.py


for x in range(1,10):
        for y in range(1,10):
                print(x*y,end=" ")
        print("")

2.Answer

fizzbuzz.py


for i in range(1,101):
        if i%3 == 0 and i%5 == 0:
                print("FizzBuzz")
        elif i%3 == 0:
                print("Fizz")
        elif i%5 == 0:
                print("Buzz")
        else:
                print(i)

3.Answer

calendar.py


import calendar
calendar = calendar.Calendar()
date = []
for year in range(int(2016),int(2017)+1):
       for month in range(1,13):
               for week in calendar.monthdayscalendar(year,month):
                       for day in week:
                                if day != 0:
                                       date.append("%d-%02d-%02d" %(year,month,day))
print(date)

We would appreciate it if you could donate! Cryptocurrency BTC 18c58m54Lf5AKFqSqhEDU548heoTcUJZk ETH 0x291d860d920c68fb5387d8949a138ee95c8d3f03 ZEC t1KtTRKy9w1Sv5zqi3MYnxZ3zugo54G4gXn REP 0x291d860d920c68fb5387d8949a138ee95c8d3f03

Recommended Posts

Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of python ①
Python slice basics
#Python basics (scope)
#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 basics ② for statement
Python: Unsupervised Learning: Basics
Python
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
kafka python
Basics of Python x GIS (Part 3)
Python Summary
Built-in python
Paiza Python Primer 5: Basics of Dictionaries
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
SNS Python basics made with Flask
Python service
python tips
Linux basics
python function ①
Python memo
ufo-> python (3)
Python comprehension
install python
NumPy basics
Python Memorandum 2