0% found this document useful (0 votes)
5 views64 pages

Mod 1 PT 2 Python

The document outlines the features and functionalities of Python programming, highlighting its simplicity, versatility, and dynamic nature. It covers essential concepts such as Python IDLE, literal constants, variables, data types, and operators, providing insights into how to write and execute Python code. Additionally, it discusses comments, reserved words, indentation, type conversion, and various operators used in Python programming.

Uploaded by

nisargap.1515
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)
5 views64 pages

Mod 1 PT 2 Python

The document outlines the features and functionalities of Python programming, highlighting its simplicity, versatility, and dynamic nature. It covers essential concepts such as Python IDLE, literal constants, variables, data types, and operators, providing insights into how to write and execute Python code. Additionally, it discusses comments, reserved words, indentation, type conversion, and various operators used in Python programming.

Uploaded by

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

PYTHON PROGRAMMING

PYTHON PROGRAMMING 1 / 64
FEATURES OF PYTHON

Simple
Easy to Learn
Versatile: Support both large and small projects.
Free and Open Source
High-level Language
Interactive : interactive testing and debugging of pieces of code.
Portable: run on multiple platforms without any change.
Object-Oriented
Interpreted: No need to compile - executed line by line by the
interpreter.
Dynamic: errors are detected at run-time.

PYTHON PROGRAMMING 2 / 64
FEATURES OF PYTHON

Extensible: can extend functionality for better performance.


Embeddable: can be embedded into C, C++, or Java applications.
Extensive Libraries
Easy Maintenance
Secure
Robust: No direct memory access; errors handled using exceptions.
Multi-threaded: executes multiple processes simultaneously.
Garbage Collection: automatically cleans unused data from memory.

PYTHON PROGRAMMING 3 / 64
Python IDLE

PYTHON PROGRAMMING 4 / 64
Python IDLE

IDLE : Integrated Development and Learning Environment


Python IDLE is a simple and user-friendly tool that comes bundled
with Python installations.
It allows you to write, edit, and execute Python code efficiently.
Python IDLE works on different platforms in almost the same way.
Writing Python Programs
Step 1: Open an editor.
Step 2: Write the instructions.
Step 3: Save it as a file with the filename having the extension . py.
Step 4: Run the interpreter with the command python
program name. py or use IDLE to run the programs.

PYTHON PROGRAMMING 5 / 64
LITERAL CONSTANTS
Fixed values in the program that cannot change.
Examples: 7, 3.9, ’A’, ”Hello”

PYTHON PROGRAMMING 6 / 64
LITERAL CONSTANTS

[Link]
Integer: Whole numbers like 5, -10
Floating Point: Numbers with decimals
e.g., 3.23, 91.5E-2 (E means ×10−2 )
Complex Numbers: Numbers with a real and imaginary part
e.g., -3 + 7 j
imaginary part of a complex number is written using j, not i.
Commas are not allowed in numeric literals (e.g., 3,567 ).

PYTHON PROGRAMMING 7 / 64
LITERAL CONSTANTS
format() Function

PYTHON PROGRAMMING 8 / 64
LITERAL CONSTANTS
Simple Operations on Numbers

PYTHON PROGRAMMING 9 / 64
LITERAL CONSTANTS

Division By Zero
Dividing a number by zero in Python generates error and no output is
produced.

PYTHON PROGRAMMING 10 / 64
LITERAL CONSTANTS
Quotient and Remainder
Quotient-//
Remainder-% (modulo operator)

PYTHON PROGRAMMING 11 / 64
LITERAL CONSTANTS

Exponentiation

PYTHON PROGRAMMING 12 / 64
LITERAL CONSTANTS

[Link]
A string is a group of characters.
Using single quotes:’HELLO’
Using double quotes:”HELLO”
Using triple quotes: to specify multi-line [Link] use as many
single and souble quotes within triple quotes.
’ ’ ’Good morning everyone.
”Welcome to the world of ’Python’.”
Happy Reading.’ ’ ’

PYTHON PROGRAMMING 13 / 64
LITERAL CONSTANTS

[Link]

PYTHON PROGRAMMING 14 / 64
LITERAL CONSTANTS
String Concatenation

There is no char datatype in Python.

PYTHON PROGRAMMING 15 / 64
LITERAL CONSTANTS

Unicode Strings
Unicode is a standard way of writing international text.
Can be specified by prefixing the string with a u or U.

PYTHON PROGRAMMING 16 / 64
LITERAL CONSTANTS
Escape Sequences
An escape sequence is a special combination of characters that starts
with a backslash and is used to represent something that can’t be
typed directly in a string.

PYTHON PROGRAMMING 17 / 64
LITERAL CONSTANTS

Escape Sequences

PYTHON PROGRAMMING 18 / 64
LITERAL CONSTANTS
Raw Strings
If you want to specify a string that should not handle any escape
sequences and want to display exactly as specified then you need to
specify that string as a raw string . A raw string is specified by prefixing r
or R to the string.

PYTHON PROGRAMMING 19 / 64
LITERAL CONSTANTS
String Formatting
format(value,format specifier)

PYTHON PROGRAMMING 20 / 64
VARIABLES AND IDENTIFIERS

Variable means its value can vary.


You can store any piece of information in a variable.
Variables are nothing but just parts of your computer’s memory where
information is stored.
To be identified easily, each variable is given an appropriate name.
Identifiers are names given to identify something. This something can
be a variable, function, class, module or other object.

PYTHON PROGRAMMING 21 / 64
VARIABLES AND IDENTIFIERS

For naming any identifier, there are some basic rules like:
The first character of an identifier must be an underscore (’ ’) or a
letter (upper or lowercase). The rest of the identifier name can be
underscores (’ ’), letters (upper or lowercase), or digits (0-9).
Identifier names are case-sensitive. For example, myvar and myVar
are not the same.
Punctuation characters such as @, $, and % are not allowed within
identifiers.
Examples of valid identifier names are sum, my var, num1, r, var 20,
First, etc.
Examples of invalid identifier names are 1num, my-var, %check, Basic Sal,
H#R&A, etc.

PYTHON PROGRAMMING 22 / 64
DATA TYPES

Data types are like labels that tell Python what type of data is
stored-a number, text etc.
Assigning or Initializing Values to Variables
In Python, programmers need not explicitly declare variables to
reserve memory space.
The declaration is done automatically when a value is assigned to the
variable using the equal sign (=).
The operand on the left side of equal sign is the name of the variable
and the operand on its right side is the value to be stored in that
variable.

PYTHON PROGRAMMING 23 / 64
DATA TYPES
Program to display data of different types using variables and literal
constants.

PYTHON PROGRAMMING 24 / 64
DATA TYPES

Output:

PYTHON PROGRAMMING 25 / 64
DATA TYPES

PYTHON PROGRAMMING 26 / 64
DATA TYPES

Multiple Assignment
Python allows programmers to assign a single value to more than one
variables simultaneously.
You can also assign different values to multiple variables
simultaneously as shown below.
Trying to reference a variable that has not been assigned any value
causes an error.
Removing a variable means that the reference from the name to the
value has been deleted.
Deleted variables can be used again in the code if and only if you
reassign them some value.

PYTHON PROGRAMMING 27 / 64
DATA TYPES
Multiple Assignment

PYTHON PROGRAMMING 28 / 64
DATA TYPES

PYTHON PROGRAMMING 29 / 64
DATA TYPES
Boolean
Boolean is another data type in Python.
A variable of Boolean type can have one of the two values- True or
False.
Similar to other variables, the Boolean variables are also created while
we assign a value to them or when we use a relational operator on
them.

PYTHON PROGRAMMING 30 / 64
INPUT OPERATION
To take input from the users, Python makes use of the input()
function.
The input() function prompts the user to provide some information
on which the program can work and give the result.
However, we must always remember that the input function takes
user’s input as a string.

