Page 1 of 1

An Introduction to Python

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 15 December 2008 - 08:01 AM

*
POPULAR

Python is one of the most easy to learn but powerful programming language. This tutorial will show you the basic of python. Here you can see how easy it is to learn python.

A formal introduction to python

Now it's time to write our first python program, a typical hello world! program. You have two choices to write your python program: 1. Using interpreter & 2. Using source file. Here is how to write hello world! program in python-

#First python program
print "Hello World!"



The code starts with # sign indicates that it is a comment line. The output of the program is-

Hello World!


Input from user

In python, raw_input is used to take input from user end. Look at the bellow example-

print "Hi ! What's your name?"
name=raw_input("My name is :")
print "Nice to meet you %s"%s




Indentation

Indentation is important in python. The statements which go together must have same indentation. Each such set of statements is called a block. Take a look at the following example-

Example-1:
#Same indentation & will run successfully
i=6
print " The value of i is",i



Example-2:
#This will show an error message as indentation level is not same
i=6
  print "The value of i is",i




Control Flow Statement

In python, three control flow statements are used: if, for & while.

The if statement
The if statement is used to check a condition and if the condition is true, a block of statements are executed (called the if-block), else another block of statements (called the else-block) are executed. The else clause is optional. Here is am example of if statement-

number=14
guess=int(raw_input("Enter an integer:"))
if guess==number:
	print "Yes! You have guessed the right number."
elif guess<number:
	print "Your guess is little lower than the number!"
else:
   print "Your guess is little higher than the number!"




The for loop
When we need to do a work repeatedly for a number of times, we can use for loop. Bellow is an example of for loop-

for i in range(1,10):
	print i



The output of the above code will be-
1
2
3
4
5
6
7
8
9


The while statement
The while statement allows you to repeatedly execute a block of statements as long as a condition is true. It is also another looping statement. A while statement can have an optional else clause. Take a look at the example given below-

stop=False
password="atik"
while not stop:
	guess=raw_input("Enter password:")
	
	if guess==password:
		print "Hello atik! Nice to see you"
		stop=True
	else:
		print "Hello guest! Try once again"




The break Statement
The break statement is used to break out of a loop statement even if the loop condition has not become False. Here is an example-

stop=False
while not stop:
	something=raw_input("Enter something:")
	if something=="quit":
		break





Comments in Python
Comment s are used as the documentation of your work. They explains your code, which is helpful when someone else want to work with your code. Or they can be useful to you also at a later date to understand easily what you have done. Comments are not executed, so they have no effect when a program runs. In Python use can comment using # sign. Anything right to # sign will be treated as comment. So take a look at the example below how to make a comment-

Example:Comments in python
#This will print Hello World
print "Hello World!"



Constants
Constants are used to store some piece of data. But their values are fixed , which means their values are never changed. A constant can be either numeric constant or string constant. Numeric constants are just numbers like 4, 2.6 etc. String constant is a sequence of characters like "This is a string constant" or "Hello World!" and so on.

Variables
Variables are used to store some information and manipulate it. It’s value can vary. Variables are just parts of your computers memory where you can store some information. To access a variable you need to give them name. There are some rules for naming variable-

1.First character must be a letter or underscore(_).
2.Other characters should be letter, digit or underscore.
3.Variable names are case-sensitive. For example- myname is not same as myName.

Example:Use of Variables
i=5
print i
i=i+1
print i



Output:
5
6

Basic Data Types

Now we will discuss about the basic data types in Python. The basic data types used in Python are-
1.Number
a)Integer Number
b)Long Integer Number
c)Floating Point Number
d)Complex Number
2.String

Integer Number
Integer numbers are just whole numbers. Example of integers are- 3,6,-11 etc.

Long Integer Number
Long integers are nothing than bigger numbers.

Floating Point Number
Floating point numbers are numbers with decimal points. For example- 4.25,5.3E-2 etc.

Complex Number
Examples of complex numbers are (2+3j),(-4+6j) etc.

String
A string is just simply sequence of characters. You can use string in Python as following ways-
1.Using single quotes->’This is a string’
2.Using double quotes->”This is a string enclosed in double quotes”
3.Using triple quotes->’’’This is a string enclosed in triple quotes’’’

Note:Triple quotes are generally used in multi-line strings. For example-

‘’’This is a multi-line string.
This is the second line.
Here comes the last line.’’’

Unicode String
In order to use you native language in python, you need to use unicode enabled editor. You can use unicode string in this way-

u’This is a unicode string’



Raw String
If you need to use a special character in your string and you don’t want any special processing, then you need to use raw string. Here is how can you do it-

r”New lines are represented by\n”



Escape Sequence
Let you want to use a string which itself contains a single quotation mark such as What’s your name? You cann’t use ‘What’s your name?’. In that case you need to do a special process called escape sequence. It is done by- ‘What\’s your name?’. You can also use double quotation mark- “What’s your name?”. In case of string containing double quote you need to do the same thing.

Is This A Good Question/Topic? 5
  • +

Replies To: An Introduction to Python

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 07 March 2009 - 03:15 PM

With newer versions of python (version3.0), to print a statement you should use
print("Hello World!")


instead of
print "Hello World!".


Was This Post Helpful? 2
  • +
  • -

#3 desirocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-June 09

Posted 20 June 2009 - 10:45 PM

View Postshadhin, on 7 Mar, 2009 - 02:15 PM, said:

With newer versions of python (version3.0), to print a statement you should use
print("Hello World!")


instead of
print "Hello World!".




Also to add that raw_input is deprecated in the new version (3.0.1) of python. Just need some edits with print statement and raw_input now becomes input.

View Postshadhin, on 7 Mar, 2009 - 02:15 PM, said:

With newer versions of python (version3.0), to print a statement you should use
print("Hello World!")


instead of
print "Hello World!".




Also to add that raw_input is deprecated in the new version (3.0.1) of python. Just need some edits with print statement and raw_input now becomes input.
Was This Post Helpful? 0
  • +
  • -

#4 phoeniX_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-August 09

Posted 06 August 2009 - 02:12 AM

Nice tut, thanks for it!
Was This Post Helpful? 0
  • +
  • -

#5 numberwhun  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 87
  • Joined: 28-March 08

Posted 14 June 2010 - 12:02 PM

Under "Input from user", I think the following line:

print "Nice to meet you %s"%s



should actually read:

print "Nice to meet you %s" % name



as the variable s has not been defined, but name has.

Regards,

Jeff
Was This Post Helpful? 1
  • +
  • -

#6 Themitchel0  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 107
  • Joined: 14-April 10

Posted 08 July 2010 - 05:46 PM

when i try to run the second program i get an error message.
NameError: name 'raw_input' is not defined
i am using python 3.1. thank you
Was This Post Helpful? 0
  • +
  • -

#7 bacNupe10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-August 08

Posted 22 April 2011 - 12:54 PM

Nice tutorial...a good refresher for me!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1