Class 7 Introduction To Python
Class 7 Introduction To Python
Knowing Python
Python is a powerful modern computer programming language. It is a
general-purpose interpreted, interactive, object-oriented and high-level
programming language that runs on many Unix variants, on the Mac and Windows.
In 1991, Guido Van Rossum developed the Python programming language and is
referred as father of Python language.
Python is easy to learn, highly readable and simple to use. It has a clean and
English-like syntax, which requires less coding and lets the programmer focus on
the business logic rather than thinking about the practical details of the language.
Features of Python are as follows:
● It is a great language for the beginner-level programmers and supports the
development of a wide range of applications from simple text processing to
WWW browsers to games.
● It is Open source language and compatible with all types of operating
systems like, Linux, Ubuntu, MS Windows, Mac OS etc.
● It uses English keywords frequently where as other languages use
punctuation and it has fewer syntactical constructions than other
programming languages.
● It supports procedure-oriented programming as well as object-oriented
programming.
Starting Python
On Windows, open the Start menu, select All Programs Python 3.7 and then select
IDLE (Python 3.7 32 bit).
A window with the >>> prompt should appear, that's the interactive shell. In this
window we execute the python statement one by one. It is preferable in case
where we are concerned about the output of each line of our python program.
print() Function
Now that we have successfully completed the installation process and we are
ready to create our first basic Python script.
Once the GUI is open, we will begin by using the simplest directive that is the
'print()' which simply prints whatever you instruct it to do, in the new line.
In python we use print() function to display string at the console and it must be
invoked with parentheses.
It can accept more than one argument. When two or more arguments are passed,
print function displays each argument separated by space.
Now we will create the mandatory "Hello World" statement. Type the following
code and press the 'Enter' Key':
>>>print("Hello World!")
can print easily one more object in print() function like this:
>>>print("Hello,", "How Are You?")
Type and enter the following command on the Python command prompt:
>>>Print(“Hello, World!”)
This is how Python will respond:
You’ll get syntax error messages whenever you enter invalid or incomplete
statements. In this case, you typed print with a capital letter. Because Python is case
sensitive. Hence Print() and print() are not same.
Order of Operations
The order of operations (also called precedence) of Python math operators is
similar to that of mathematics.
An easy way to remember the order of operations is PEDMAS or 'Please Excuse My
Dear Aunt Sally'.
1. Parentheses
2. Exponents
3. Multiplication and Division
4. Addition and Subtraction
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has
higher precedence than +, so it first multiplies 3*2 and then adds into 7 but if
x=(7+3)*2 then x is assigned to 20.
Module or just press the F5 key. Your program should run in the interactive shell
window that appeared when you first started IDLE. Remember, you have to press
F5 from the file editor window, not the interactive shell window. The program's
output in the interactive shell should look something like this:
When there are no more lines of code to execute, the Python program terminates;
that is, it stops running. You can close the file editor by clicking the close button at
the top of the window.
One of the great things about Python is that you can test your new programs
immediately. A good practice is to have your command prompt open at the same
time while you have your editor open. When you save your changes in your editor,
you can immediately run the program from the command line, allowing you to
quickly test the changes.
ASSIGNMENT
Tick the correct option for the following statements.
2. A command line shell which gives immediate output for each typed statement.
a) Interactive Mode b) Command Mode c) Output Mode
4. This type of error occurs when a program does not confirm to the grammar of a programming language.
a) ** b) ^ c) //
6. In Python following operator is evaluated first in any mathematical expression as per the order of
operation rules.
a) ** b) * c) + or –
a) 27 b) 99 c) 102
Answers in brief:
1. In Python, a command line shell which gives immediate feedback for each statement. Interactive Mode
2. A set of rules specifying which procedure should be performed first in a given mathematical expression.
PEDMAS
3. In Python, a window used to write or edit python programs. File Editor Window
4. In Python, name the operator which returns the quotient of the division. /
Try the following commands in interactive shell and write the output.
5. >>>17%3 _______________________________________________
ACTIVITY Lab Activity
Try following program using print() function.
1. print('First Number = 10 and Second Number =5')
print('Addition =')
print(10+5)
print('Subtraction =')
print(10-5)
print('Multiplication =')
print(10*5)
2. print("There are")
print (365*24*60)
print ("Minutes in a Year")
4. print (5,"*",4,"=",5*4)
print (10,"+",6,"=",10+6)