Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lab 1

The document provides information about a lab manual for fundamentals of Python programming from St. Mary's University. It includes an introduction to Python describing it as an interpreted, interactive, and object-oriented scripting language. It then covers Python features, variables, operators, and constants.

Uploaded by

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

Lab 1

The document provides information about a lab manual for fundamentals of Python programming from St. Mary's University. It includes an introduction to Python describing it as an interpreted, interactive, and object-oriented scripting language. It then covers Python features, variables, operators, and constants.

Uploaded by

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

St, Mary`s University

Faculty of informatics

Department of computer science

LAB MANUAL

FUNDAMENTALS OF PYTHON PROGRAMMING

1|Page
Lab 1
Topic - Introduction to Python Programming.

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, and it has fewer syntactical constructions than other languages.

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

You should have a basic understanding of Computer Programming terminologies. A basic


understanding of any of the programming languages is a plus.

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

Python's features include −

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

• Easy-to-maintain − Python's source code is fairly easy-to-maintain.

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

• Databases − Python provides interfaces to all major commercial databases.

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

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

# Declare a variable and initialize it


f=0
print(f)
# re-declaring the variable works f =
'guru99'
print(f)

List of some different variable types

x = 123 # integer

x = 123L # long integer


x = 3.14 # double float

x = "hello" # string

x = [0,1,2] # list

x = (0,1,2) # tuple

x = open(‘hello.py’, ‘r’) # file

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)

When you run the program, the output will be:

3.14
9.8
Types of Operators

Python language supports the following types of operators.


• Arithmetic Operators

• Comparison (Relational) 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

Assume variable a holds 10 and variable b holds 20, then −

6|Page
Operator Description Example

+ Addition Adds values on either side of the a + b = 30


operator.

- Subtraction Subtracts right hand operand from left a – b = 10


hand operand.

* Multiplication Multiplies values on either side of the a * b = 200


operator

/ Division Divides left hand operand by right hand b/a=2


operand

% Modulus Divides left hand operand by right hand b%a=0


operand and returns reminder

** Exponent Performs exponential (power) calculation a**b =10 to the


on operators power 20

//
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.

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.

Assume variable a holds 10 and variable b holds 20, then −

7|Page
Operator Description Example

== If the values of two operands are equal, then the (a == b) is not


condition becomes true. true.

!= If values of two operands are not equal, then condition (a != b) is true.


becomes true.

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

Python Assignment Operators

Assume variable a holds 10 and variable b holds 20, then −

Operator Description Example


8|Page
= Assigns values from right side operands to left side c = a + b assigns
operand value of a
+ b into c
*= It multiplies right operand with the left operand and
Multiply assign the result to left operand c *= a is equivalent
AND to c = c * a

/= 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

%= It takes modulus using two operands and assign the c %= a is equivalent


Modulus result toleft operand to c = c % a
AND

**= Performs exponential (power) calculation on operators c **= a is


Exponent andassign value to the left operand equivalent to c = c
AND ** a

//= Floor It performs floor division on operators and assign value c //= a is equivalent
Division tothe left operand to c = c // a

Python Bitwise Operators

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

There are following Bitwise operators supported by Python language [

Operator Description Example

& Binary AND Operator copies a bit to the result if it (a & b) (means 0000 1100)
exists in both operands

| Binary OR It copies a bit if it exists in either operand. (a | b) = 61(means 0011 1101)


9|Page

^ 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

Operator Descriptio Example


n

and If both the operands are true then condition becomes true. (a and
Logical b) is
AND true.

or Logical OR If any of the two operands are non-zero then condition (a or b)


becomes true. is true.

not Used to reverse the logical state of its operand. Not(a


Logical and b) is
NOT false.

Used to reverse the logical state of its operand.

Python Membership Operators

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

In Evaluates to true if it finds a variable in the


specified sequence and false otherwise. x in y,
here in results in a 1
if x is a member of
sequencey.

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 −

Ope Description Example


rato
r

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).

is Evaluates to false if the variables on either side of the


x is not y, here is not
not operator point to the same object and true otherwise.
results in 1 if id(x) is

not equal to id(y).

11 | P a g e
Python Operators Precedence

The following table lists all operators from highest precedence to lowest.

Sr.No. Operator & Description


**
1 Exponentiation (raise to the power)
~+-
2
Complement, unary plus and minus (method names for the last two are +@ and -
@)
* / % //
3
Multiply, divide, modulo and floor division
+-
4 Addition and subtraction
>> <<
5
Right and left bitwise shift
&
6
Bitwise 'AND'
^|
7
Bitwise exclusive `OR' and regular `OR'

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

You might also like