Python Tutorial:The Basics

This is a tutorial for people having some experience with at least 1 programming language.

Now, python can be thought of as pseudo-code.  Variables don’t have types. They appear when you assign them and dissapear when you don’t use them. You can do multiple assignments at one go.


Blocks are indicated ONLY through INDENTATION 

if x<5 or a<x<b

The index variable given in a for loop iterates through a list(explained later). To use it as you would to iterate through a set of values, use the range() function as follows:

for value in range(100)
  print value

To get input from the user using a text prompt, use the input function:

x=input("What is your age: ")
print "The square of the number is: ", x*x

Now, there are some things called LISTS and DICTIONARIES in python

They can be nested. A list can be nested within a list while a dictionary can be nested within a dictionary. Also, lists can be nested within dictionaries and dictionaries can be nested within lists. This can go on and on and so you can creaate complicated and advanced data structures.

This is an example of list.


You can also index and slice lists.

Indexing means that you can refer to it by its index, like, name[0]=cleese

Slicing is like indexing, except that you’re telling the boundaries from where you want the elements, for example:

<code>x = ["spam","spam","spam","spam","spam","eggs","and","spam"]
print x[5:7] # Prints the list ["eggs","and"]</code>

The end is non inclusive. If one of the indices is dropped, it means that you want everything in that direction.
For example, x[:3] will print everything till element 3.

Now, for dictionaries.

Dictionaries are similar to lists, except that their content is not ordered. Example:

<pre><code>{ "Ojas" : 23452532, "Chinmay" : 252336, "Bhushan" : 2352525, "Rohit" : 23624643}
person = { 'first name': "Rohan", 'last name': "Sonawane", 'occupation': "Scoundrel" }</code></pre>

Now, if you have to access the person’s last name, you can use:
person[‘last name’]


To define a function you use the def function like this:

<pre><code>def square(x):
</code>  return x*x
print square(2) # Prints out 4</pre>

When you pass a parameter to a function, you bind the parameter to the value, thus creating a new reference. If you change the “contents” of this parameter name (i.e. rebind it) that won’t affect the original.

<pre><code>def change(some_list):
</code>  some_list[1] = 4 x = [1,2,3]
print x # Prints out [1,4,3]</pre>

The value is changed, but this is not the case if we pass a variable whose value is a number.

That is all for now, the next tutorial will cover OOP concepts in python


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s