Python - From Hello World to Saying Hello to the World - Part 1

in #python7 years ago (edited)

Introduction

Have you ever thought about learning to program? Maybe you've thought about how it would be nice to know how. Maybe you've thought it would be nice to make websites that are more than just a simple page with a few graphics, but actually have functionality, like a custom blog, a forum, or even a social network! What about making a game or an application that you think would be useful? It's a lot easier than you might think. It just takes the right teacher, or the dedication to look through the resources, practice programming, and persevere through frustration and the many bugs you will encounter. A lot of programming is actually just research, and either implementing what someone tells you, or copying techniques and bringing them together to make them your own!

I'm not gonna claim to be the right teacher for you. Many people learn in their own ways. What I am going to do is try to do my best to introduce you to a programming language, Python, and hopefully make it easy to follow, as well as informative. I have hopes that after reading this series of posts, and following along, you'll be able to write useful programs of your own, perhaps with a little extra research.

So, why Python? Well, Python is a very easy language to learn. It's also very powerful. It is one of the most common programming languages used today. All this means that there are a lot of modules that have been built to make programming in Python even easier. It's also pretty fast, as well as being quicker to build programs in. This means that you can learn Python, be programming faster, and get programs done that are very useful, quickly.

If you already know how to program, then this post, and the series, might go a little slow for you. I hope you still enjoy it however. If you already know Python, you might want to skip this one, and stick around for future posts to learn about Flask and other programming tasks. If you do decide to read through though, I'd love your input, and your upvote if you think I did a good job.

What this series aims to do:

  1. Provide a basic introduction to the Python programming language.
  2. Introduce you to how to make web apps and website back-ends using Python and Flask.
  3. Hopefully use code that will run in either Python 2.7 or 3.x.

What this series will not do:

  1. Teach you anywhere near everything you need to know to program. You will likely need to do extensive research after you read this. Programming takes a lot of research. Almost every program you write requires you to research new modules and techniques, or look up things that you've forgotten, or aren't sure about.
  2. Teach you how to install Python. If you have a *nix based OS like Linux or Mac OS X, then you probably already have Python installed. If you don't, I'm sorry. You should probably install Linux. :P Or just google how to install Python. It's a little bit of a pain on Windows, but you can do it.
  3. Teach you how to make Android or iOS apps. While you can technically use Python to make Android apps, I would highly suggest that you use languages with greater adoption on those platforms.
  4. Teach you to be a 1337 haxor. Programming is not hacking. If you are interested in cyber security though, programming is a useful tool to have in your belt. Python in particular is used often in InfoSec, and can be highly useful. I do not plan on covering this in this particular series, but I may cover topics useful to sys admins and infosec in the future.

The Print Function: Hello World

Almost every tutorial on almost every programming language starts with an introduction using the now famous "Hello World!" This is meant to show you the bare basics of the language. It also introduces you to the "standard output". The standard output is usually set to print to the terminal. In web development, it's also often used to print to the browser.

#!/usr/bin/env python

print("Hello World!")

If you don't have any prior experience with programming languages, this might look a little foreign to you. If you have programmed before, even in another language, this probably looks all too familiar.

Lets break it down.

#!/usr/bin/env python

This is what is known as a shebang, or hash-bang, among other names. This tells *nix based operating systems to send the code to an interpreter.

If you have multiple versions of Python installed, your interpreter may default to Python 2.7, and you may have to specify Python 3 for some programs.

#!/usr/bin/env python3

Specifying Python 3 isn't required for the programs covered in this post though, and I'll try my best to ensure every program in this series runs on both Python 2.7 and Python 3.x. Specifically I'm testing them with Python 2.7.13 and Python 3.5.3, and maybe Python 3.6.1 if I feel like it, for the more advanced stuff later on.

print("Hello World!")

This is the part that does the actual dirty work. This passes the string "Hello World!" to the print function. The print function then sends it to standard out, which is usually the terminal, along with a newline character. The newline character is what is put in text documents when you press the return key.

Functions are sections of code, that are usually named, so you can call them from elsewhere in your code. Functions are often use for commonly used tasks, or occasionally for complex code that you want to keep separate from your main code. Any time you do something more than once, it's good to put it into a function.

You can read more about the built in function in Python at docs.python.org.

Note: You don't need to use the parenthesis in version earlier than Python 3, because it used to be a statement rather than a function. It's good practice to do so now that it is though, so it works in both Python 2.7 and 3. Adding parenthesis to print commands is likely one of the first things you'll do when modifying code to be able to run on Python 3.

Running Your Code

The Python Interpreter

You can run the program in a few different ways. The first way I will show you is the Python interpreter. The Python interpreter is extremely useful. I only wish I had been introduced to it when I first started using Python. I likely would have learned a lot faster, and gotten more used to it.

Simply type in "python" in the terminal to start the python interpreter.

$ python
Python 3.5.3
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

It should look something like the above.

In the Python interpreter you don't have to use the shebang. You simply type the code directly in, and it instantly runs it.

>>> print("Hello World!")
Hello World!

When you're done, you can use the quit of exit functions to exit the interpreter.

>>> quit()
OR
>>> exit()

The Command Line

Running Python programs directly from the command line can be useful occasionally. It may come particularly in handy for sys admins and users that use the Linux terminal a lot.

$ python -c 'print ("Hello World!")'
Hello World!

Note: This probably doesn't work on Windows.

