Introduction: Get Started With Python

About: I love inventing and playing around with things. My projects reflect this - usually in-depth reports of problems I have solved as an inventor. I hope that my projects will inspire others to use just a littl-bi…

Programming is awesome!

It's creative, its fun and and it gives your brain a mental workout. Many of us want to learn about about programming but convince ourselves that we can't. Maybe it has too much math, maybe the jargon thats thrown around scares you. Let me tell you now that that is exactly what ever programmer thought before they became programmers. In fact that's exactly what I though less than 10 weeks ago when I started programming.

Let me tell you right now that anyone can learn to write programs. With advances in easy to read programming languages like python, and the wealth of information on the internet, Its no longer a full time investment to learn the ins and outs of a programming language. Infant most '21st century' programmers are extremely lazy, just learn the basics and build on from there.

This is a No-Jargon, easy to follow ible that you can do right now. Thats right! Minimise facebook and youtube, relax and follow along on your computer.

Old Person Computer

Today we will be looking at a programming language called Python, wikipedia defines python as:

"Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasises code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale."

So what does IT ALL MEAN? I thought you said no-jargon? Well basically:

"Python is a compact, general purpose, easy to read programming language. It is very versatile, so can be used to create programs of all shapes sizes."

Why did I pick python? Because the basics of python are easy to pickup and the language has an excellent support community online. After finishing this ible you can start making programs immediately, instead of spending the your time learning the required intricacies of the language.

So lets start!

Step 1: What You Will Need

Firstly you will need a computer with a copy of python, sorry mobile users!

Head over to https://www.python.org/downloads/ and download the latest release available with the big yellow button.

Python Download

Then follow the Installer Instructions to install python. Python Installer

To verify python is installed, go to CMD (or terminal) and type:

python --version

Python should respond with the version of python.

Now for this tutorial we will be using an IDE, or Integrated Development Environment (basically a text editor and compiler stuffed together) so head over to https://www.jetbrains.com/pycharm/download/ and download the "Community Edition" of Pycharm.

PyCharm Community

Then follow the Installer Instructions to install PyCharm.

Now, to start programming!

Step 2: Setup PyCharm and Create a New Project

The first time you launch pycharm it will ask you what keymap and theme you want to use. I would reccomend leaving the key map at default, but you can play around with the theme, colours and fonts to your liking. For this tutorial I am using pycharm community 4.5 with the Dracula theme.

You will then be greeted with a welcome screen.

Press Create New Project

PyCharm Welcome Screen

Select Pure Python then choose a folder to store the files in, then press create ( Note that the folder name will be the name of your project )

PyCharm Project Screen

At this point you should be greeted with the actual Code Creating Screen :P

Code Screen

Step 3: Create Your First Program

Right Click on your project folder and go to new -> Python File

Create New MENU

Name the file and press ok

File Dialogue

Now a new tab will appear in your main area

Below __author__ copy and paste this code.

message = "Hello World"
print message

Then right click on the file and press Run

This will compile our program and return a result. Hello World will be printed in the Run Area

Lets look at what we just wrote.

Step 4: Figuring Out Your First Program

Now lets try to understand what the code

message = "Hello World"
print message

really means.

Firstly I create and set the value of the variable to a string containing Hello World, If you change the text between the speech marks then you can change the value of the variable and therefore the message. For Instance:

message = "Hello Instructables!"
print message

Returns:

when run.

A string is defined because of the speech marks, strings can also be defined with single marks

message = 'Hello World'
print message

Variables can also have different types. For Instance this:

integer = 29302

is a variable with an integer value ( abbreviated int ) and this:

floatingPoint = 1469.928

is a variable with a floating point value (abbreviated to float).

Basically, the difference between integers and floats is integers are whole numbers while floats are decimal numbers. Integers take up less room but cannot hold decimals. For instance interger 1 / integer 2

integer1 = 1
integer2 = 2
print integer1 / integer2

is 0.5 right? But the result is:

Because integers cannot be divided into decimals. However this:

float1 = 1.0
integer2 = 2
print float1 / integer2

returns 0.5 when run because one of the variables is a float

'Print' simply prints a value. For Instance

print "DESTROY THE WORLD"

prints the string

Print can also print the value of an equation, including combining two strings

string1 = "HELLO "
string2 = "IBLE LOVERS"
print string1 + string2

prints

Step 5: Loops and If's - Control Structures

One of the most essential things about any program is the existence of control structures.

The first control structure is a while loop, this piece of code loops while a condition is true. For Instance this code

count = 0
while count < 10:
    # Add to count
    count = count + 1

    print count 

print "Finished"

runs the code in the loop until count < 10 and then continues on with the program.

The Second is an if-else statement, this piece of code checks and does something if the value of a variable is equal to 10, does something else if the value is equal to 11 and does something else in all other circumstances.

integer = 0

# If its equal to 10
if integer == 10:
    print "ITS 10"

# If its equal to 11
elif integer == 11:
    print "ITS 11"

# In all other Circumstances
else:
    print "I DONT KNOWWWWWWWW"

print "Finished"

Will return

Because the variable integer is not equal to 10 or 11 and in every other situation it runs else.

To define a while or if control structure put the type (while or if) followed by the true or false value then :

type true == true:

Notice the 'whitespace' for the contents of each loop, python is very specific about whitespace, that's how it knows what code is in a while or if loop. PyCharm uses one tab whitespace which must be consistent across all of your code! You can also adjust the settings to use spaces instead (which has some advantages).

Step 6: Comments

You may have noticed that I have 'commented' on the code I posted on the control structures part. You can comment a piece of code by placing a # and the rest of the line will be commented. When the software is compiled the comments are ignored

# Hello World

Comments are the saviour of code. Because they allow you to show everyone whats in your code and what you were thinking when writing the code. But don't overdo it! Over commented code can be just as bad as under-commented code

So when should I comment my code?

My rule of thumb is to comment your thoughts as your writing, so if you added a variable just to hold your age that serves no other purpose. Comment that.

# Variable that holds my age in years
# Not actually used in this program but # essential for the survival of the human race! myAge = 23</p>

Step 7: Your Finished - Time to Write Something Yourself!

Woah, you just learnt the basics of python!

So "what do I do now?" you might ask, well you can continue to have a play around with your file. When you think you are ready you can take on a challenge. Write a program to add or subtract two user inputted numbers. Use the internet to find out how to get user input then add or subtract the two inputs and be sure to comment your code! While Python is easy to learn it by no means has a low possibility ceiling - programs of all shapes, sizes and configurations are possible using Python and it's comprehensive (and usually community submitted) libraries. Post your code below for suggestions and improvements. Good Luck!