0% found this document useful (0 votes)
36 views50 pages

Practical # 1

This document provides an overview of Python programming, including installation, features, syntax, and basic programming constructs such as variables, operators, and control flow statements. It highlights Python's readability, object-oriented nature, and suitability for beginners, along with examples of coding practices. Additionally, it outlines class tasks and lab assignments for practical application of Python concepts.

Uploaded by

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

Practical # 1

This document provides an overview of Python programming, including installation, features, syntax, and basic programming constructs such as variables, operators, and control flow statements. It highlights Python's readability, object-oriented nature, and suitability for beginners, along with examples of coding practices. Additionally, it outlines class tasks and lab assignments for practical application of Python concepts.

Uploaded by

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

PRACTICAL

# UNDERSTAND
TO 1 BASICS OF
PYTHON

Komal Naz Soomro


INSTALLATI
ON
•Text editor
(sublime/pycharm
)
•Python 3.12.5
PYTHON
(OVERVIEW)
•Python is a high-level, interpreted, interactive and object-
oriented scripting language.
•Python is designed to be highly readable.
•It uses English keywords frequently where as other languages
use punctuation.
• Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is
similar to PERL and PHP.
• Python is Interactive − You can actually sit at a Python prompt and
interact with the
interpreter directly to write your programs.
• Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
• Python is a Beginner's Language − Python 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.
•Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and
other scripting languages.
•Python source code is now available under the GNU General
Public License (GPL).
FEATUR
ES
•Easy-to-read
•Easy to learn
•Open Source
•Portable − Python can run on a wide variety of hardware
platforms and has the same interface on all platforms.
FEATUR
ES
•It supports functional and structured programming methods as
well as OOP.
•It can be used as a scripting language or can be compiled to
byte-code for building large applications.
•It provides very high-level dynamic data types and supports
dynamic type checking.
•It supports automatic garbage collection.
•It can be easily integrated with C, C++, COM, CORBA, and
Java.
•Python is an interpreted programming language. Python source
code is compiled to bytecode as a .pycfile, and this bytecode
can be interpreted.
•There are two modes for using the Python interpreter:
• Interactive Mode
• Script Mode
INTERACTIVE
MODE:
Without passing python script file to the interpreter, directly execute code to
Python prompt.
Example:
SCRIPT
MODE
•Store Python script source code in a file with the .py extension,
and use the interpreter to execute the contents of the file. To
execute the script by the interpreter, you have to tell the
interpreter the name of the file. For example, if you have a
script name MyFile.py
SYNTA
X
SYNTA
X
• Simple Print Statement:
print(“hello world!”)
• Python Identifiers
• A Python identifier is a name used to identify a variable, function, class, module or
other object. An identifier starts with a letter A to Z or a to z or an underscore (_)
followed by zero or more letters, underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @, $, and % within identifiers.
Python is a case sensitive programming language. Thus, Manpower and manpower are
two different identifiers in Python.
• Here are naming conventions for Python identifiers −
• Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore indicates that the identifier is
private.
• Starting an identifier with two leading underscores indicates a strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier is a language-defined
RESERVED
WORDS
LINES AND
INDENTATION
•Python provides no braces to indicate blocks of code for class
and function definitions or flow control. Blocks of code are
denoted by line indentation, which is rigidly enforced.
if True:
print "True"
else:
print "False"
MULTI-LINE
STATEMENTS

QUOTATION IN
PYTHON
COMMENTS IN
PYTHON
•Taking User Input
input(“waiting for user input”)

•Multiple Statements on a Single


Line:
Import sys; x = 'foo'; sys.stdout.write(x + '\n')
VARIABLE
TYPES
ASSIGNIN
G
VALUES
TO
• INTEGER
V
• ARIABLES
FLOAT
• STRING
• BOOLEA
N

