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

PY1

Uploaded by

akshatgupta02668
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PY1

Uploaded by

akshatgupta02668
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

PYTHON PROGRAMMING

AKTU SYLLABUS UNIT-1

Introduction to Python: Python variables, Python basic Operators,


Understanding python blocks. Python Data Types, Declaring and using
Numeric data types: int, float etc.
PYTHON PROGRAMMING

UNIT -1 :Introduction to Python

Lec-1
Today’s Target
 Basic about python
 Variable, print(), input()
 Some basic program
 AKTU PYQs
What is python and its features?
 Python is a high-level, Features:
1. Simple and Easy to Learn:
general-purpose
2. It is an interpreted language.
programming language that
3. Programs written in Python can run on various
is widely used for various
operating systems like Windows, macOS, Linux, etc.,
applications like web
without any modifications. (Cross platform
development, data science,
compatible)
automation, artificial
4. Python is open-source.
intelligence, and more.
5. Python is dynamically typed, meaning you don't
 It was created by Guido van
need to declare variable types explicitly. Python
Rossum and released in
automatically assigns data types at runtime.
1991.
Print ( )
 print( ) function prints the
message to the screen or any
other standard output device.
 The end keyword is used to
specify the content that is to be
printed at the end of the
execution of the print( )
function.
 By default, it is set to “\n”,
which leads to the change of
line after the execution of print(
) statement
Variable
 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.
 The value stored in a variable can be
changed during program execution.
 A Variables in Python is only a name
given to a memory location.
Input ( )
 This function first takes the input  But if we want to input the data of any
from the user and converts it into a other type then we must convert it to
string. that type (type casting/Explicit
 The type of the returned object conversion)
always will be <class ‘str’>.
What is dynamic typing? (AKTU 2023-24 EVEN)

 When you assign a value to a variable,


Python's interpreter automatically
infers the variable's type based on the
assigned value.
 The type of a variable can change
during the execution of the program, as
Python doesn't enforce strict typing.
 Python automatically determines the
type of the variable based on the value
assigned to it at runtime.
What is dynamic typing? (AKTU 2023-24 EVEN)

KEYPOINTS:
1. The type of the variable is determined when a value is assigned.
2. No need to declare the type of a variable.
3. Variables can store different types of values at different times in the program, this
provide greater flexibility.
ADVANTGES DISADVANTAGES
 Ease of Coding: You don't have to Runtime Errors: Errors related to type mismatches
worry about declaring types are only caught during execution (runtime), making
explicitly, making code faster and it harder to catch bugs early compared to statically
easier to write, especially for typed languages.
beginners. Less Explicit Code: Since types are not declared, it
 Flexibility: You can reuse the may be harder to understand the intended usage of
same variable for different types variables, leading to confusion and less maintainable
of data, which can be convenient code in large programs.
in certain cases like handling
multiple data types dynamically.
Write a program of swapping of two number(using third
/intermediate/temporary variable ) AKTU 2021-22
Write a program of swapping of two number(without any third variable)
PYTHON

UNIT -1 : Introduction to python

Lec-2
Today’s Target
 Type conversion
 Data Type
 AKTU PYQs By Pragya rajvanshi
B.tech,M.tech
Type conversion
The act of changing an object’s data type is known as type conversion.

Implicit conversion(Automatic Explicit conversion(type casting)


conversion)

 Python interpreter automatically  The data type is manually changed by the user
converts one data type to another as per their requirement
without any user involvement.  There is a risk of data loss since we are forcing
 There is no loss of data. an expression to be changed in some specific
data type.
DATA TYPE
 Python Data types are the classification or categorization of data items.
 A data type defines the kind of values a variable can hold and determines what
operations can be performed on that data.
NUMERIC
 Python, numeric data type is used to
hold numeric values.
 Int - holds signed integers of non-
limited length.
 float - holds floating decimal points
