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-
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 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-
#Same indentation & will run successfully i=6 print " The value of i is",i
#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-
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 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 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
Basic Data Types
Now we will discuss about the basic data types in Python. The basic data types used in Python are-
b)Long Integer Number
c)Floating Point 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.
Examples of complex numbers are (2+3j),(-4+6j) etc.
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.’’’
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’
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”
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.