0% found this document useful (0 votes)
2 views

Python 2,3 Expt SBM

The document outlines various programming concepts in Python, including expressions, variables, data types, and basic operations. It explains different types of expressions such as arithmetic, relational, and logical expressions, as well as the rules for naming variables and the types of data they can hold. Additionally, it covers string manipulation, lists, tuples, dictionaries, and arrays, providing examples and descriptions of their functionalities.

Uploaded by

aimstudy565
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)
2 views

Python 2,3 Expt SBM

The document outlines various programming concepts in Python, including expressions, variables, data types, and basic operations. It explains different types of expressions such as arithmetic, relational, and logical expressions, as well as the rules for naming variables and the types of data they can hold. Additionally, it covers string manipulation, lists, tuples, dictionaries, and arrays, providing examples and descriptions of their functionalities.

Uploaded by

aimstudy565
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/ 16

Jawahar Education Society's

A.C. Patil College of Engineering Kharghar


Department: Electrical Engineering

EXPERIMENT NO.02

AIM: WRITE A PROGRAM TO UNDERSTAND EXPRESSIONS, VARIABLES, QUOTES, MATH OPERATIONS.

Expressions in Python

An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an
expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides
which operation will be performed first. We have many different types of expressions in Python. Let’s discuss all types along with some exemplar
codes :

Constant Expressions: These are the expressions that have constant values only

Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and sometimes parenthesis. The result of this
type of expression is also a numeric value. The operators used in these expressions are arithmetic operators like addition, subtraction, etc Here are
some arithmetic operators in Python
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Integral Expressions: These are the kind of expressions that produce only integer results after all computations and type conversions

Floating Expressions: These are the kind of expressions which produce floating point numbers as result after all computations and type
conversions.

