Introduction to Python Hands On Part 1

Create introductory materials for company members. The environment was created with github + cloud9. Please make something good. There is a possibility that it will be an article soon. By the way, it is python2.7 for the time being.

Let's run it first.

For the time being, prepare a file with main.py, Described as follows. I will try it. The execution command is python main.py

main.py


#encoding=utf-8

if __name__ == '__main__':
    print "Hello"

Execution result


Hello

if __name__ == '__main__': This is the process of whether or not you are the boss of the program. For the time being, you should think that this is a magic.

print "Hello" It means to output.

Try to branch

Speaking of programming, there are branching and repetition, but let's branch.

main.py


#encoding=utf-8

if __name__ == '__main__':
    a = "Bye"
    if a == "Hello":
        print a
    elif a == "Bye":
        print a*2
    else 
        print "foo"

If a is "Hello" then "Hello" If a is "Bye", it means that "Bye" is output twice. That is, "ByeBye" is output. It feels bad, but programming is such a thing. Check the execution result by changing a to Hello or Bye.

repetition

Repeat this time.

main.py


#encoding=utf-8

if __name__ == '__main__':
    a = [1,2,3,4,5]
    for num in a:
        print num

Execution result


1
2
3
4
5

There are elements 1 to 5 in the array a, The elements are taken out one by one and output.

File split

As you write the python program, it will grow more and more. So, the part that can be shared is made into a function or a class. I think it's a good idea to split the files quickly.

For example image When the file is arranged like this

main.py


#encoding=utf-8

import sub1

if __name__ == '__main__':
    sub1.method()   

sub1.py


#encording=utf-8

def method():
    print "----"
    print "method"
    print "----"

if __name__=="__main__":
    method()

When I run it

Execution result


----
method
----

It looks like this.

Because it is one of them, around here.

Recommended Posts

Introduction to Python Hands On Part 1
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to PyQt4 Part 1
Introduction to Python with Atom (on the way)
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
Update python on Mac to 3.7-> 3.8
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Ansible Part ③'Inventory'
Introduction to Python For, While
Introduction to Ansible Part ④'Variable'
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Ansible Part 2'Basic Grammar'
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
WSL2 ~ Linux on Windows ~ (Part 1: Introduction)
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
An introduction to Python that even monkeys can understand (Part 3)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
An introduction to Python that even monkeys can understand (Part 1)
Steps to install python3 on mac
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
An introduction to Python that even monkeys can understand (Part 2)
Introduction to Python Django (2) Mac Edition
Introduction to Ansible Part 1'Hello World !!'
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Update Python on Mac from 2 to 3
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
Migrate Django applications running on Python 2.7 to Python 3.5
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
How to read pydoc on python interpreter