Lab 1
Lab 1
Faculty of informatics
LAB MANUAL
1|Page
Lab 1
Topic - Introduction to Python Programming.
• 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.
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
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. Like Perl, Python source code is now available under the GNU General
Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido van
Rossum still holds a vital role in directing its progress.
2|Page
Python Features
• Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax.
This allows the student to pick up the language quickly.
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows interactive
testing and debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable − You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
• GUI Programming − Python supports GUI applications that can be created and ported to
many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and
the X Window system of Unix.
• Scalable − Python provides a better structure and support for large programs than shell
scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are listed
below −
• It can be used as a scripting language or can be compiled to byte-code for building large
applications.
3|Page
• 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, ActiveX, CORBA, and Java.
Python Variables: Declare, Concatenate, Global & Local
What is a Variable in Python?
A Python variable is a reserved memory location to store values. In other words, a variable in a
python program gives data to the computer for processing.
Every value in Python has a datatype.
Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. this are the main
standard data types we have in python. Variables can be declared by any name or even alphabets like
a, aa, abc, etc.
How to Declare and use a Variable
Let see an example. We will declare variable "a" and print it.
a=100
print (a)
4|Page
Python Example 1
x = 123 # integer
x = "hello" # string
x = [0,1,2] # list
x = (0,1,2) # tuple
Constants
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants
as containers that hold information which cannot be changed later.
Non technically, you can think of constant as a bag to store some books and those books cannot be
replaced once place inside the bag.
Assigning value to a constant in Python
In Python, constants are usually declared and assigned on a module. Here, the module means a
new file containing variables, functions etc which is imported to main file. Inside the module,
constants are written in all capital letters and underscores separating the words.
5|Page
Declaring and assigning value to a constant
Create a constant.py
1. PI = 3.14
2. GRAVITY =
9.8
Create a main.py
1. import constant
2. print(constant.PI)
3. print(constant.GRAVI
TY)
3.14
9.8
Types of Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Let us have a look on all operators one by one.
Python Arithmetic Operators
6|Page
Operator Description Example
//
Floor Division - The division of 9//2 = 4
operands where the result is the quotient
in which the digits after the decimal
point are removed.
These operators compare the values on either sides of them and decide the relation among them.
They are also called Relational operators.
7|Page
Operator Description Example
<> If values of two operands are not equal, then condition (a <> b) is true.
becomes true. Thisis similar to
!= operator.
> If the value of left operand is greater than the value of (a > b) isnot true.
rightoperand, then condition becomes true.
< If the value of left operand is less than the value of right (a < b) is
operand, then condition becomes true. true.
>= If the value of left operand is greater than or equal to the (a >= b) is
value of right operand, then condition becomes true. not true.
<= If the value of left operand is less than or equal to the (a <= b) is
value of right operand, then condition becomes true. true.
/= Divide AND It divides left operand with the right operand and c /= a is equivalent
assign the result to left operand to c = c / ac /= a is
equivalent to c = c /
a
//= Floor It performs floor division on operators and assign value c //= a is equivalent
Division tothe left operand to c = c // a
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13;
Now in binary format they will be as follows − a = 0011 1100 b = 0000 1101
& Binary AND Operator copies a bit to the result if it (a & b) (means 0000 1100)
exists in both operands
^ Binary XOR It copies the bit if it is set in one operand (a ^ b) = 49(means 0011 0001)
but not both.
Python Logical Operators
There are following logical operators supported by Python language. Assume variable a holds
10 and variable b holds 20 then
and If both the operands are true then condition becomes true. (a and
Logical b) is
AND true.
Python’s membership operators test for membership in a sequence, such as strings, lists, or
tuples. There are two membership operators as explained below −
10 | P a g e
Operator Description Example
not in Evaluates to true if it does not finds a variable in x not in y, here not in
the specified sequence and false otherwise. results in
a 1 if x is not a member
of sequencey.
Python Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity
operators explained below −
Is Evaluates to true if the variables on either side of the operator x is y, here is results in
point to the same object and false otherwise. 1 if id(x) equals id(y).
11 | P a g e
Python Operators Precedence
The following table lists all operators from highest precedence to lowest.
12 | P a g e
<= < > >=
8
Comparison operators
<> == !=
9
Equality operators
= %= /= //= -= += *= **=
10
Assignment operators
Is, is not
11
Identity operators
In, not in
12
Membership operators
not, or, and
13
Logical operators
Exercise:
1. Write python code that will add two numbers?
2. Declare variables and display data types of respective variables?
3. Demonstrate type casting in python
4. Demonstrate Logical operators
13 | P a g e