Python Programming Workshop-Super Introductory Vol.4

Before the main subject


Positioning / premise

This article was prepared for the ** In-house Workshop **. Therefore, please understand that we will proceed on the following assumptions.


Purpose of this workshop

The purpose is to allow participants to ** stand at the entrance where they can self-study as needed **.


How to proceed with the workshop

The workshop basically focuses on hands-on. (Some explanations are included)


Caution

This workshop is expected to be ** 60-120 minutes **. Therefore, we do not do ** systematic learning ** because we do not have enough time for full-scale learning.

In order to be able to create the program you want from scratch in earnest, you need to study by yourself or take some kind of course.


target

This workshop is mainly aimed at the following participants.

Please note that there are many things that I do not dare to mention because I do not deal with intermediate and advanced people at all.


Preparing the programming environment

Please install either of the following. If you have the time, please install Anaconda.

** Note: You don't need to install both **


What I learned up to the last time (review)

In the workshops so far, we have learned the following contents.

Programming concept

How to use Python module (part)

Other


To review these, it is good to review the workshop, try to solve the problem here, or copy the example.


Today's goal

To be able to understand the concepts of "functions", "classes", and "objects" that are indispensable in modern programming, and to be able to create simple functions and classes.

By doing this, we aim to be able to utilize various modules and SDKs.

If possible, make sure you understand what you wrote later. However, you don't have to remember it perfectly.


Hands-on start


Hands-on 1


Hands-on 1-1

Save the following script as exp411.py in the c: \ script folder and run it.

ex411.py


flag = True

# Part 1
if flag is True:
    print("Test Python is True.")
else:
    print("Test Python is False.")

# Part 2
if flag is True:
    print("Test Python is True.")
else:
    print("Test Python is False.")

# Part 3
if flag is True:
    print("Test Python is True.")
else:
    print("Test Python is False.")

Hands-on 1-2

Save exp411.py as exp412.py in the c: \ script folder, modify it, and then execute it.

ex412.py


flag = True

def printMsg():
    if flag is True:
        print("Test Python is True.")
    else:
        print("Test Python is False.")

printMsg()

Hands-on 1-3

Save exp412.py as exp413.py in the c: \ script folder, modify it, and then execute it.

ex413.py


F1 = True
F2 = False

def printMsg(flag):
    if flag is True:
        print("Test Python is True.")
    else:
        print("Test Python is False.")

printMsg(F1)
printMsg(F2)

Hands-on 1-4

Save exp413.py as exp414.py in the c: \ script folder, modify it, and then execute it.

ex414.py


F1 = True
F2 = False

def printMsg(flag, msg):
    if flag is True:
        print("Test Python is " + msg)
    else:
        print("Test Python is not" + msg)

printMsg(F1, "easy")
printMsg(F2, "difficult")

Commentary 1


Programming crap (review)


Changes from this time


Hands-on 1-2 commentary

def function name():
Processing content
Function name()

Hands-on 1-3 Commentary

def function name(argument):
Processing content
Function name(argument)

Hands-on 1-4 commentary


Hands-on 2


Hands-on 2-1

Save the following script as exp421.py in the c: \ script folder and run it.

ex421.py


ONES = "1"
ONEN = 1

if ONES == ONEN:
    print("Same")
else:
    print("Not Same")

Hands-on 2-2

Save exp421.py as exp422.py in the c: \ script folder, modify it, and then execute it.

ex422.py


SEVEN1 = 7
SEVEN2= "7"

print(SEVEN1 + int(SEVEN2))
print(str(SEVEN1) + int(SEVEN2))
print(SEVEN1 + SEVEN2)

An error will occur in the last sentence, but please proceed to the next hands-on.


Hands-on 2-3

Save exp422.py as exp423.py in the c: \ script folder, modify it, and then execute it.

ex423.py


ONES = "1"
ONEN = 7.0

if ONES:
    print("ONES is True.")
else:
    print("ONES is False.")

if ONEN:
    print("ONEN is True.")
else:
    print("ONEN is False.")

Commentary 2


Hands-on 2-1 Commentary

Variables have types

There are many other types, and different types are not considered to be the same data. See here for accurate information.

Embedded — Python 3.7.7 documentation


Hands-on 2-2 Commentary

bool(variable)
str(variable)
int(variable)
float(variable)

Hands-on 2-3 commentary

FLAG = "TEST"

if FLAG:
    print("True")
else:
    print("False")

Note: Depending on the programming language you use, the behavior may be slightly different.


Hands-on 3


Hands-on 3-1

Save the following script as exp431.py in the c: \ script folder and run it.