MULTIPLE
ASSIGNMEN
T
PYTHON
OPERATORS
PYTHON
OPERATORS
Operators are the constructs which can manipulate the value
of operands. Types of Operator
•Arithmetic Operators
•Comparison (Relational) Operators
•Assignment Operators
•Logical Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
PYTHON ARITHMETIC OPERATORS
PYTHON
COMPARISO
N
OPERATORS
These operators
compare the
values on either
sides of them
and decide the
relation among
them. They are
also called
Relational
operators.
PYTHON
ASSIGNMEN
T
OPERATORS
PYTHON
MEMBERSHIP
OPERATORS

Python’s
membership
operators test for
membership in
a sequence,
such as strings,
lists, or tuples.
EXAMPL
E
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print ("Line 1 - a is available in the given list“)
else:
print ("Line 1 - a is not available in the given list“)

Output: Line 1 - a is not available in the given list


IDENTITY
OPERATO
RS
•Identity
operators
compare the
memory
locations of two
objects
EXAMPL
E
OUTPU
T
BITWISE
OPERATO
RS
•Bitwise operator
works on bits
and performs
bit by bit
operation.
Assume if a =
60; and b =
13;
LOGICAL
OPERATO
RS
DECISION
MAKING
DECISION
MAKING
STATEMEN
T
IF
STATEMENT
Syntax:
if expression: #execute your
code

Example:
a = 15
if a > 10:
print("a is greater")

Output:a is greater
IF ELSE
STATEMENT
Syntax:
if expression: #execute your
code else: #execute your code
Example:
a = 15
b = 20
if a > b:
print("a is
greater") else:
print("b is
greater")
Output: b is greater
ELIF
STATEMENTS
Syntax:
if expression: #execute your
code
elif expression: #execute your
code else: #execute your code
Example:
a = 15
if=a15 b:
b
>
print("a greater")
is
elif == b:
a
print("bot are
Output:hboth are equal")
else:
equal
print("b greater")
is
LOOP
S
LOOP
S
•For loop
•While
loop
•Nested
loop
FOR
LOOP
Syntax:
for iterating_var in sequence: #execute your
code
Example 01:
for x in range (0,3) :
print ('Loop execution %d' % (x))
Loop executio 0
Output:
n
Loop executio 1
n
Loop executio 2
n
WHILE
Syntax:
LOOP
while expression: #execute your
code
Example:
#initialize count variable
to 1 count =1
while count < 6 :
print (count)
#the
count+=1 line means = count +
above count 1
Outpu
t:
1
2
3
4
5
NESTED
LOOP
Syntax:
for iterating_var in sequence:
for iterating_var in sequence: #execute your code
#execute
your code
Example:
for g in range(1,
6): for k in
range(1, 3):
1 * 1 print
= 1 ("%d *
1 * 2 =%d2 = %d" %
2 * 1 =( 2g, k, g*k))
Output:
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10
LOOP
CONTROLS
LOOP
CONTRO
L
•These
statements are
used to change
execution from
its normal
sequence.
BREAK
STATEMENT
Syntax:
break
Example:
count = 0
while count <=
100: print
(count)
count += 1
if count >= 3:
break
Output:
0 1 2
CONTINUE
Syntax:
STATEMENT
continue
Example:
for x in range(10): #check whether x is
even if x % 2 == 0:
continue
print (x)
Output:
1
3
5
7
9
CLASS
TASKS:
•Write a marksheet and display marks obtained, percentage and grade
•Write a program for table
Class task 3:
LAB # 1:
TASKS
•Write a Python program which accepts the radius of a circle
from the user and compute the area.
• Write a Python program to guess a number between 1 to 9.
Hint(use radom method to randomly generate numbers).
•Write a Python program that accepts a word from the user and
reverse it.
•Write a Python program that prints all the numbers from 0 to 6
except 3 and
6. Note : Use 'continue' statement.
•Write a Python program which iterates the integers from 1 to 50.
For multiples of three print “Multiple of 3" instead of the number
and for the multiples of five print “Multiple of 5". For numbers
which are multiples of both three and five print “Multiple of
both 3 & 5".
•Write a Python program to check whether an alphabet is
a vowel or consonant.
•Write a Python program to convert temperatures to and
from celsius, fahrenheit.
LAB SUBMISSION
DATE:11/09/2024

You might also like