0% found this document useful (0 votes)
7 views9 pages

P2S2 LabExperiment1

The document outlines the basics of Python programming, focusing on two modes of development: Interactive Mode and Script Mode. It details how to write and execute Python scripts in various text editors and IDEs, and introduces different types of operators used in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and ternary operators. Each operator type is explained with examples to illustrate their functionality.

Uploaded by

r.udaykiran93475
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)
7 views9 pages

P2S2 LabExperiment1

The document outlines the basics of Python programming, focusing on two modes of development: Interactive Mode and Script Mode. It details how to write and execute Python scripts in various text editors and IDEs, and introduces different types of operators used in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and ternary operators. Each operator type is explained with examples to illustrate their functionality.

Uploaded by

r.udaykiran93475
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/ 9

A.

Y 2024-25 PUC-II SEMESTER –II


Python Programming Lab
experiments
Experiment:1

1a). Demonstrate about Basics of Python Programming.

Learning objectives:
We will see the different ways to develop a python program/application are

Modes of Python Program:


We can develop a python program in 2 different styles.

Interactive Mode or Immediate Mode


Script Mode.

1. Interactive Mode:
Python comes with an interactive interpreter. Typically the interactive mode is
used to test the features of the python, or to run a smaller script that may not be reusable.

Example for Interactive Mode:


Open the command prompt, and go to the location which your python has been installed and hit
the python command. When you type python in your shell or command prompt, the python
interpreter becomes active with a>>> (REPL) and waits for your commands.

 Now you can type any valid python expression at the prompt.
 Python reads the typed expression, evaluates it and prints the result
2. Running Python scripts in Command Prompt:
We can write a group of python statements in any one of the following editors or IDEs

Editors:

 Notepad
 Notepad++
 editPlus
 nano
 gedit
 IDLE
 Etc..

IDEs :

 Pycharm
 IDLE
 Eric
 Eclipse
 Netbeans Etc..

Steps to write python program in script mode:


1. Open your text editor, type the following text and save it as script.py
1. print "welcome to python"
2. In the command prompt, and run this program by calling python script.py.
3. Make sure you change to the directory where you saved the file before
doing it.
Python program using IDLE shell:

1b) Demonstration of various operators used in Python with examples?

Operators are the constructs which can manipulate the value of operands.

Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.

Types of Operator
Python language supports the following types of operators.

1.Arithmetic Operators
2.Comparison Relational Operators
3.Assignment Operators
4.Logical Operators
5.Bitwise Operators
6.Membership Operators
7.Identity Operators

Let us have a look on all operators one by one.


1. Arithmetic calculations.
Operators:
Arithmetic operators used to perform all arithmetic

2. Comparison Relational Operators:


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

Output:

a is not greater than b

3. Assignment Operators:
We can use assignment operator to assign value to the variable
4. 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
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Example:
print(4&5) ==>4
print(4|5) ==>5
print(4^5) ==>1
5. Logical Operators:
Following are the various logical operators used in Python.
 And
 Or
 not
You can apply these operators for boolean types and non-boolean types, but the behavior
is different.
For boolean types behaviour:
and ==>If both arguments are True then only result is
True or ====>If atleast one argument is True then result is
True not ==> complement
True and False
==>False True or False
===>True not False
==>True

For non-boolean types behaviour:


0 means False
non-zero means True
empty string is always treated as False

Eg:

Output:

6. 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
7. Ternary Operator (or) Conditional Operator:

1. If the operator operates on only one operand, we will call such operator as
unary operator. For eg:, ~a.

2. If the operator operates on Two operands, we will call such operator as


binary operator. For eg:, a + b.

3. If the operator operates on Three operands, we will call such operator as Ternary
operator.

Syntax:
x = firstValue if condition else secondValue

If condition is True then firstValue will be considered else secondValue will be considered

You might also like