Python Notes for Student-1
Python Notes for Student-1
Prof. K.D.Dongare
Python Programming
Introduction to Python
def func():
statement 1
statement 2
…………………
…………………
statement N
Prof. K.D.Dongare
Python Programming
✓ In the above example, the statements that are the same level to the right belong to
the function. Generally, we can use four whitespaces to define indentation.
❖ Python Features
• Readability: Python's syntax emphasizes code readability and uses indentation
to define code blocks, making it easy to write and understand code.
• Versatility: Python can be used for various applications, including web
development, data analysis, scientific computing, artificial intelligence,
automation, and more.
• Dynamic Typing: Python uses dynamic typing, allowing variables to change
types on the fly, which enhances flexibility and reduces the need for explicit type
declarations.
• Interpreted Nature: Python is an interpreted language, allowing you to execute
code directly without needing a compilation step, making development and
debugging quicker.
• Large Standard Library: Python comes with a comprehensive standard library
that provides modules and functions for a wide range of tasks, reducing the need
to reinvent the wheel.
• Cross-Platform: Python is cross-platform, meaning code written on one
platform can be easily executed on other platforms without significant
modifications.
• Object-Oriented: Python supports object-oriented programming, allowing you
to structure code using classes and objects for better organization and reusability.
• Extensive Third-Party Libraries: Python has a vast ecosystem of third- party
libraries and frameworks that simplify complex tasks and accelerate development.
• Community and Support: Python has a large and active community of
developers who contribute to its growth, provide support, and create various
resources for learning.
❖ Python Application
Prof. K.D.Dongare
Python Programming
1) Web Applications
3) Console-based Application
4) Software Development
• This is the era of Artificial intelligence where the machine can perform the
task the same as the human.
• Python language is the most suitable language for Artificial intelligence or
machine learning.
• It consists of many scientific and mathematical libraries, which makes easy
to solve complex calculations.
6) Business Applications
Prof. K.D.Dongare
Python Programming
8) 3D CAD Applications
9) Enterprise Applications
• Python contains many libraries that are used to work with the image.
• The image can be manipulated according to our requirements.
Prof. K.D.Dongare
Python Programming
Output:
Shree Krushna
Prof. K.D.Dongare
Python Programming
Keywords
• Python keywords are unique words reserved with defined meanings and
functions that we can only apply for those functions. You'll never need to
import any keyword into your program because they're permanently present.
• Python's built-in methods and classes are not the same as the keywords.
Built-in methods and classes are constantly present; however, they are not as
limited in their application as keywords.
• Assigning a particular meaning to Python keywords means you can't use them
for other purposes in our code. You'll get a message of SyntaxError if you
attempt to do the same. If you attempt to assign anything to a built-in method
or type, you will not receive a SyntaxError message; however, it is still not a
smart idea.
Prof. K.D.Dongare
Python Programming
print( 4 == 4 )
print( 6 > 9 )
print( True or False )
Output:
True
False
True
Prof. K.D.Dongare
Python Programming
OR, 𝗏 || or
NOT, ¬ ! not
CONTAINS, ∈ in
IDENTITY === is
Example:
x = 10
y=3
addition = x + y
comparison = x > y
logical_and = x > 5 and y < 5
assignment = x += y
print("Addition:", addition)
print("Comparison:", comparison)
print("Logical AND:", logical_and)
print("Assignment:", assignment)
Prof. K.D.Dongare
Python Programming
Output:
Addition: 13 Comparison:
True Logical AND: False
SyntaxError: cannot use assignment expressions with subscript
Examples:
Prof. K.D.Dongare
Python Programming
Output:
1
2
3
0
1
2
Examples:
if number > 0:
print("The number is positive.")
Output:
Prof. K.D.Dongare
Python Programming
❖ Python Keyword
Python Keywords are some predefined and reserved words in Python that have special
meanings. Keywords are used to define the syntax of the coding. The keyword cannot be
used as an identifier, function, or variable name. All the keywords in Python are written in
lowercase except True and False. There are 35 keywords in python
In Python, there is an inbuilt keyword module that provides an is keyword() function that can
be used to check whether a given string is a valid keyword or not. Furthermore, we can check
the name of the keywords in Python by using the kwlist attribute of the keyword module.
Prof.K.D.Dongare
Python Programming
and This is a logical operator which returns true if both the operands are true else returns false.
or This is also a logical operator which returns true if anyone operand is true else returns false.
not This is again a logical operator it returns True if the operand is false else returns false.
Elif is a condition statement used with an if statement. The elif statement is executed if the previous
elif
conditions were not true.
Else is used with if and elif conditional statements. The else block is executed if the given condition
else
is not true.
assert This function is used for debugging purposes. Usually used to check the correctness of code
Prof.K.D.Dongare
Python Programming
Keywords Description
Finally is used with exceptions, a block of code that will be executed no matter if there is an
finally
exception or not.
in It’s used to check whether a value is present in a list, range, tuple, etc.
This is a special constant used to denote a null value or avoid. It’s important to remember, 0, any
none
empty container(e.g empty list) do not compute to None
Prof.K.D.Dongare
Python Programming
Identifiers in Python
Identifier is a user-defined name given to a variable, function, class, module, etc. The
identifier is a combination of character digits and an underscore. They are case-sensitive i.e.,
‘num’ and ‘Num’ and ‘NUM’ are three different identifiers in python. It is a good
programming practice to give meaningful names to identifiers to make the code
understandable.
or
Python Identifier is the name we give to identify a variable, function, class, module or other
object. That means whenever we want to give an entity a name, that’s called identifier.
Sometimes variable and identifier are often misunderstood as same but they are not. Well for
clarity, let’s see what is a variable?
What is Variable
A variable, as the name indicates is something whose value is changeable over time. In fact a
variable is a memory location where a value can be stored. Later we can retrieve the value to
use. But for doing it we need to give a nickname to that memory location so that we can refer
to it. That’s identifier, the nickname.
.
Rules for Naming Python Identifiers
• It cannot be a reserved python keyword.
• It should not contain white space.
• It can be a combination of A-Z, a-z, 0-9, or underscore.
• It should start with an alphabet character or an underscore ( _ ).
• It should not contain any special character other than an underscore ( _ ).
Valid identifiers:
• var1
• _var1
• _1_var
• var_1
Invalid Identifiers
• !var1
• 1var
• 1_var
• var#1
• var 1
Prof.K.D.Dongare
Python Programming
Variable
Example:
Here we have stored “Krushna” in a var which is variable, and when we call its
name the stored information will get printed.
Var = “Krushna”
print(Var)
Output:
Krushna
Note:
• 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, all the
operations done on the variable effects that memory location.
Prof.K.D.Dongare
Python Programming
Example:
# display
print( Number)
Output:
100
Prof.K.D.Dongare
Python Programming
❖ Types of Variable
There are two types of variables in Python - Local variable and Global variable. Let's
understand the following variables.
◆ Local Variable
Local variables are the variables that declared inside the function and have
scope within the function.
Example:
# This function uses global variables def
f():
s = "Krushna"
print(s)
Output:
Krushna
◆ Global Variables
Global variables can be used throughout the program, and its scope is in the entire
program. We can use global variables inside or outside the function.
Example:
Prof.K.D.Dongare
Python Programming
Output:
I love Krushna
❖ Delete a variable
We can delete the variable using the del keyword. The syntax is given
below.
Syntax -
del <variable_name>
Example:
a = 10
b = 20
print(a+b)
a = "Shree "
Output:
b = "Krushna"
30
print(a+b)
ShreeKrushna
Prof. K.D.Dongare
Python Programming
Data Types
• Every value has a datatype, and variables can hold values. Python is a
powerfully composed language; consequently, we don't have to
characterize the sort of variable while announcing it.
• The interpreter binds the value implicitly to its type.
a=5
We did not specify the type of the variable a, which has the value five from an
integer. The Python interpreter will automatically interpret the variable as an
integer.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Prof. K.D.Dongare
Python Programming
x = "Hello World"
#display x:
print(x)
Output
Hello World
<class 'str'>
x = 1 # int
y = 2.8 # float
z = 1j # complex
To verify the type of any object in Python, use the type() function:
Example
print(type(x))
print(type(y))
print(type(z))
Prof. K.D.Dongare
Python Programming
x = 20
#display x:
print(x)
Output=
20
<class 'int'>
#display x:
print(x)
Output
['apple', 'banana', 'cherry']
<class 'list'>
Prof. K.D.Dongare
Python Programming
◆ Numbers
◆ Sequence Type
◆ Boolean
◆ Set
◆ Dictionary
Prof. K.D.Dongare
Python Programming
❖ Numbers
• Numeric values are stored in numbers. The whole number, float, and
complex qualities have a place with a Python Numbers datatype.
• Python offers the type() function to determine a variable's data type. The
instance () capability is utilized to check whether an item has a place with
a specific class.
➢ Int: Whole number worth can be any length, like numbers 10, 2, 29, - 20, -
150, and so on. An integer can be any length you want in Python. Its worth
has a place with int.
➢ Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc. It can be
accurate to within 15 decimal places.
➢ Complex: An intricate number contains an arranged pair, i.e., x + iy, where
x and y signify the genuine and non-existent parts separately. The complex
numbers like 2.14j, 2.0 + 2.3j, etc.
Example:
age = 25
height = 5.9
# Arithmetic operations
total = age + height
difference = age - height
product = age * height
quotient = age / height
Prof. K.D.Dongare
Python Programming
Output:
Total: 30.9
Difference: 19.1
Product: 147.5
Quotient: 4.23728813559322
❖ Dictionary
• A dictionary is a key-value pair set arranged in any order. It stores a specific
value for each key, like an associative array or a hash table.
• Value is any Python object, while the key can hold any primitive data type.
• The comma (,) and the curly braces are used to separate the items in the
dictionary.
Example:
student = {
"name": "Alice",
"major": "Computer Science"
}
# Accessing dictionary values print("Name:",
student["name"])
print("Major:", student["major"])
Output:
Name: Alice
Major: Computer Science
Prof. K.D.Dongare
Python Programming
❖ Boolean
• True and False are the two default values for the Boolean type. These
qualities are utilized to decide the given assertion valid or misleading.
• The class book indicates this. False can be represented by the 0 or the letter
"F," while true can be represented by any value that is not Zero.
Example:
is_sunny = True
is_raining = False
print(is_sunny)
print(is_raining)
Output:
True
False
❖ Set
• The data type's unordered collection is Python Set. It is alterable, mutable(can
change after creation), and has remarkable components.
• The elements of a set have no set order; It might return the element's altered
sequence. Either a sequence of elements is passed through the curly braces
and separated by a comma to create the set or the built-in function set() is
used to create the set.
• It can contain different kinds of values.
Prof. K.D.Dongare
Python Programming
Example:
# Creating a set
fruits = {"apple", "banana", "orange"}
Output:
I have an apple!
grape
orange
apple
Prof. K.D.Dongare
Python Programming
❖ Sequence Type
➢ String
➢ List
• Lists in Python are like arrays in C, but lists can contain data of different
types. The things put away in the rundown are isolated with a comma (,)
and encased inside square sections [].
• To gain access to the list's data, we can use slice [:] operators. Like how
they worked with strings, the list is handled by the concatenation
operator (+) and the repetition operator (*).
➢ Tuple
• In many ways, a tuple is like a list. Tuples, like lists, also contain a
collection of items from various data types. A parenthetical space ()
separates the tuple's components from one another.
• Because we cannot alter the size or value of the items in a tuple, it is a
read-only data structure.
Prof. K.D.Dongare
Python Programming
4 Marks
1. Explain any 4 features of python programming(CO1)
2. Write steps to install python and to run python code. (CO1)
3. List and Explain different data types used in python(CO1)
4. Describe the role of indentation in python. (CO1)
5. Define the following terms: (CO1)
i. Identifier
ii. Literal
iii. Keyword
iv. Variable
Prof. K.D.Dongare