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

Practical # 1

The document discusses the basics of Python programming language including its features, syntax, variables, operators, decision making statements like if/else and loops like for/while. It also provides some tasks for students as practical exercises on topics like functions, classes, conditions and loops.

Uploaded by

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

Practical # 1

The document discusses the basics of Python programming language including its features, syntax, variables, operators, decision making statements like if/else and loops like for/while. It also provides some tasks for students as practical exercises on topics like functions, classes, conditions and loops.

Uploaded by

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

PRACTICAL # 1

TO UNDERSTAND BASICS OF PYTHONS

EMAN SHAHID
INSTALLATION

• Text editor (sublime)


• Python 3.6.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 is copyrighted. Python source code is now available under the GNU
General Public License (GPL).
FEATURES

• 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.
FEATURES

• t 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
SYNTAX
SYNTAX

• 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 special name.
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
ASSIGNING
VALUES TO
VARIABLES
• INTEGER
• FLOAT
• STRING
• BOOLEAN

MULTIPLE
ASSIGNMENT
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
COMPARISON
OPERATORS

These operators
compare the values
on either sides of
them and decide the
relation among them.
They are also called
Relational operators.
PYTHON
ASSIGNMENT
OPERATORS
PYTHON
MEMBERSHIP
OPERATORS

Python’s membership
operators test for
membership in a
sequence, such as
strings, lists, or tuples.
EXAMPLE

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
OPERATORS

• Identity operators
compare the
memory locations of
two objects
EXAMPLE
OUTPUT
BITWISE
OPERATORS

• Bitwise operator
works on bits and
performs bit by bit
operation. Assume
if a = 60; and b =
13;
LOGICAL
OPERATORS
DECISION MAKING
DECISION
MAKING
STATEMENT
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
b = 15
if a > b:
print("a is greater")
elif a == b:
print("both are equal")
else:
print("b is greater")
Output: both are equal
LOOPS
LOOPS

• 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))
Output:
Loop execution 0
Loop execution 1
Loop execution 2
WHILE LOOP
Syntax:
while expression: #execute your code
Example:
#initialize count variable to 1
count =1
while count < 6 :
print (count)
count+=1
#the above line means count = count + 1
Output:
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):
print ("%d * %d = %d" % ( g, k, g*k))
Output:
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10
LOOP CONTROLS
LOOP
CONTROL

• 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 STATEMENT
Syntax:
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
PASS STATEMENT
Syntax:
pass
Example:
for letter in ‘Python’:
if letter == ‘y’:
pass
print ('Pass block’)
print ('Current letter is:', letter)
Output:
Current letter is : P
Pass block
Current letter is : y
Current letter is : t
Current letter is : h
Current letter is : o
Current letter is : n
CLASS TASKS::

• Write a marksheet and display marks obtained, percentage and grade


• Write a program for table
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: 20/4/2018

You might also like