and it's accurate up to 15 decimal
places.
 complex - holds complex numbers.
SEQUENCING TYPE
String
 The sequence of characters in the
quotation marks can be used to
describe the string.
LIST
 List is an ordered collection of similar
or different types of items separated
by commas and enclosed within
brackets [ ].
Tuple
 Tuple is an ordered sequence of items
same as a list. The only difference is that
tuples are immutable. Tuples once created
cannot be modified.
 we use the parentheses ( ) to store items of
a tuple.
Dictionary data type
 Dictionary is an ordered collection of items. It stores elements in key/value
pairs.
 Here, keys are unique identifiers that are associated with each value .
Boolean Sets
 The Boolean value can be of two  Set items are unordered,
types only i.e. either True or False. unchangeable, and do not allow
The output indicates the variable is a duplicate values.
Boolean data.
PYTHON PROGRAMMING

UNIT -1 : Introduction to Python

Lec-3
Today’s Target
 Operators
 Some basic program
 AKTU PYQs Pragya Rajvanshi
B.tech,M.tech
Write a program to find out the area and perimeter of Square.
Benefits of python(interpreted , high level, OOP)
Q. How do you read an input from a
 Python is interpreted, which allows for easier
user in python to be used as an
debugging and code development.
integer in the rest of the program ?
 Python is portable across operating systems and
Explain with an example. (AKTU)
interactive, allowing real-time code execution and
testing.
 Python is dynamically typed, meaning you don’t need
to declare data types explicitly.
 Python is a high-level language that abstracts low-level
details, making it more user-friendly.
 Python is open source.
Ques. In some language every statement ends with semi-colon (;) what
happens if you put a semicolon at the end of python statement

 A semicolon in Python signifies separation rather than termination.


 No error produce.
OPERATOR Special operators
 These are the special symbols in
 Identity operators
Python and are used to execute
 Membership operators
an Arithmetic or Logical
 1 Arithmetic operators
computation
 An Operand is a value that the operator example

operator needs to complete a +,-,* 3+3=6 5-3=2 5*3=15


task. /,%(modulus) 50/2=25, 21%2=1
 Types of Operators in Python- **(exponent) 10**3=1000
//( floor division) It divides two numbers and
 Arithmetic operators.
rounds down to the nearest
 Comparison operators . integer.
 Assignment operators. result = 7 // 3
print(result) # Output: 2
 Logical operators .
 Bitwise operators.
2. Comparison operator
3. logical Operators 4. Bitwise operator
 It is used to make decision based  Bitwise operators are used in Python
on condition. to perform the operations on binary
 And - Logical AND numerals or bit patterns.
 Or - Logical OR  It operates bit by bit.
 Not - Logical not  & Binary AND
 | Binary OR
 ^ Binary XOR
 ~ Binary Ones Complement
 << Binary Left Shift
 >> Binary Right Shift
Bitwise and

128 64 32 16 8 4 2 1

2 0 0 0 0 0 0 1 0
5 0 0 0 0 0 1 0 1
2&5 0 0 0 0 0 0 0 0

Bitwise or

128 64 32 16 8 4 2 1

2 0 0 0 0 0 0 1 0
5 0 0 0 0 0 1 0 1
2|5 0 0 0 0 0 1 1 1
Bitwise ones' complement Bitwise xor
128 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1

2 0 0 0 0 0 0 1 0
5 0 0 0 0 0 1 0 1
5 0 0 0 0 0 1 0 1
~5 1 1 1 1 1 0 1 0
2^5 0 0 0 0 0 1 1 1
128 64 32 16 8 4 2 1

5 0 0 0 0 0 1 0 1

5<<1 0 0 0 0 1 0 1 0
Bitwise
left shift

128 64 32 16 8 4 2 1

5 0 0 0 0 0 1 0 1

5>>1 0 0 0 0 0 0 1 0
Bitwise
right shift
Membership operator
 Membership Operators are