PYTHON PROGRAMMING 31 / 64
COMMENTS
Comments are the non-executable statements in a program.
They are just added to describe the statements in the program code.
Comments make the program easily readable and understandable by
the programmer as well as other users who are seeing the code.
The interpreter simply ignores the comments.
In Python, a hash sign (#) that is not inside a string literal begins a
comment.
All characters following the # and up to the end of the line are part
of the comment

PYTHON PROGRAMMING 32 / 64
RESERVED WORDS

In every programming language there are certain words which have a


pre-defined meaning.
These words which are also known as reserved words or keywords
cannot be used for naming identifiers.
All the Python keywords contain lowercase letters only.

PYTHON PROGRAMMING 33 / 64
INDENTATION
Whitespace at the beginning of the line is called indentation.
These whitespaces or the indentation are very important in Python.
In a Python program, the leading whitespace including spaces and
tabs at the beginning of the logical line determines the indentation
level of that logical line.

All statements inside a block should be at the same indentation level.


PYTHON PROGRAMMING 34 / 64
TYPE CONVERSION
In Python, it is just not possible to complete certain operations that
involves different types of data.
For example, it is not possible to perform ”2” + 4 since one operand
is an integer and the other is of string type.

PYTHON PROGRAMMING 35 / 64
TYPE CONVERSION

PYTHON PROGRAMMING 36 / 64
TYPE CONVERSION

PYTHON PROGRAMMING 37 / 64
TYPE CONVERSION

PYTHON PROGRAMMING 38 / 64
TYPE CONVERSION
Type casting vs Type coercion
Type casting : explicit conversion of a value from one data type to
another. The programmer manually change a variable’s data type.
You tell Python exactly how to convert it.
x = ”10” # string
y = int(x) # convert string to integer
print(y + 5) # Output: 15
Type coercion: implicit conversion of data types either during
compilation or during run-time. Python automatically converts one
data type to another when needed in an expression.
x = 5 # integer
y = 2.0 # float
z = x + y # Python converts int to float automatically
print(z) # Output: 7.0

PYTHON PROGRAMMING 39 / 64
OPERATORS AND EXPRESSIONS

Operators are the constructs that are used to manipulate the value of
operands.
Some basic operators include +, -, *, and /. In an expression, an
operator is used on operand(s) (values to be manipulated).
For example, in the expression sum = a + b, a and b are operands
and + is the operator.
Python supports different types of operators which are as follows:
Arithmetic Operators
Unary Operators
Comparison (Relational) Operators
Bitwise Operators
Assignment Operators
Membership Operators
Logical Operators
Identity Operators

PYTHON PROGRAMMING 40 / 64
Arithmetic Operators
If a=100 and b=200 then:

PYTHON PROGRAMMING 41 / 64
Comparison Operators
Comparison operators also known as relational operators are used to
compare the values on its either sides and determines the relation between
them.
If a=100 and b=200 then:

PYTHON PROGRAMMING 42 / 64
Assignment and In-place or Shortcut Operators

Assignment operator as the name suggests assigns value to the


operand.
The in-place operators also known as shortcut operators that
includes +=, -= , *= , /=, %=, //= and **= allow you to write
code like num = num + 10 more concisely, as num += 3.

PYTHON PROGRAMMING 43 / 64
Assignment and In-place or Shortcut Operators

PYTHON PROGRAMMING 44 / 64
Assignment and In-place or Shortcut Operators

PYTHON PROGRAMMING 45 / 64
Unary Operators
Unary operators act on single operands.
Python supports unary minus operator.
Unary minus operator is strikingly different from the arithmetic
operator that operates on two operands and subtracts the second
operand from the first operand.
When an operand is preceded by a minus sign, the unary operator
negates its value.
For example, if a number is positive, it becomes negative when
preceded with a unary minus operator.
Similarly, if the number is negative, it becomes positive after applying
the unary minus operator.
Consider the given example:
b = 10
a = -(b)
The result of this expression, is a = -10, because variable b has a
positive value.
PYTHON PROGRAMMING 46 / 64
Bitwise Operators

Bitwise operators perform operations at the bit level.


These operators include bitwise AND, bitwise OR, bitwise XOR,
bitwise NOT and shift operators.
Bitwise operators expect their operands to be of integers and treat
them as a sequence of bits.
The truth tables of these bitwise operators are given below.

PYTHON PROGRAMMING 47 / 64
Shift Operators

Python supports two bitwise shift operators.


They are shift left (<<) and shift right (>>). These operations are
used to shift bits to the left or to the right.
The syntax for a shift operation can be given as follows:
operand op num
Example 1:
a = 5 = 0000 0101 (Binary)
a <<1 = 0000 1010 = 10
a <<2 = 0001 0100 = 20
Example 2:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

PYTHON PROGRAMMING 48 / 64
Bitwise and Shift Operators

PYTHON PROGRAMMING 49 / 64
Logical Operators

Logical AND (&&) operator is used to simultaneously evaluate two


conditions or expressions with relational operators.
If expressions on both the sides (left and right side) of the logical
operator are true, then the whole expression is true.
For example, If we have an expression (a>b) && (b>c), then the
whole expression is true only if both expressions are true. That is, if a
is greater than b and bis greater than c.
Logical OR (||) operator is used to simultaneously evaluate two
conditions or expressions with relational operators.
If one or both the expressions of the logical operator is true, then the
whole expression is true.
For example, If we have an expression (a>b) || (b>c), then the whole
expression is true if either a is greater than b or b is greater than c.

PYTHON PROGRAMMING 50 / 64
Logical Operators

Logical not (!) operator takes a single expression and negates the
value of the expression.
Logical NOT produces a zero if the expression evaluates to a non-zero
value and produces a 1 if the expression produces a zero.
In other words, it just reverses the value of the expression. For
example, a = 10, b b = !a; Now, the value of b = 0. The value of a
is not zero, therefore, !a = 0. The value of !a is assigned to b, hence,
the result.

PYTHON PROGRAMMING 51 / 64
Membership Operators
Python supports two types of membership operators-in and not in.
These operators, as the name suggests, test for membership in a
sequence such as strings, lists, or tuples.
in Operator: The operator returns True if a variable is found in the
specified sequence and False otherwise.
not in Operator: The operator returns True if a variable is not found
in the specified sequence and False otherwise.

PYTHON PROGRAMMING 52 / 64
Identity Operators
Python supports two types of identity operators.
These operators compare the memory locations of two objects.
is Operator: Returns True if operands or values on both sides of the
operator point to the same object and False otherwise. For example,
if a is b returns 1, if id(a) is same as id(b).
is not Operator: Returns True if operands or values on both sides of
the operator does not point to the same object and False otherwise.
For example, if a is not b returns 1, if id(a) is not same as id(b).

PYTHON PROGRAMMING 53 / 64
Identity Operators

PYTHON PROGRAMMING 54 / 64
Operators Precedence and Associativity
When an expression has more than one operator, then it is the relative
priorities of the operators with respect to each other that determine the
order in which the expression will be evaluated.

PYTHON PROGRAMMING 55 / 64
Operators Precedence and Associativity
>>> 10 + 30 * 5
160
Parentheses can change the order in which an operator is applied.
The operator in the parenthesis is applied first even if there is a
higher priority operator in the expression.
>>> (40 + 20) * 30 / 10
180
>>> 40 + (20 * 30) / 10
100
Operators are associated from left to right. This means that operators
with same precedence are evaluated in a left to right manner.
>>>False == False or True
True (Because == has a higher precedence than or)
>>>False == (False or True)
False (Parenthesis has changed the order of operators)
>>>(False == False) or True
True
PYTHON PROGRAMMING 56 / 64
Expressions

An expression is any legal combination of symbols (like variables,


constants and operators) that represents a value.
In Python, an expression must have at least one operand (variable or
constant) and can have one or more operators.
On evaluating an expression, we get a value.
Operand is the value on which operator is applied.

PYTHON PROGRAMMING 57 / 64
Expressions
Constant Expressions: One that involves only constants.
Eg: 8 + 9 – 2
Integral Expressions: One that produces an integer result after
evaluating the expression.
Eg: a = 10
Floating Point Expressions: One that produces floating point results.
Eg: a * b / 2
Relational Expressions: One that returns either true or false value.
Eg: c = a>b
Logical Expressions: One that combines two or more relational
expressions and returns a value as True or False.
Eg: a>b && y! = 0
Bitwise Expressions: One that manipulates data at bit level.
Eg: x = y&z
Assignment Expressions: One that assigns a value to a variable.
Eg: c = a + b or c = 10
PYTHON PROGRAMMING 58 / 64
Operations on Strings
Concatenation

Multiplication(Repetition)

PYTHON PROGRAMMING 59 / 64
Operations on Strings

PYTHON PROGRAMMING 60 / 64
Operations on Strings

Slice a String
You can extract subsets of strings by using the slice operator ([ ] and
[:]).
You need to specify index or the range of index of characters to be
extracted.
The index of the first character is 0 and the index of the last
character is n-1, where n is the number of characters in the string.
If you want to extract characters starting from the end of the string,
then you must specify the index as a negative number.
For example, the index of the last character is -1.

PYTHON PROGRAMMING 61 / 64
Operations on Strings
Slice a String

PYTHON PROGRAMMING 62 / 64
Operations on Strings

Slice a String Output:

PYTHON PROGRAMMING 63 / 64
Module I
Introduction: Problem Solving Strategies (31-32), Program Design Tools
(32-41), Programming Paradigms (69-72),Features of OOPs (72-76),
Merits and Demerits of OOP Language (77).
Basics of Python Programming: Features of Python (83-85), Python
IDLE (87-88), Literal Constants (88-93),Variables and Identifiers (94),
Data Types (94-97), Input/output Operations (97-98), Comments(98),
Indentation(98-99),Reserved words(98), operators and
expressions(99-109), Type conversion(110-112).

PYTHON PROGRAMMING 64 / 64

You might also like