Monday, July 14, 2014

Python Lesson 1: Downloading Python and Hello World

Hello guys, and welcome to my python lesson series! In lesson 1, we will learn a little about the language, find and download a release and do a python "Hello World" example as our first program!

What is Python?

Python is a high level programming language, like Java, or C. Python allows you to easily reuse code you have written. For example, suppose you wrote a 10000 line program for a project. Then 6 months later, you need to use the code in a different project. Thanks to python's high level capabilities, adding all this code to your new project is as simple as 1 line of code in the new project. Python is the 4th most popular programming language, behind Java, C, and C++. You can use python for pretty much anything, from 2D and 3D games, to scientific studies, to number crunching (with numpy) and pretty much anything else you can think of. Python is also open source. This means that anyone can see it's source code. Java, on the other hand, is closed source, which means that you cannot see its source code.

Downloading Python

There are 2 versions of python that are commonly used today. At the time of this writing, the newest releases are 3.4.1 and 2.7.8 . You may be thinking: "Hey why do people use python 2 and not just use python 3?" This is because when python 3 was released, it did not offer backwards compatibility to python 2. Python 3 would not run python 2 code, causing a big issue. All previous work had to be ported to python 3. Although an automated python 2 to 3 converter was released, the tool is not perfect and usually a lot of code would have to be manually changed.

So which version should I download?

Although python 2 is still used widely today, in the future it will be replaced with python 3. Many new parts of the programming language are also not included in python 2. In these tutorials, we will be learning python 3, but if there is a difference in code between the 2, I will try to include it. We will be doing our python hello world example in python 3, along with all of our other future examples in this tutorial.

Enough talk! lets download it already!

download python
Go to https://www.python.org/downloads/, and choose the newest python 3 release for your operating system. At the time of writing, it is 3.4.1, but you should download the newest release.

Time for Your First Program: Python Hello World

It is a tradition that when you first learn a programming language for your first program to be a "Hello World" program. We will do this in this python lesson as well. Find a program called "IDLE" and open it. This stands for Integrated Development Environment.

python Hello World 1
IDLE in windows 8

IDLE, also known as python shell, should display a >>>. This is where we will do our python hello world example. Type in 

print('my python hello world!')

Make sure to type it in exactly, and do not use any uppercase letters. If you typed this in correctly, python should respond back by saying:

>>>print('my python hello world!')
my python hello world!

Congratulations! You just wrote your program.


So what happened in the python hello world example?

Lets dissect this simple line of code:

print('my python hello world!')

the print part is called a function. A function in programming can be thought of like a math function: you give it data, and it outputs data. In the case of print, you gave it 'my python hello world' as data, and it printed 'my python hello world' to the screen. Notice how this line of words is in quotes and shows up in green on python shell. This is called a string. You can think of strings in python as a line of text. Strings can use double or single quotes, but I prefer the single quotes because you do not have to hit shift on your keyboard to get them. More on strings in the future. So the print function will take a string, and print it to the screen, simple enough.

Expect the python lesson 2 in the next few days. In the meantime, do not forget to share, and follow me on google plus or on twitter for updates and new blog posts. Thanks!

No comments:

Post a Comment