0% found this document useful (0 votes)
6 views

Lab 3 Programming

Uploaded by

acesenpai191
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab 3 Programming

Uploaded by

acesenpai191
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 3: Running Python Programs in Interactive Mode

Overview: Python comes with an interactive interpreter. Entering python on the command
line without any parameters will launch the Python interpreter and the python interpreter
becomes active with a >>> prompt and waits for your commands. This is a text console in
which you can enter Python commands one by one – they will be interpreted on the fly.
Here is an example of an interpreter prompt:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on
win32
Type "copyright", "credits" or "license()" for more information.
>>>

Now you can type any valid Python expression at the prompt. Python reads the typed in
expression, evaluates it and prints the result. There are various ways to start Python.

1. Immediate mode

Typing python in the command line will invoke the interpreter in immediate mode. We can
directly type in Python expressions and press enter to get the output.

>>>

is the Python prompt. It tells us that the interpreter is ready for our input. Try typing in 1 +
1 and press enter. We get 2 as the output. This prompt can be used as a calculator. To exit this
mode type exit() or quit() and press enter.

2. Script mode

This mode is used to execute Python program written in a file. Such a file is called a script.
Scripts can be saved to disk for future use. Python scripts have the extension .py, meaning
that the filename ends with .py.

For example: helloWorld.py

To execute this file in script mode we simply write python helloWorld.py at the command
prompt.

Task 1: Type the following commands in the Python editor and observe their output.

>>> print ("Hello, world!")


>>> 1 + 1
2

>>> 20 + 80
100

>>> 18294 + 449566


467860
(These are additions.)

>>> 6 - 5
1
(Subtraction)

>>> 2 * 5
10
(Multiplication)
>>> 5 ** 2
25
(Exponentials; e.g., this one is 5 squared)

>>> print ("1 + 2 is an addition")


1 + 2 is an addition
(The print statement, which writes something on screen. Notice that 1 + 2 is left
unevaluated.)

>>> print ("One kilobyte is 2^10 bytes, or", 2 ** 10, "bytes.")


One kilobyte is 2^10 bytes, or 1024 bytes.
(You can print sums and variables in a sentence. The commas separating each section are a
way of separating clearly different things that you are printing.)

>>> 21 / 3
7

>>> 23 / 3
7
(Division; note that Python ignores remainders/decimals.)

>>> 23.0 / 3.0


7.666666666666667
(This time, since the numbers are decimals themselves, the answer will be a decimal.)

>>> 23 % 3
2
>>> 49 % 10
9
(The remainder from a division)

Task 2: Perform the calculations using following equations in interactive mode of Python
and display individually the output of each. Submit results in PDF.
i. 31/2
ii. 31//2
iii. 2.5 + 2.5
iv. 7.5//2.4
v. 72 % 4
vi. 4%2*6%2
vii. 3+5*5–1*7/2+3
viii. 3**2 + 7**2 + 2*3*7
ix. 6**2 + 9**2 - 2*6*9
x. 1**2 + 2**2 + 3**2 + 4**2 + 5**2

Task 3: Print your name, father name, address, contact number, intermediate marks and
grade using the Python Print command. Save your output in a PDF file and upload the same
on LMS. Your output should look like as given below.

My Biodata
Name: Your Name
Father Name: Your father name
Address: Your house address
Phone No.: 0123-9121212
FSc Marks obtained/Total Marks: 760/1100
Grade: A

You might also like