Page 1
97
Chapter 1
Getting Started
After studying this lesson, students will be able to:
? Appreciate the use of Graphical User interface and Integrated Development
Environment for creating Python programs.
? Work in interactive & Script mode for programming.
? Create and assign values to variables.
? Understand the concept and usage of different data types in python.
? Appreciate the importance and usage of different types of operator (arithmetic, Relation
and logical)
? Create Python expression(s) and statement(s).
Introduction
In order to tell the computer „what you want to do?, we write a program in a language
which computer can understand. Though there are many different programming
languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. but we will
study Python in this course.
Before learning the technicalities of Python, let?s get familiar with it.
Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica) which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.
Some of the features which make Python so popular are as follows:
? It is a general purpose programming language which can be used for both
scientific and non scientific programming.
? It is a platform independent programming language.
Page 2
97
Chapter 1
Getting Started
After studying this lesson, students will be able to:
? Appreciate the use of Graphical User interface and Integrated Development
Environment for creating Python programs.
? Work in interactive & Script mode for programming.
? Create and assign values to variables.
? Understand the concept and usage of different data types in python.
? Appreciate the importance and usage of different types of operator (arithmetic, Relation
and logical)
? Create Python expression(s) and statement(s).
Introduction
In order to tell the computer „what you want to do?, we write a program in a language
which computer can understand. Though there are many different programming
languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. but we will
study Python in this course.
Before learning the technicalities of Python, let?s get familiar with it.
Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica) which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.
Some of the features which make Python so popular are as follows:
? It is a general purpose programming language which can be used for both
scientific and non scientific programming.
? It is a platform independent programming language.
98
? It is a very simple high level language with vast library of add-on modules.
? It is excellent for beginners as the language is interpreted, hence gives immediate
results.
? The programs written in Python are easily readable and understandable.
? It is suitable as an extension language for customizable applications.
? It is easy to learn and use.
The language is used by companies in real revenue generating products, such as:
? In operations of Google search engine, youtube, etc.
? Bit Torrent peer to peer file sharing is written using Python
? Intel, Cisco, HP, IBM, etc use Python for hardware testing.
? Maya provides a Python scripting API
? i–Robot uses Python to develop commercial Robot.
? NASA and others use Python for their scientific programming task.
First Step with Python
We are continuously saying that Python is a programming language but don?t know
what a program is? Therefore, let?s start Python by understanding Program.
A program is a sequence of instructions that specifies how to perform a Computation.
The Computation might be mathematical or working with text.
To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment. IDLE is an acronym of Integrated Development Environment. It lets edit,
run, browse and debug Python Programs from a single interface. This environment
makes it easy to write programs.
We will be using version 2.7 of Python IDLE to develop and run Python code, in this
course. It can be downloaded from www.python.org
Python shell can be used in two ways, viz., interactive mode and script mode. Where
Interactive Mode, as the name suggests, allows us to interact with OS; script mode let us
Page 3
97
Chapter 1
Getting Started
After studying this lesson, students will be able to:
? Appreciate the use of Graphical User interface and Integrated Development
Environment for creating Python programs.
? Work in interactive & Script mode for programming.
? Create and assign values to variables.
? Understand the concept and usage of different data types in python.
? Appreciate the importance and usage of different types of operator (arithmetic, Relation
and logical)
? Create Python expression(s) and statement(s).
Introduction
In order to tell the computer „what you want to do?, we write a program in a language
which computer can understand. Though there are many different programming
languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. but we will
study Python in this course.
Before learning the technicalities of Python, let?s get familiar with it.
Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica) which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.
Some of the features which make Python so popular are as follows:
? It is a general purpose programming language which can be used for both
scientific and non scientific programming.
? It is a platform independent programming language.
98
? It is a very simple high level language with vast library of add-on modules.
? It is excellent for beginners as the language is interpreted, hence gives immediate
results.
? The programs written in Python are easily readable and understandable.
? It is suitable as an extension language for customizable applications.
? It is easy to learn and use.
The language is used by companies in real revenue generating products, such as:
? In operations of Google search engine, youtube, etc.
? Bit Torrent peer to peer file sharing is written using Python
? Intel, Cisco, HP, IBM, etc use Python for hardware testing.
? Maya provides a Python scripting API
? i–Robot uses Python to develop commercial Robot.
? NASA and others use Python for their scientific programming task.
First Step with Python
We are continuously saying that Python is a programming language but don?t know
what a program is? Therefore, let?s start Python by understanding Program.
A program is a sequence of instructions that specifies how to perform a Computation.
The Computation might be mathematical or working with text.
To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment. IDLE is an acronym of Integrated Development Environment. It lets edit,
run, browse and debug Python Programs from a single interface. This environment
makes it easy to write programs.
We will be using version 2.7 of Python IDLE to develop and run Python code, in this
course. It can be downloaded from www.python.org
Python shell can be used in two ways, viz., interactive mode and script mode. Where
Interactive Mode, as the name suggests, allows us to interact with OS; script mode let us
99
create and edit python source file. Now, we will first start with interactive mode. Here,
we type a Python statement and the interpreter displays the result(s) immediately.
Interactive Mode
For working in the interactive mode, we will start Python on our computer. You can
take the help of your Teacher.
When we start up the IDLE following window will appear:
What we see is a welcome message of Python interpreter with revision details and the
Python prompt, i.e., „>>>?. This is a primary prompt indicating that the interpreter is
expecting a python command. There is secondary prompt also which is „…? indicating
that interpreter is waiting for additional input to complete the current statement.
Interpreter uses prompt to indicate that it is ready for instruction. Therefore, we can
say, if there is prompt on screen, it means IDLE is working in interactive mode.
We type Python expression / statement / command after the prompt and Python
immediately responds with the output of it. Let?s start with typing print “How are you”
after the prompt.
>>>print “How are you?”
How are you?
What we get is Python?s response. We may try the following and check the response:
i) print 5+7
Page 4
97
Chapter 1
Getting Started
After studying this lesson, students will be able to:
? Appreciate the use of Graphical User interface and Integrated Development
Environment for creating Python programs.
? Work in interactive & Script mode for programming.
? Create and assign values to variables.
? Understand the concept and usage of different data types in python.
? Appreciate the importance and usage of different types of operator (arithmetic, Relation
and logical)
? Create Python expression(s) and statement(s).
Introduction
In order to tell the computer „what you want to do?, we write a program in a language
which computer can understand. Though there are many different programming
languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. but we will
study Python in this course.
Before learning the technicalities of Python, let?s get familiar with it.
Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica) which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.
Some of the features which make Python so popular are as follows:
? It is a general purpose programming language which can be used for both
scientific and non scientific programming.
? It is a platform independent programming language.
98
? It is a very simple high level language with vast library of add-on modules.
? It is excellent for beginners as the language is interpreted, hence gives immediate
results.
? The programs written in Python are easily readable and understandable.
? It is suitable as an extension language for customizable applications.
? It is easy to learn and use.
The language is used by companies in real revenue generating products, such as:
? In operations of Google search engine, youtube, etc.
? Bit Torrent peer to peer file sharing is written using Python
? Intel, Cisco, HP, IBM, etc use Python for hardware testing.
? Maya provides a Python scripting API
? i–Robot uses Python to develop commercial Robot.
? NASA and others use Python for their scientific programming task.
First Step with Python
We are continuously saying that Python is a programming language but don?t know
what a program is? Therefore, let?s start Python by understanding Program.
A program is a sequence of instructions that specifies how to perform a Computation.
The Computation might be mathematical or working with text.
To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment. IDLE is an acronym of Integrated Development Environment. It lets edit,
run, browse and debug Python Programs from a single interface. This environment
makes it easy to write programs.
We will be using version 2.7 of Python IDLE to develop and run Python code, in this
course. It can be downloaded from www.python.org
Python shell can be used in two ways, viz., interactive mode and script mode. Where
Interactive Mode, as the name suggests, allows us to interact with OS; script mode let us
99
create and edit python source file. Now, we will first start with interactive mode. Here,
we type a Python statement and the interpreter displays the result(s) immediately.
Interactive Mode
For working in the interactive mode, we will start Python on our computer. You can
take the help of your Teacher.
When we start up the IDLE following window will appear:
What we see is a welcome message of Python interpreter with revision details and the
Python prompt, i.e., „>>>?. This is a primary prompt indicating that the interpreter is
expecting a python command. There is secondary prompt also which is „…? indicating
that interpreter is waiting for additional input to complete the current statement.
Interpreter uses prompt to indicate that it is ready for instruction. Therefore, we can
say, if there is prompt on screen, it means IDLE is working in interactive mode.
We type Python expression / statement / command after the prompt and Python
immediately responds with the output of it. Let?s start with typing print “How are you”
after the prompt.
>>>print “How are you?”
How are you?
What we get is Python?s response. We may try the following and check the response:
i) print 5+7
100
ii) 5+7
iii) 6*250/9
iv) print 5-7
It is also possible to get a sequence of instructions executed through interpreter.
Example 1 Example 2
>>> x=2
>>> y=6
>>> z = x+y
>>> print z
8
>>> a=3
>>> a+1, a-1
(4,2) #result is tuple of 2 values
#result is tuple of 2 values, is a comment statement. We will talk about it in the later
part of chapter.
Now we are good to write a small code on our own in Python. While writing in Python,
remember Python is case sensitive. That means x & X are different in Python.
Note: If we want to repeat prior command in interactive window, you can use „ ? key
to scroll backward through commands history and „ ? key to scroll forward. Use Enter
key to select it. Using these keys, your prior commands will be recalled and displayed,
and we may edit or rerun them also.
^D (Ctrl+D) or quit () is used to leave the interpreter.
^F6 will restart the shell.
Help of IDLE can be explored to know about the various menu options available for
Programmer.
Apart from writing simple commands, let?s explore the interpreter more.
Page 5
97
Chapter 1
Getting Started
After studying this lesson, students will be able to:
? Appreciate the use of Graphical User interface and Integrated Development
Environment for creating Python programs.
? Work in interactive & Script mode for programming.
? Create and assign values to variables.
? Understand the concept and usage of different data types in python.
? Appreciate the importance and usage of different types of operator (arithmetic, Relation
and logical)
? Create Python expression(s) and statement(s).
Introduction
In order to tell the computer „what you want to do?, we write a program in a language
which computer can understand. Though there are many different programming
languages such as BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python, etc. but we will
study Python in this course.
Before learning the technicalities of Python, let?s get familiar with it.
Python was created by Guido Van Rossum when he was working at CWI (Centrum
Wiskunde & Informatica) which is a National Research Institute for Mathematics and
Computer Science in Netherlands. The language was released in I991. Python got its
name from a BBC comedy series from seventies- “Monty Python?s Flying Circus”.
Python can be used to follow both Procedural approach and Object Oriented approach
of programming. It is free to use.
Some of the features which make Python so popular are as follows:
? It is a general purpose programming language which can be used for both
scientific and non scientific programming.
? It is a platform independent programming language.
98
? It is a very simple high level language with vast library of add-on modules.
? It is excellent for beginners as the language is interpreted, hence gives immediate
results.
? The programs written in Python are easily readable and understandable.
? It is suitable as an extension language for customizable applications.
? It is easy to learn and use.
The language is used by companies in real revenue generating products, such as:
? In operations of Google search engine, youtube, etc.
? Bit Torrent peer to peer file sharing is written using Python
? Intel, Cisco, HP, IBM, etc use Python for hardware testing.
? Maya provides a Python scripting API
? i–Robot uses Python to develop commercial Robot.
? NASA and others use Python for their scientific programming task.
First Step with Python
We are continuously saying that Python is a programming language but don?t know
what a program is? Therefore, let?s start Python by understanding Program.
A program is a sequence of instructions that specifies how to perform a Computation.
The Computation might be mathematical or working with text.
To write and run Python program, we need to have Python interpreter installed in our
computer. IDLE (GUI integrated) is the standard, most popular Python development
environment. IDLE is an acronym of Integrated Development Environment. It lets edit,
run, browse and debug Python Programs from a single interface. This environment
makes it easy to write programs.
We will be using version 2.7 of Python IDLE to develop and run Python code, in this
course. It can be downloaded from www.python.org
Python shell can be used in two ways, viz., interactive mode and script mode. Where
Interactive Mode, as the name suggests, allows us to interact with OS; script mode let us
99
create and edit python source file. Now, we will first start with interactive mode. Here,
we type a Python statement and the interpreter displays the result(s) immediately.
Interactive Mode
For working in the interactive mode, we will start Python on our computer. You can
take the help of your Teacher.
When we start up the IDLE following window will appear:
What we see is a welcome message of Python interpreter with revision details and the
Python prompt, i.e., „>>>?. This is a primary prompt indicating that the interpreter is
expecting a python command. There is secondary prompt also which is „…? indicating
that interpreter is waiting for additional input to complete the current statement.
Interpreter uses prompt to indicate that it is ready for instruction. Therefore, we can
say, if there is prompt on screen, it means IDLE is working in interactive mode.
We type Python expression / statement / command after the prompt and Python
immediately responds with the output of it. Let?s start with typing print “How are you”
after the prompt.
>>>print “How are you?”
How are you?
What we get is Python?s response. We may try the following and check the response:
i) print 5+7
100
ii) 5+7
iii) 6*250/9
iv) print 5-7
It is also possible to get a sequence of instructions executed through interpreter.
Example 1 Example 2
>>> x=2
>>> y=6
>>> z = x+y
>>> print z
8
>>> a=3
>>> a+1, a-1
(4,2) #result is tuple of 2 values
#result is tuple of 2 values, is a comment statement. We will talk about it in the later
part of chapter.
Now we are good to write a small code on our own in Python. While writing in Python,
remember Python is case sensitive. That means x & X are different in Python.
Note: If we want to repeat prior command in interactive window, you can use „ ? key
to scroll backward through commands history and „ ? key to scroll forward. Use Enter
key to select it. Using these keys, your prior commands will be recalled and displayed,
and we may edit or rerun them also.
^D (Ctrl+D) or quit () is used to leave the interpreter.
^F6 will restart the shell.
Help of IDLE can be explored to know about the various menu options available for
Programmer.
Apart from writing simple commands, let?s explore the interpreter more.
101
Type Credits after the prompt and what we get is information about the organization
involved in Python development. Similarly, Copyright and Licenses command can be
used to know more about Python. Help command provides help on Python. It can be
used as….. help() with nothing in parenthesis will allow us to enter an interactive help
mode. And with a name (predefined) in bracket will give us details of the referred
word.
To leave the help mode and return back to interactive mode, quit command can be
used.
Script Mode
In script mode, we type Python program in a file and then use the interpreter to execute
the content from the file. Working in interactive mode is convenient for beginners and
for testing small pieces of code, as we can test them immediately. But for coding more
than few lines, we should always save our code so that we may modify and reuse the
code.
Note: Result produced by Interpreter in both the modes, viz., Interactive and script
mode is exactly same.
Python, in interactive mode, is good enough to learn, experiment or explore, but its only
drawback is that we cannot save the statements for further use and we have to retype
all the statements to re-run them.
To create and run a Python script, we will use following steps in IDLE, if the script
mode is not made available by default with IDLE environment.
1. File>Open OR File>New Window (for creating a new script file)
2. Write the Python code as function i.e. script
3. Save it (^S)
4. Execute it in interactive mode- by using RUN option (^F5)
Otherwise (if script mode is available) start from Step 2
Note: For every updation of script file, we need to repeat step 3 & step 4
Read More