Calling the Python Interpreter with the File

Likely the most common way to run Python scripts is to save them to a file, and then run that file. One of the ways to do that is to feed it into the Python interpreter.

$ python helloworld.py
Hello World!

This tells Python to execute the script "helloworld.py". You can then see the output directly after.

Executing the Script Directly

There's one last thing you need to do though to be able to just double click to run your script, or run it by typing it's name into the terminal. You need to give it executable permissions! On a *nix based system, such as Linux or Mac OS X you can issue the following command.

chmod +x helloworld.py

This just tells the operating system that you're giving it permission to execute the file. This is a security measure so you don't execute something you don't want to.

If you're putting the file on a webserver, the much more common method is to do the following.

chmod 755 helloworld.py

This gives your account read, write, and execute permissions (the 7), the group your account belongs to read and execute permissions (the second 5), and the entire server read and execute permissions (the last 5).

You can read the manual for chmod on *nix based systems by issuing the following.

man chmod

Or just read it on linux.die.net.

In most operating systems you can also right click it, go to get info, and add executable permissions in one of the tabs of the window that pops up.


Image copyright Michael McConville
Used under Creative Commons Attribution 4.0 International
(source)

I'm gonna stop here for now. I had planned on covering a lot more in my initial post on programming, but I feel as though certain subjects, such as variables and loops, deserve their own posts. I have the majority of the Variables post already written though, so expect that out soon.
Hope you enjoyed this rather simple introduction. We're only just getting started.

Sort:  

Despite the fact that I am more of a functional programming guy; I still recommend that new developers learn Python as their first programming language. It lets them contribute almost any field they want to get involved in and is very easy to understand as well. Of course, after they learn python, I quickly point them in the direction of Haskell and Clojure.

Cool! I'd like to follow this purely so I can read some of the gibberish I always come across these days. I know it's all python gibberish. Following

Python is a must learn programming language. Once you master it, you can shine in the field of Data Science, Machine Learning and even more. All the best!!!

This is great! I will try some of these things later since all my Python work has been on Windows or on the Raspbian operating system. Since the Raspbian work was rather straightforward, it will be more of an exercise in Python-on-other-Linux.

Anyway, from my learning process so far in life, I have observed that I usually lose interest if I try to solve disconnected problems gradually increasing in difficulty.

Do you think it will be more engaging for you and for the readers if you start off with a complex problem and break down the process along with the programming language as you work on it? As an example, let us say you start off with the objective of "simulating an ant colony in Python" (I have a thing for ants, long story), or "designing a website to do X in Python."

Of course you don't have to be so specific, but having a goal in mind seems to help with motivation. When you accumulate an audience with your posts on Python and Flask, you could consider getting feedback on interesting projects that the readers would like to learn (maybe you could even have a poll?) and then break the project down into interesting little bits.

Those are the thoughts that come to me at the moment, and please feel free to ignore them. I will look forward to your future posts.

I can definitely see what you're saying with having some complex goal in mind. The goal of this series is to just introduce someone to programming Flask, from nothing. After I introduce people to Flask, I've got a few tasks planned with Flask itself.
Once I get the bare bones basic of Python down though, I was thinking about a few side tutorials. I hadn't really thought about anything in particular though, other than maybe thinking a few arcade games.

Python is very cool, i really enjoy using it. I switched from perl as my favorite script language some years ago. Once you get used to it you don't want to use anything else anymore. The only thing that really hit me was fixing 2000 lines of code after I deleted all the tabs and line breaks by accident. Stupid windows line feeds.

As someone who has written my fair share of detailed tutorial posts, I understand the time and effort this required - thank you! Wonderful post, and as I am about to begin diving into Python this is perfect timing. I would much rather begin with a Steemit post than whatever I turn up with a Google search. I think you made the right call to cut it off there rather than make one huge post with the next subjects - you already covered a lot of ground, and covered it thoroughly. I appreciate that you are taking the time to explain each step of the way and providing several different ways to run the code.

Very much looking forward to the continuation of this series. Followed :) Cheers - Carl

Thanks!
Yeah, it was a lot of work just editing and reordering sections.
I should have the next post up by tomorrow. I planned on it being part of this one, but I realized it would be better to make it it's own post so I can cover a lot more, and just reference back to it occasionally.

Yes, that is a big part of what impressed me was the obvious time spent editing this. Really well written and organized, with concise explanations. Job well done :) Cheers - Carl

I want to learn python as well but hardly speak English. Good content though mate, keep it up.

Thanks!
There are sources to learn programming in most major languages, and it's not super hard. I'm sure if you try, you can learn. :)

Congratulations @geekpowered, this post is the second most rewarded post (based on pending payouts) in the last 12 hours written by a Dust account holder (accounts that hold between 0 and 0.01 Mega Vests). The total number of posts by Dust account holders during this period was 2917 and the total pending payments to posts in this category was $458.56. To see the full list of highest paid posts across all accounts categories, click here.

If you do not wish to receive these messages in future, please reply stop to this comment.

Congratulations, your post received one of the top 10 most powerful upvotes in the last 12 hours. You received an upvote from @hendrikdegrote valued at 61.19 SBD, based on the pending payout at the time the data was extracted.

If you do not wish to receive these messages in future, reply with the word "stop".

I picked up some materials to teach myself python some months ago. But here I am; I don't even know where the materials are. This post has really piqued my interest. I think I should go look for my tuts