Post: [Turorial] The Basics of Python [Work in Progress]
10-26-2010, 10:50 PM #1
schnzrs
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); Python is a highly portable interpreted programming language. What does all of that mean? A Python program can run on a Mac, Windows, or Linux machine with pretty much no extra work. Python programs will run off a thing called an interpreter (which was written in the C language) that interprets the code and runs it.

You can download python at You must login or register to view this content. and click Python 2.7 installer.

Please check out the pages in order to understand the basics on Python and some other cool stuff.

I am still working on this tutorial.... much more to come in the following days!

[multipage=The Very Basics]
To get started in Python, you need a text editor. Notepad or Notepad++ is fine (or you could use Idle, but it sucks). Any time you make a python program, remember to save it as a .py file! But since we are just starting, please open up python.exe. But if you just want to start using .py files right away, you can. Just remember to end any .py programs with a
    pause=raw_input()
at the end to pause the program.



Python is awesome because it requires no extra work to get a program started. Here is your first python program (type this into python.exe)
    print "hello, world!"

The print statement prints characters onto the command prompt. The quotations are used to show we are printing a string, which I will get more into later. Basically, anytime you use text in a program, you need to put " " around it.

Python can easily assign variables. (I will use x for simplicity. Any thing that starts in a letter and is one word can be a variable in python).
    x="hello, world"
print x

x is a variable and now can be referenced anytime in your program.
It is also easy to re-assign variables.
    x="hey, I re-assigned this variable!"
print x

You can make a variable hold a number too.
    x=5
x=-5

This writes over the previous version of x and prints the newer version. You could also completely delete the x variable by saying:
    del x

Simple huh?

It is VERY easy to do basic math in python. Here are some examples of addition and subtraction in python.
    print 5+5
print 5-2
print -5+3

Multiplication and division is a little more difficult.
    print 10/2
print 10*2
/ is for divide, and * is for multiply. Python makes any basic number an integer (any small-ish number that has no decimal point.... I will go into this stuff more later.) This means that two integers multiplied or divided can not have a sum that has a decimal. Basically, 5/2=2 in python, and 10/3=3. To get an answer that is a decimal, do this.
    print 5/2.0
print 9/3.0

You should get 2.5 and 3.3 repeating.
For using powers of numbers, use **.
    print 5**2
print 2**3

5 to the 2nd power should equal 25, and 2 to the 3rd power should equal 8!

How about we use our learned information and combine them?
In python, you can make basic equations with variables. For example:
    x=2+4/2

You could get 4 as an answer. But, do you want to change the order of operations? Use parenthesis to change the order of operations in an equation.
    x=(2+4)/2

Now you should get 3 as an answer!

On to the next section...

[multipage=User Input and Basic String Manipulation]
User input is quite easy in python. To get input for a user and store it as a variable, do this.
    x=raw_input()
print x

What this program does is gets text input from the user, stores it as a variable named x, then prints what the user typed.
You can also make this a little more snazzy by adding a message such as:
    age=raw_input("Enter your age: ")
print age


But wait... this is not good enough. In python, you are able to add strings of text together. An example of this would be:
    x="Hello"
y="NGU"
print x+" "+y

or something with more variables like:
    x="Hello"
y="NGU"
z=x+" "+y
print z

This shows "Hello NGU". The reason for the bank " " is to add a space in between the two string variables.

So back to the point. This could make our basic age program a lot better. We could now say something like "Hey, you are 20 years old!" by adding this.
    age=raw_input("Enter your age: ")
print "You are " + age + " years old."

[multipage= Data Types]
As you may of previously read, I talked about strings and integers. If you have ever programmed before, this stuff should be very familiar. If you want to learn a lot about data structures in computer science, go here You must login or register to view this content.

Integer: an integer in simply a positive or negative number with no decimal.

Floating Point: a floating point is pretty much the same as an integer, except it can handle larger numbers.... and most importantly decimal points!

String: a string is just some text that is surrounded by two quotes.

Boolean: I wont talk about booleans much, since they are not that important in the basics of python. A boolean is pretty much a True or False statement.

Now converting a data type in another data type is python is easy. Say you make a variable x and it is an integer, so how would you convert it to a string? Well, the obvious syntax for data types is, int(), float(), str(), and bool(). The parenthesis will take the variable or raw data that will be converted. For example:
    Number="4"
Number=int(Number)
Number=float(Number)

In that code, I made a variable called "Number" that is a string, converted it into an integer, then converted it into a floating point.
(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked schnzrs for this useful post:

-Lazy-

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo