0% found this document useful (0 votes)
9 views34 pages

CSE1021- Problem Solving and Programming -unit 2

Uploaded by

Tanmay Bhujade
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
9 views34 pages

CSE1021- Problem Solving and Programming -unit 2

Uploaded by

Tanmay Bhujade
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 34

CSE1021- Problem Solving

and Programming
DATA, EXPRESSIONS, STATEMENTS
Variables
A variable allows us to store a value by assigning it to a name,
which can be used later.

Named memory locations to store values.

Programmers generally choose names for their variables that are


meaningful.

It can be of any length. No space is allowed.

We don't need to declare a variable before using it. In Python,


we simply assign a value to a variable and it will exist.
Variables

A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).

Rules for Python variables:

A variable name must start with a letter or the underscore


character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are
three different variables)
Assigning value to variable:

Value should be given on the right side of assignment operator(=) and variable on left side.

>>>counter =45
print(counter)

Assigning a single value to several variables simultaneously:

>>> a=b=c=100

Assigning multiple values to multiple variables:

>>> a,b,c=2,4,"ram"
KEYWORDS:

• Keywords are the reserved words in Python.

• We cannot use a keyword as variable name, function name or any


other identifier.
• They are used to define the syntax and structure of the Python
language.

• Keywords are case sensitive.


KEYWORDS:
IDENTIFIERS:
• Identifier is the name given to entities like class, functions, variables etc. in Python.

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A


to Z) or digits (0 to 9) or an underscore (_).

2. All are valid example.

3. An identifier cannot start with a digit.

4. Keywords cannot be used as identifiers

5. Cannot use special symbols like !, @, #, $, % etc. in our identifier.

6. Identifier can be of any length.


IDENTIFIERS:

• Names like myClass, var_1, and this_is_a_long_variable


STATEMENTS AND EXPRESSIONS:
Statements:
Instructions that a Python interpreter can
executes are called statements.

A statement is a unit of code like creating


a variable or displaying a value.

> n = 17
> print(n)

Here, The first line is an assignment statement that gives a value to n.


The second line is a print statement that displays the value of n.
INPUT

Input is data entered by user (end user)


in the program.

In python, input () function is available for


input.

#python accepts string as default data


type. conversion is required for type.
OUTPUT

Output can be displayed to the user


using Print statement .

Syntax:
print (expression/constant/variable)
COMMENTS:

A hash sign (#) is the beginning of a comment.

Anything written after # in a line is ignored by


interpreter.

Python does not have multiple-line commenting


feature. You have to comment each line
individually as follows :

Eg: # This is a comment. Eg: percentage = (minute * 100) / 60


# This is a comment, too. # calculating percentage of an hour
# I said that already.
DOCSTRING

Docstring is short for documentation string.


Syntax:
functionname__doc.__

It is a string that occurs as the first statement in


a module, function, class, or method definition.
We must write what a function/class does in the
docstring.

Triple quotes are used while writing docstrings.


LINES AND INDENTATION
Most of the programming languages like C, C++, Java use braces { } to define a
block of code. But, python uses indentation.

Blocks of code are denoted by line indentation.

It is a space given to the block of codes for class and function definitions or
flow control.
QUOTATION IN PYTHON
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals.
Anything that is represented using quotations are considered as string.
TUPLE ASSIGNMENT

• An assignment to all of the elements in a tuple using a single assignment statement.

• Python has a very powerful tuple assignment feature that allows a tuple of variables on
the left of an assignment to be assigned values from a tuple on the right of the
assignment.

• The left side is a tuple of variables; the right side is a tuple of values.

• Each value is assigned to its respective variable.


• All the expressions on the right side are evaluated before any of the assignments. This
feature makes tuple assignment quite versatile.
• Naturally, the number of variables on the left and the number of values on the right have
to be the same.
TUPLE ASSIGNMENT

Example:
• It is useful to swap the values of two variables. With conventional assignment statements,
we have to use a temporary variable. For example, to swap a and b:
TUPLE ASSIGNMENT
• Tuple assignment solves this problem neatly:

(a, b) = (b, a)

• One way to think of tuple assignment is as tuple packing/unpacking.


• In tuple packing, the values on the left are ‘packed’ together in a tuple:
>>> b = ("George", 25, "20000") # tuple

• In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into
the variables/names on the right:
TUPLE ASSIGNMENT
• The right side can be any kind of sequence (string,list,tuple)
OPERATORS
• 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 Operators:
-Python language supports the following types of operators

Arithmetic Operators

Comparison (Relational) Operators

Assignment Operators

Logical Operators

Bitwise Operators

Membership Operators

Identity Operators
Arithmetic operators
• They are used to perform mathematical operations like addition,
subtraction, multiplication etc. Assume, a=10 and b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.

• It either returns True or False according to the condition. Assume, a=10 and
b=5
Comparison (Relational) Operators:
• Comparison operators are used to compare values.

• It either returns True or False according to the condition. Assume, a=10 and
b=5
Assignment Operators
• Assignment operators are used in Python to assign values to variables.
Assignment Operators
Assignment Operators
Logical Operators
• Logical operators are the and, or, not operators.
Bitwise Operators:
• A bitwise operation operates on one or more bit patterns at the level of individual bits

Example: Let x = 10 (0000 1010 in binary) and y = 4


(0000 0100 in binary)
Bitwise Operators:
• A bitwise operation operates on one or more bit patterns at the level of individual bits
Membership Operators
• Evaluates to find a value or a variable is in the specified sequence of string, list, tuple,
dictionary or not.

• Let, x=[5,3,6,4,1]. To check particular item in list or not, in and not in operators are used.
Identity Operators
• They are used to check if two values (or variables) are located on the same part of the
memory

You might also like