Class 7 Introduction To Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Introduction to Python

Computer Programming Language


A programming language is a computer language used by programmer to develop software
programs, scripts or other sets of instructions for computers to execute. These instructions are
written using computer programming languages which allow us to give instructions to a
computer in a language that computer understands. The term programming language usually
refers to high-level languages, such as BASIC, C, C++, COBOL, Java, FORTRAN, Python etc.
Almost all programming languages work the same way:
1. We write code to tell it what to do.
2. The code is compiled, which turns it into machine code the computer can
understand.
3. The computer executes the code and displays output.

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.

Download and Install Python for Windows Systems


The Windows Python interpreter can be downloaded for free from the Python
website:
https://www.python.org/downloads/
Once you have chosen and downloaded an installer, simply run it by
double-clicking on the downloaded file. A dialog box should appear that looks
something like this:
Then just click Install Now. A few minutes later you should have a working Python
3.7 installation on your system.

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.

Quit The Python Shell


It's easy to end the interactive session: You can either use exit(), quit() or Ctrl+D to
exit. The brackets with the exit and quit function are crucial. You will receive a
message box and click OK to quit the python shell.

Python's Interactive Shell


The easiest way to get started is to run Python as an interpreter (interactive shell),
which behaves similar to the way one would use a calculator. In the interpreter, you
type a command and Python produces the answer. Then you type next command,
which produces answer and so on.
The interpreter (interactive shell) can acts as a simple calculator, you can type an
expression in it and it will give the value. Let's try some simple Python commands.
Start the interpreter or interactive in shell and a window with the>>> prompt
appears.
The >>> is the prompt, telling that IDLE is waiting for you to type something.
Enter 2 + 2 at the prompt to instruct Python to do some simple mathematics. This
math problem is a simple programming instruction. The + sign tells the computer to
add the numbers 2 and 2.
TABLE: Math Operators from Highest to Lowest Precedence
Operator Operation Example Evaluates to...
** Exponent 2 ** 3 8
% Modulus/remainder 22 % 8 6
// Integer division or 22 // 8 2
floored quotient
/ Division 22 / 8 2.75
* Multiplication 3*5 15
- Subtraction 5-2 3
+ Addition 2+2 4

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.

Multiplication, Parentheses and Precedence


Try in the Shell:
>>>(2+3)*4
20
>>>2 * (4 - 1)
6
Negation also works. Try in the Shell : -(2 + 3)

Using // and % Operator


Division (/) always returns a float, so to get an integer result (discarding any
fractional result) you should use the //(floor) operator and to calculate the
remainder you can use % (modulus).
>>> 17 / 3
5.666666666666667 (division returns a float)
>>> 17 // 3
5 (discards the fractional part)
>>> 17 % 3
2 (the % operator returns the remainder of the division)

Your First Python Program


While the interactive shell is good for running Python instructions one at a time, to
write entire Python programs, you'll type the instructions into the file editor. The file
editor is similar to text editors such as Notepad, but it has some specific features for
typing in source code.
Step 1: To open the file editor in IDLE, select File New File
The window that appears contains a cursor awaiting your input, but it's different
from the interactive shell, which runs Python instructions as soon as you press
ENTER. The file editor lets you type in many instructions, save the file and run the
program.
The interactive shell window will always be the one with the >>> prompt.
The file editor window will not have the >>> prompt.
Step 2: When the file editor window opens, type the following into it.
Once you've entered your source code, save it so that you don't have to retype it
each time you start IDLE.
Step 3: From the menu at the top of the file editor window, select File > Save As. In
the Save As window, enter program 1.py in the File Name field and then click Save.
You should save your programs every once in a while as you type them. That way, if
you accidentally exit from IDLE, you won't loose the code. As a shortcut, you can press
CTRL+S.
Once you've saved, let's run our program.
print("My name is Saurav")
print("I am learning Python")
print("Addition of 10 and 5 =", 10+5)

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.

Reload a Saved Program


Step 1: Select File Open from the menu.
Step 2: In the window that appears, choose program1.py and click the Open button.
Your previously saved add.py program should open in the file editor window.

ASSIGNMENT
Tick the correct option for the following statements.

1. One of the following is not true for Python.

a) It is high level programming language.

b) It can be run on MS Windows, Linux and Mac OS.

c) It is only available after paying license fee.

2. A command line shell which gives immediate output for each typed statement.
a) Interactive Mode b) Command Mode c) Output Mode

3. This is the prompt of the interactive shell of the Python window.

a) <<< b) >>> c) ///

4. This type of error occurs when a program does not confirm to the grammar of a programming language.

a) Logical error b) Syntax error c) Grammatical error

5. Python uses this symbols for calculating exponential value.

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 –

7. The output of the (4+5)*2+7+2 would be one of these:

a) 27 b) 99 c) 102

8. The shortcut key to run the Python program.

a) F10 Key b) F6 Key c) F5 Key

9. The file extension name for Python programs files.

a) .pyt b) .py c) .ph

10. The function/shortcut key to quit the Python shell.

a) quit() b) exit() c) Ctrl + D d) Any of them

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.

1. >>> 8*(4+3)+(7+2) _______________________________________________

2. >>> 5**2 _______________________________________________

3. >>> 181//2 _______________________________________________

4. >>>print(“Good Morning” , ”Friends”) _______________________________________________

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")

3. print("square root of a number")


print (4**0.5)

4. print (5,"*",4,"=",5*4)
print (10,"+",6,"=",10+6)

5. print (" ********************** ")


print (" * H A P P Y * ")
print (" * B I R T H D A Y * ")
print (" * T O * ")
print (" * Y O U * ")
print (" ********************** ")

You might also like