ex431.py


LIST = [10, 20, 'text']
print(LIST[0])
print(LIST[1])
print(LIST[2])

LIST[2] = 100

for i in LIST:
    print(i)

Hands-on 3-2

Save exp431.py as exp432.py in the c: \ script folder, modify it, and then execute it.

ex432.py


DICT = { 'one': 1, 'two': 2, 's1': 'Text1', 's2': 'Text2'}
print(DICT['s1'])
print(DICT['two'])

for i in ['one', 'two', 's1', 's2']:
    print(i)

print('---')
for i in DICT.keys():
    print(DICT[i])

Hands-on 3-3

Save exp432.py as exp433.py in the c: \ script folder, modify it, and then execute it.

ex433.py


TUPLE = (1, 2, 'TEXT0')
for i in TUPLE:
    print(i)

TUPLE[3] = 100

It doesn't matter if an error occurs, so proceed to the next step.


Commentary 3


Hands-on 3 commentary

Variables have types

List
Tuple
Dictionary (dict)

If you explain each as chitin, there are various things, so this time I will omit it just to know its existence.


Hands-on 4


Hands-on 4-1

Save the following script as exp441.py in the c: \ script folder and run it.

ex441.py


class TestClass:
    x = 1
    def show(self):
        print(self.x)

objTest = TestClass()
print(objTest.x)
objTest.show()

Hands-on 4-2

Save exp441.py as exp442.py in the c: \ script folder, modify it, and then execute it.

ex442.py


class TestClass:
    x = 1
    def get(self):
        print(self.x)
    def set(self, val):
       self.x = val

obj1 = TestClass()
obj1.get()

obj1.set(2)
obj1.get()

obj2 = TestClass()
obj2.get()

obj2.set("Obj2")
obj2.get()

obj1.get()

Hands-on 4-3

Save exp442.py as exp443.py in the c: \ script folder, modify it, and then execute it.

ex443.py


import requests

r = requests

# User-Agent
url = 'https://www.cloudgate.jp/ua.php'
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
header = { 'User-Agent': ua }

res = r.get(url, headers=header)
print("HTTP CODE: " + str(res.status_code))
print("HTTP BODY: " + res.text)

# GET PARAMETER
url = 'https://www.google.co.jp/search'
params = {'q': 'Qiita'}

res2 = r.get(url, params=params)

print(res2.url)
print(res2.text)

Commentary 4


Hands-on 4-1 and 4-2 Commentary

The image looks like this.

[Quoted from "Plato Edition-Idea Theory and Classes / Instances-ITmedia Enterprise"](https: / /www.itmedia.co.jp/im/articles/0506/11/news011.html)

Hands-on 4-3 Commentary

That said, if you have the documentation and usage examples, you may not be in trouble. However, understanding it will make it easier to deal with problems.


At the end

This should cover the basic terms.

However, we do not give details, so it is recommended that you read a technical book or practice with a collection of questions.

Also, if you create a script using API or SDK in this state, you will find many things that you do not understand. However, each time I read the documentation, I should be able to reach a level where I can understand it. By repeating them, you will be able to develop programs freely. let's do our best!

If you would like to continue learning, please make a request.


official


Python reference site


Python related books

Recommended Posts

Python Programming Workshop-Super Introductory Vol.3
Python Programming Workshop-Super Introductory Vol.4
Python Programming Workshop-Super Introductory Python Execution Environment
Python programming note
Programming in python
3. 3. AI programming with Python
Competitive programming diary python 20201213
Python programming with Atom
Competitive programming diary python 20201220
Python programming in Excel
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
Competitive programming diary python
Programming with Python Flask
Programming with Python and Tkinter
Python3 programming functions personal summary
Atcoder Acing Programming Contest Python
[Python] Dynamic programming knapsack problem
[Python] Dynamic programming TDPC D
Python web programming article summary
Paiza Python Primer 1 Learn Programming
Python Competitive Programming Site Summary
Python Machine Learning Programming> Keywords
An introduction to Python Programming
[Python] Dynamic programming TDPC A
Network programming with Python Scapy
[Swift / Ruby / Python / Java] Object-oriented programming
Python3 standard input for competitive programming
GUI programming in Python using Appjar
Functional programming in Python Project Euler 1
[Introduction to Python3 Day 1] Programming and Python
Competitive programming, coding test template: Python3
Functional programming in Python Project Euler 3
[Python] Object-oriented programming learned with Pokemon
"The easiest Python introductory class" modified
Functional programming in Python Project Euler 2
python documentation reading socket programming HOWTO
Introductory table of contents for python3