Python 2,3 Expt SBM
Python 2,3 Expt SBM
EXPERIMENT NO.02
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.
-A Python variable name must start with a letter or the underscore character.
-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.
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'
Python Arithmetic Operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.
Addition Operator
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
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.
Example:
Output
Jawahar Education Society's
A.C. Patil College of Engineering Kharghar
Department: Electrical Engineering
And so on…
Python Lists
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.
Example:
Tuple
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.
Tuple Items
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
Example
Python Dictionaries
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:
Dictionary Items
Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
Example
Example
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