Relational Expressions: In these types of expressions, arithmetic expressions are written on both sides of relational operator (> , < , >= , <=).
Those arithmetic expressions are evaluated first, and then compared as per relational operator and produce a boolean output in the end. These
expressions are also called Boolean expressions.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Logical Expressions: These are kinds of expressions that result in either True or False. It basically specifies one or more conditions. For example,
(10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so it will return False. Studying logical expressions, we also come across
some logical operators which can be seen in logical expressions most often. Here are some logical operators in Python:

Example:

Bitwise Expressions: These are the kind of expressions in which computations are performed at bit level

Combinational Expressions: We can also use different types of expressions in a single expression, and that will be termed as combinational
expressions.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Python Variables

Python Variable is containers that store values. Python is not “statically typed”. We do not need to declare variables before using them or declare
their type. A variable is created the moment we first assign a value to it. A Python variable is a name given to a memory location. It is the basic
unit of storage in a program. In this article, we will see how to define a variable in Python.

Example of Variable in Python


An Example of a Variable in Python is a representational name that serves as a pointer to an object. Once an object is assigned to a variable, it
can be referred to by that name. In layman’s terms, we can say that Variable in Python is containers that store values.
Here we have stored “Electrical” in a variable var, and when we call its name the stored information will get printed.

Rules for Python variables

-A Python variable name must start with a letter or the underscore character.

-A Python variable name cannot start with a number.

-A Python variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

-Variable in Python names are case-sensitive (name, Name, and NAME are three different variables).

-The reserved words(keywords) in Python cannot be used to name the variable in Python.

Variable Types in Python

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a
particular data. Since everything is an object in Python programming, data types are actually classes and variables are instances (object) of these
classes.
Built-in Python Data types are:
-Numeric
-Text Type
-Sequence Type (Python list, Python tuple, Python range)
-Boolean
-Set
-Dictionary

Python Quotes
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the
string.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

The triple quotes are used to span the string across multiple lines. For example, all the following are legal −

word = 'Word'

sentence = "This is a sentence."

paragraph = """This is a paragraph. It is

made up of multiple lines and sentences."""

Python Arithmetic Operators

Python Arithmetic Operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

Arithmetic Operators in Python


There are 7 arithmetic operators in Python. The lists are given below:
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Precedence of Arithmetic Operators in Python

Python Arithmetic Operators Examples

Addition Operator

In Python, + is the addition operator. It is used to add 2 values.

Subtraction Operator

In Python, – is the subtraction operator. It is used to subtract the second value from the first value.

Multiplication Operator
In Python, * is the multiplication operator. It is used to find the product of 2 values.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Modulus Operator

In Python, % is the modulus operator. It is used to find the remainder when the first operand is divided by the second.

Exponentiation Operator

In Python, ** is the exponentiation operator. It is used to raise the first operand to the power of the second.
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

EXPERIMENT NO.03

AIM : WRITE A PYTHON PROGRAM TO UNDERSTAND STRINGS: BASIC STRING OPERATIONS AND STRING

METHODS, LIST, TUPELS, DICTIONARIES AND ARRAYS

A String is a data structure in Python that represents a sequence of characters. It is an immutable data type, meaning that once you have created a string,
you cannot change it. Strings are used widely in many different applications, such as storing and manipulating text data, representing names, addresses,
and other types of data that can be represented as text.

What is a String in Python?


Python does not have a character data type, a single character is simply a string with a length of 1.

Example:

Python String Methods


Python string methods is a collection of in-built Python functions that operates on lists.

Case Changing of Strings


The below Python functions are used to change the case of the strings. Let’s look at some Python string methods with examples:
 lower(): Converts all uppercase characters in a string into lowercase
 upper(): Converts all lowercase characters in a string into uppercase
 title(): Convert string to title case
 swapcase(): Swap the cases of all characters in a string
 capitalize(): Convert the first character of a string to uppercase

Output
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

List of Python String Methods


Here is the list of some in-built Python string methods, that you can use to perform actions on string:

Function Name Description

Converts the first character of the string to a


capitalize()
capital (uppercase) letter

casefold() Implements caseless string matching

center() Pad the string with the specified character.

Returns the number of occurrences of a substring


count()
in the string.

Encodes strings with the specified encoded


encode()
scheme

Returns the lowest index of the substring if it is


find()
found

format() Formats the string for printing it to console

Formats specified values in a string using a


format_map() dictionary

Returns the position of the first occurrence of a


index()
substring in a string

Checks whether all the characters in a given string


isalnum()
is alphanumeric or not

And so on…

Python Lists

Lists are used to store multiple items in a single variable.


Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). In simple language, a
list is a collection of things, enclosed in [ ] and separated by commas.

Lists are created using square brackets:

Creating a List in Python


Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for
its creation of a list.
Note: Unlike Sets, the list may contain mutable elements.

Example:

Tuple

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different
qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

Tuple Items

Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has index [1] etc.

Ordered
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.

Unchangeable

Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.

Allow Duplicates

Since tuples are indexed, they can have items with the same value

type()

From Python's perspective, tuples are defined as objects with the data type 'tuple' :Tuple Items - Data Types

Tuple items can be of any data type:

Example

String, int and boolean data types:

Python Dictionaries

Dictionaries are used to store data values in key: value pairs.


Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Dictionaries are written with curly brackets, and have keys and values:

Example:

Create and print a dictionary:

Dictionary Items

Dictionary items are ordered, changeable, and do not allow duplicates.

Dictionary items are presented in key:value pairs, and can be referred to by using the key name.

Example

Print the "brand" value of the dictionary:

Example

Duplicate values will overwrite existing values:


Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

Python Array

For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can
identify the location of any of your friends by simply knowing the count of the steps they are on. The array can be handled in Python by a
module named array. They can be useful when we have to manipulate only specific data type values. A user can treat lists as arrays. However,
the user cannot constrain the type of elements stored in a list. If you create arrays using the array module, all elements of the array must be of
the same type.
What is an Array in Python?
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This
makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first
element of the array (generally denoted by the name of the array).

C
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering

You might also like