BCA 402 Python Unit 1
BCA 402 Python Unit 1
I) BCA-402
Applications:
1. Web Development
2. Game Development
3. IOT Programming
4. Machine Learning
5. AI
6. DB Programming
7. Network Programming
Limitations:
1. Mobile Application is not possible through python
2. Performance is not upto the mark
Installations Steps :
Step - 1: Select the Python's version to download.
Step - 2: Click on the Install Now.
Step - 3 Installation in Process.
Step - 4: Verifying the Python Installation.
Step - 5: Opening idle.
Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fractions or decimals). In Python, there is no limit to how long an integer
value can be.
Float – This value is represented by the float class. It is a real number with a floating-point
representation. It is specified by a decimal point. Optionally, the character e or E followed by
a positive or negative integer may be appended to specify scientific notation.
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Type of b: <class
'float'>
❖ Python String
❖ Python List
❖ Python Tuple
Python String:
A string is a collection of one or more characters put in a single quote, double-quote, or
triple-quote. In python there is no character data type, a character is a string of length one.
It is represented by str class.
Creating String
Strings in Python can be created using single quotes or double quotes or even triple
quotes.
# Python program to
# demonstrate string value
a = ‘Neelima’
print("Type of a: ", type(a))
Output:
Output:
List containing multiple values:
Ms.
Neelima
<class ‘List’>
Tuple Data Type:
Just like a list, a tuple is also an ordered collection of Python objects.
The only difference between a tuple and a list is that tuples are immutable i.e. tuples cannot
be modified after it is created. It is represented by a tuple class.
Creating a Tuple
In Python, tuples are created by placing a sequence of values separated by a ‘comma’ with or
without the use of parentheses for grouping the data sequence. Tuples can contain any number
of elements and of any datatype (like strings, integers, lists, etc.)
# Creating a Tuple
# with nested
tuples Tuple1 =
(5,55,10)
Print(type(Tuple1))
Output:
<class ‘Tuple1’>
Boolean Data Type:
Data type with one of the two built-in values, True or False. Boolean objects
that are equal to True are truthy (true), and those equal to False are falsy (false).
But non-Boolean objects can be evaluated in a Boolean context as well and
determined to be true or false. It is denoted by the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise
python will throw an error.
# Python program to
# demonstrate boolean type
print(type(True))
print(type(False))
print(type(true))
Output:
<class 'bool'>
<class 'bool'>
NameError: name 'true' is not defined
Set Data Type :
In Python, a Set is an unordered collection of data types that is iterable, mutable
and has no duplicate elements. The order of elements in a set is undefined though
it may consist of various elements.
Output:
{1, 2,‘Nisha', 4.5}<class ‘set’>
Dictionary Data Type in Python
Create a Dictionary
Note – Dictionary keys are case sensitive, the same name but different cases of
Key will be treated distinctly.
# Creating a Dictionary
Output:
Dictionary with the use of Integer Keys:
{1: ‘Welcome', 2: ‘MMDU', 3: ‘BCA'}
Variable : A variable is a container in which a value is store and that
value is change during the execution time. For ex a=10, b=5.5 etc.
Identifier:
• Identifier is the name of anything in a program like variable
name, function name, class name, object name etc.
• Identifier is a collection of valid characters that identifies something.
Construction Rule for Identifier:
Literals/Constant:
Literals are constants that are self-explanatory and don’t need to be
computed or evaluated.
1. Number Literals:12,55.5
2. Boolean Literals: True, False
3. Special Literals: None
4. Collection Literals: list, tuple, dictionary
Operators in python:
Output:
3 2.0
Examples of Relational Operators/Comparision
a = 13 Output:
b = 33 False True False True
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Logical Operators in python is as follows:
1. Logical not
2. logical and
3. logical or
# Print a or b is True
print(a or b)
In Python, is and is not are the identity operators both are used to check if two
values are located on the same part of the memory. Two variables that are
equal do not imply that they are identical.
• is True if the operands are identical
• is not True if the operands are not identical .
Output
True True
Membership Operators in Python
In Python, in and not in are the membership operators that are used to
test whether a value or variable is in a sequence.
• in True if value is found in the sequence
• not in True if value is not found in the
sequence # Python program to illustrate
# not 'in' operator
list = [10, 20, 30, 40, 50]
10 in list
Output:
True
list=[5,10,15,20]
10 not in
list Output
False
Bitwise Operators in Python
Python Bitwise operators act on bits and perform bit-by-bit operations.
These are used to operate on binary numbers.
Comments in Python are the lines in the code that are ignored by
the interpreter during the execution of the program.
There are three types of comments in Python:
1. Single line
Comments # sample
comment
# Python program to demonstrate
2. Docstring Comments
1) If Statement :
An "if statement" is written by using the if keyword.
E.g:
a = 33
b = 200
if b > a:
print("b is greater than a")
2)If-Else Statement:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
3)El-if Statement:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Iterative Statement
Loops :
It provides the two types of loops to handle looping requirements.
1) While Loop:
Syntax:
i) for var_name in range(start
,end): Statements
2) For Loop:
Syntax:
ii) while condition :
Statement
#Example to illustrate the for loop:
Output:
Enter a
number=5 5
10
15
20
25
30
35
40
45
50
#Example to illustrate the while loop:
password = “ Neelima”
input_password = input(“Enter Password= ”)
while password != input_password:
input_password = input(“Enter password= ” )
else:
print(“ Unlocked!! ”)
Output:
Enter Password= Neelima
Unlocked!!
Transfer Statement
#Break Statement
for num in
range(1,11): if num
%2==0;
break
else:
print(num)
Output:
1
#Example of continue statement:
a = 33
b = 200
If b > a:
pass
Output:
# having an empty if statement like this, would raise an error without the pass
statement
Python Build In Function :
1. print()
#it will display the statement written within print statement
2. input()
# If we want to take input from user then input function is used.
3. type()
#it will tell the type of data
. For ex.
var a=15
print(type(a))
Output:
<class:’int’>
4. int()
#The int() function returns the numeric integer equivalent from a
given expression.
# int() on string representation of numbers
print("int('9')) =", int('9'))
Output:
int('9')) = 9
5. abs()
#The abs() function return the absolute
value abs(-4)
Output
4
6. pow()
#It will return the power of value .for example
pow(2,2)
◻ 4
6. min / max()
#It will return min and max value from the list
min([6,5,3,8,9])
Output
3
7. round()
#The round() function returns a floating point number that is a rounded version
of the specified number, with the specified number of decimals.
var c=22/7
print(c)
round(c,2)
output
3.14
8. divmod()
#It return division integer and remainder value respectively
divmod(5,2)
output
(2,1)
9. bin()
#It wil return the binary value for respective number.
bin(4)
Output
(0100)
10. Id()
#It will return the memory location where the variable is store.
a=5
id(a)
14078##9833
11. len()
#It will return the length of the given string .
len(“SHIFA”)
Output
5
12. sum()
#It will add the all integers present in the list.
Sum({5,4,2,3,1})
Output
15
13. help()
#If we want to know the detail of any function then we can use the function
help.
help(“sum”)
Output
It will describe sum function
Strings
• Slicing operation is used to return / select / slice the particular substring based on
the user requirements.
• A segment of string is called slice.
• Syntax: string_variablename [start: end]
String Comparison