mainly used in Python to find
whether a value or variable is
present in a sequence (string,
tuple, list, set, and directory).
 “In” and “Not In” are the two
membership operators
available in Python.
Membership Operator Description Syntax

Returns True if the value exists in a


in Operator Value/variable in sequence
sequence, else returns False

Returns False if the value exists in Value/variable not


not in Operator
a sequence, else returns True in sequence
Identity Operators
Identity operators are used to compare the objects if both the objects are
actually of the same data type and share the same memory location.

Identity Operator Description Syntax

Returns True if both objects


is Operator refers to same memory location, obj1 is obj2
else returns False

Returns False if both object refers


is not Operator to same memory location, else obj1 is not obj2
returns True
Variable Variable
Tuple string
List
PYTHON PROGRAMMING

UNIT -1 :Introduction to Python

Lec-4
Today’s Target
 Global and local variable
 Operator Precedence and Associativity
 Single line block and multi line block
 AKTU PYQs
Difference between-
GLOBAL VARAIBLE LOCAL VARAIBLE

Definition Declared outside the functions. Declared within the functions.

Data
It Offers Data Sharing. It doesn’t offers Data Sharing.
Sharing

It can access only inside the


Scope It can be access throughout the code.
function.

They are created when the execution of the They are created when the function
Lifetime program begins and are lost when the starts its execution and are lost
program is ended. when the function ends.

once changed the variable don’t


Once the value changes it is reflected
Value affect other functions of the
throughout the code.
program.
Local variable Global Variable
Operator precedence and
associativity
 In Python, operators have different
levels of precedence, which
determine the order in which they
are evaluated.
 When multiple operators are present
in an expression, the ones with higher
precedence are evaluated first.
 In the case of operators with the
same precedence, their associativity
comes into play, determining the
order of evaluation
Precedence Operators Description Associativity

1 () Parentheses Left to right

x[index], x[index:in Subscription,


2 Left to right
dex] slicing

3 await Await expression N/A

4 ** Exponentiation Right to left

Positive, negative,
5 +x, -x, ~x Right to left
bitwise NOT

Multiplication,
matrix, division,
6 *, @, /, //, % Left to right
floor division,
remainder
Precedence Operators Description Associativity

Addition and
7 +, – Left to right
subtraction

8 <<, >> Shifts Left to right

9 & Bitwise AND Left to right

10 ^ Bitwise XOR Left to right

11 | Bitwise OR Left to right

in, not in, is, is Comparisons,


12 not, <, <=, >, >=, !=, membership tests, Left to Right
== identity tests
Precedence Operators Description Associativity

13 not x Logical/Boolean NOT Right to left

14 and Logical/Boolean AND Left to right

15 or Logical/Boolean OR Left to right

16 if-else Conditional expression Right to left

17 lambda Lambda expression N/A

Assignment expression
18 := Right to left
(walrus operator)
Solve the following question Solve the following question
a & b << 2 // 5 ** 2 + c ^ b a**2<<2>>b**2^c**3
a=3,b=5,c=10 a=3, b=5,c=10
3&5<<2//5**2+10^5 Precedence 1)**(right to left)
((1) ** (2) // (3) + (4) << (5) & (6)^ 2)<<,>>(left to right)
precedence) 3)^(left to right)
3& 5<<2//25+10^5 3**2<<2>>5**2^10**3
3&5<<0+10^5 3**2<<2>>5**2^1000
3&5<<10^5 3**2<<2>>25^1000
3&5120^5 9<<2>>25^1000
0^5=5 36>>25^1000
(Note a ^0=a and a^1=a’) 0^1000=1000
What will the output of the program (AKTU2020-21)
Questions
Difference between-
Single-line block Multi-line block

Code is written in a single line. Code spans multiple lines with proper indentation.

Might be more challenging to identifying Easier to identify and correct error due to the
and fix error within a single line structured layout

You might also like