[go: up one dir, main page]

0% found this document useful (0 votes)
11 views8 pages

Chota Abd

Uploaded by

Hafiz Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Chota Abd

Uploaded by

Hafiz Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical 1

Understanding Python Environment: IDE and Basic Commands

Objective:

 To have basic understanding to work in Python Environment


 To understand IDLE and Interpreter
 To familiarize with basic commands of Python
 To use comments in Python Code

Tools/Software Requirement

 Python 3.0

Theory
Introduction to MATLAB

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming


language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also
available under the GNU General Public License (GPL). This tutorial gives enough understanding on
Python programming language. Python is a high-level, interpreted, interactive and object-oriented
scripting language. Python is designed to be highly readable. It uses English keywords frequently whereas
other languages use punctuation, and it has fewer syntactical constructions than other languages.
Many modern languages use both processes. They are first compiled into a lower-level language, called
byte code, and then interpreted by a program called a virtual machine.
Language Translator:
A language translator is a software which translates the programs from a source language that are in
human-readable form into an equivalent program in an object language. The source language is generally
a high-level programming language, and the object language is typically the machine language of an
actual computer.
Types of Translator:
1. Compiler
2. Interpreter

Compiler and Interpreter are two different ways to execute a program written in a programming or
scripting language.
Compiler:
❖ The language processor that translates the complete source program as a whole in machine code
before execution is called compiler.
❖ The C and C++ compilers are best examples of compilers.
❖ The program translated into machine code is called the object program.
❖ The source code is translated to object code successfully if it is free of errors.
❖ If there are any errors in the source code, the compiler specifies the errors at the end of
compilation.
❖ The errors must be removed before the compiler can successfully compile the source code.

Fig. 1.1 Flowchart of execution of a program

Interpreter:
❖ The language processor that translates (converts) each statement of source program into machine
code and executes it immediately before to translate the next statement is called Interpreter.
❖ If there is an error in the statement, the interpreter terminates its translating process at that
statement and displays an error message.

Fig. 1.2 Flowchart of interpretation of a program

Python uses both processes, but because of the way programmers interact with it, it is usually considered
an interpreted language. There are two ways to use the Python interpreter: shell mode and script mode.
In shell mode, you type Python statements into the Python shell and the interpreter immediately prints the
result.
In script mode, you type statements into the editor and save it in a file known as script. The interpreter
executes the code of the script.
i. Running Python Interpreter:
Python comes with an interactive interpreter. When you type python in your shell or command prompt,
the python interpreter becomes active with a >>> (REPL) and waits for your commands.
Fig. 1.3 IDE Shell First Look

Now you can type any valid python expression at the prompt. Python reads the typed expression,
evaluates it and prints the result.

Fig. 1.4 IDE Shell Running print Command

ii. Running Python Scripts in IDLE (Integrated Development and Learning


Environment):

IDLE is the standard Python development environment. Its name is an acronym of "Integrated
Development Learning Environment". It works well on both Unix and Windows platforms. It has a
Python shell window, which gives you access to the Python interactive mode. It also has a file editor that
lets you create and edit existing Python source files.

Go to File menu click on New File (CTRL+N) and write the code and save abc.py. Then run the program
by pressing F5 or Run ==> Run Module
Fig. 1.5 Run Module in IDLE Window

Writing a Python Program:


The text that makes up a Python program has a particular structure. The syntax must be correct, or the
interpreter will generate error messages and not execute the program. A program consists of one or more
statements. A statement is an instruction that the interpreter executes.
input (): This function first takes the input from the user and converts it into a string. When the input
function is called it stops the program and waits for the user’s input. When the user presses enter, the
program resumes and returns what the user typed.

val = input("Enter your value: ")


print(val)

name = input('What is your name?\n')# \n --->newline---> It causes a


line break
print(name)

Python print() function prints the message to the screen or any other standard output device.
Syntax:
print(value(s), sep= ' ', end = '\n')
Parameters:
 value(s): Any value, and as many as you like. Will be converted to a string before printed
 sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one
 end=’end’: (Optional) Specify what to print at the end.
Return Type: It returns output to the screen.

# This line will automatically add a new line before the


# next print statement
print ("Python is the best ")

# This print() function ends with "**" as set in the end argument.
print ("Python is the best ", end= "**")
print("Welcome to IDLE")

a=12
b=12
c=2022
print(a,b,c,sep="-")

Single line comments: Python single-line comment starts with a hashtag symbol with no white spaces
(#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line
and continue the comment.

a, b = 1, 3 # Declaring two integers


sum = a + b # adding two integers
print(sum) # displaying the output

Multi-line string as a comment: Python multi-line comment is a piece of text enclosed in a delimiter
(“””) on each end of the comment. Again there should be no white space between delimiter (“””). They
are useful when the comment text does not fit into one line; therefore need to span across lines.

'''This statement gives you a


perfect example of
multi-line comments'''

print("ABCD")

Lab Task:
Write a code in Python which takes user input for name, registration ID and email address and display the
responses as three different display formats.

Codes:

Name=input('Enter your Name:')


ID=input('Enter your registration ID:')
Email=input('Enter your email address:')
print("format 1:")
print("Name:",Name)
print("Registraion ID:",ID)
print("Email Address:",Email)
print("Format 2:")
print('My Name is',Name)
print('My ID is',ID)
print('My email is',Email)
print("Format 3:")
print("Name:",Name,"|","Registration ID:",ID,"|","Email Address:",Email)
Results:

Enter your Name:M ABDULLAH TAHIR


Enter your registration ID:F23602026
Enter your email address:abdullah.at2287@gmail.com
format 1:
Name: M ABDULLAH TAHIR
Registraion ID: F23602026
Email Address: abdullah.at2287@gmail.com
Format 2:
My Name is M ABDULLAH TAHIR
My ID is F23602026
My email is abdullah.at2287@gmail.com
Format 3:
Name: M ABDULLAH TAHIR | Registration ID: F23602026 | Email Address:
abdullah.at2287@gmail.com

Evaluation Criteria:
Un- Developing Satisfactory Good Exemplary Marks
satisfactory (2) (3) (4) (5)
(0-1)
Does not Completed
comply with less than 75%
Completed at Completed
requirements of the Completed
least 75% of between 80-
(does requirements 100% of
the 99% of the
something . requirements
requirements requirements
Organization other than . Code
Not delivered . .
requirements) appears
/ Structure on time or
. Delivered on Code lacks finished and
not in correct
time, and in some minor organized
Information is format (disk,
correct structural properly
presented in a email,
format. continuity.
very mundane Canvas,
way printout etc.)

Poorly Unclear OR
States explicit
Respective written inaccurate
Correct logic results of a
Code and program results due to
but produces program in
Results and result does with faulty minor
no result due accordance
Conclusion not logic and programming
to an error in with the
complement syntax, logic
the code programming
each other producing negligence or
logic
wrong result. syntax error.

Lab Instructor: Lec. Faria Tasneem Sheikh/Lab Engr. Wahaj Rafique Total

You might also like