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.
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.
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.
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.
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 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