Knowledge Base‎ > ‎Programming‎ > ‎Python‎ > ‎

Python Quick Start

(With Java to Python Notes)

A fast overview of Python that can get you on track. Read, and try the code in the Python interpreter. With translations to Java to help you get you a good starting point if you have been doing some Java.


A Note About Indenting

Understanding indenting is crucial to not getting frustrated with Python. You must indent properly for you code to work properly - in other languages you are suppose to indent but it is not required. On class projects using other languages you will often lose marks when you didn't indent properly. But in Python not indenting correctly creates errors in your code - it is not optional. 


Indenting makes your code is much more readable (and it is also more readable because there are no { } (curly braces) or ; (semicolons).


Using an IDE will make your job of indenting much easier. Try highlighting a section of code and hit tab or shift-tab. The whole blocks moves back and forth.


  

Basic Syntax

Python aims to be readable even for a new programmer. Try reading the Python code in the following examples and predict what the code is doing.


One of the first things that you will notice about Python is that there are no semicolons at the end of lines.  


Also Python uses indenting to indicate blocks of code.

 

Python

age = 16
print "hello world!"
Java
int age = 16;
System.out.println("hello world!");

Variables

Variables are ways to store data such as numbers and text. A variable is a name which you give a value with the equals sign.

Variables don't need a type when they are created. See types to understand more about this. Variables are not restricted to one type and can be changed as needed.

 
Python
age = 16
name = "Patrick"
Java
int age = 16;
String name = "Patrick";

Interpreter

Python can be run one line at a time through the Python interpreter. This lets you try out small pieces of code and see what happens.

The example to the right is a little more advanced and shows how you can use Python to fetch weather data from Environment Canada.


 
Python

>>> url = "http://text.www.weatheroffice.gc.ca" >>> page = "/rss/city/on-143_e.xml" >>> import urllib >>> data = urllib.urlopen(url + page) >>> data.readlines()[23] # grab the 23rd line of data ' <title>Current Conditions: Sunny, 15.7&#xB0;C</title>\n'

 Java

 Can not run code this way unless using a specialized tool such as BeanShell.

 

If

If statements in Python allows you to have your code do different things depending on variables or other things. If the condition is true then the code indented under the if statement is executed. If it is false then it is not executed. Pay close attention to indenting your code properly.
 
The example shown prints out whether or not you can drive based on the value of the variable age.

The else keyword is used to specify alternative code to run if the condition is false. There is also a combined keyword called elif if you want to combine the else with an if statement. Like else, elif is only tried if the previous if is false.

Python and Java syntax are similar with the main difference being that Java uses { } (curly braces) to show which code executes where Python just uses indenting. 

 
 
Python
if age > 16:
print "You can drive!" elif age == 16: print "You just made it!" else: print "Nope, get a skateboard!"
Java
if (age >= 16 )
{
System.out.println("You can drive!");
} else if (age == 16) { System.out.println("You just make it!"); } else { System.out.println("Nope, get a skateboard!"); }

While

A while loops allows you to repeat a section of code over and over until a condition is met. A while statement contains a condition. The code is run so long as the condition is true. The code that belongs to the loops is indicated by being indented under the while statement.

You can end a while loop by using the break command. 

(Code from Wikipediaarticle.)
 
Python
counter = 5
factorial = 1

while counter > 0:
factorial *= counter
counter -= 1

print (factorial)
Using the break command
counter = 0
factorial = 1

while True:
    counter = counter + 1
factorial *= counter
if counter == 5:         break print factorial


Java

int counter = 5;
long factorial = 1;

while (counter > 1)
{
factorial *= counter--;
}
System.out.println(factorial);

Types

Types in Python are dynamic and can change. Python selects the type for you when you assign a variable.

You can find out what type a variable is by using type().

You can covert between types using simple functions such as int() or str().


Python
>>> age = 16
>>> type(age)
<type 'int'>
>>> age = raw_input("what's your age? ")
what's your age? 16
>>> type(age)
<type 'str'>
>>> age
'16'
>>> age = int(age)
>>> age
16
>>> type(age)
<type 'int'>
>>>


Code Blocks

Code blocks are how Python knows what to repeat in a loop or selecting code in if statements. 


In Python code blocks are indicated by your indenting. In other languages you are expected to indent properly as it makes your code much easier to read - but in Python you are actually showing which code belongs to if statements and loops.


If you don't indent properly in Python then watch out! You will either get you an error or (worse) have a weird bug in in your code (those weird bugs are a problem Python programmers eliminate with Unit Testing). 
 
Python
if age >= 16:
print "You can drive!"
else:
print "Nope, get a skateboard!"
Java
if (age >= 16 )
{
System.out.println("You can drive!");
} else {
System.out.println("Nope, get a skateboard!");
}



Subpages (1): Reading Text Files
Comments