BCA, Department of Information Technology teaches Python to Semester II students. The Curriculum of BCA JIMS is very well updated and as per the IT Industry. Admissions to BCA are open and students keen to do BCA have started applying.
To Apply for the best BCA College in Delhi NCR with the best Industry Interface you can visit www. jimssouthdelhi.com
Admission Open 2022, Apply Online
#BCAAdmissionOpen2022
#BestBCACollegeinDelhi
#JIMSVasantKunjII
#TopITPlacements
#ReasonableFees
#BCAUpdatedCurriculum
#TopBCAInstitute
#JIMSVasantKunjII
#BestBCACollegeinDelhi
#BCAADMISSIONOPEN2022
#BestBCACurriculum
#TopBCAPlacements
#BestIndustryInterface
Visit us at https://www.jimssouthdelhi.com/
BCA Course:https://www.jimssouthdelhi.com/course-bca.html
BCA Placements:https://www.jimssouthdelhi.com/bca-placement.html
BCA Admissions :https://www.jimssouthdelhi.com/admission-procedure.html
Follow us on:
Facebook: https://www.facebook.com/JIMSVASANTKUNJII/
Twitter: https://twitter.com/jimsljptweets
Instagram : : https://www.instagram.com/jims_vk2/?hl=en
YouTube: https://www.youtube.com/channel/UCZgioa2rpculDY7bHlljD6g
Blog: https://jimssouthdelhi.com/blog/
Linked In: https://www.linkedin.com/in/jims-vasant-kunj-ii-38785a85/
1. Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: BVITSD 205 : Programming And
Problem Solving Though PYTHON
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
2. Subject: BVITSD 205 : PROGRAMMING AND
PROBLEM SOLVING THOUGH PYTHON
Topic: Unit II- Python Variables and Data Types
@Dr. Arpana Chaturvedi
3. Unit-II Python Variables and Data Types
@Dr. Arpana Chaturvedi
An Introduction to
Variables
Data Types
Types
Conversion
Local and
Global
Variables
Variable Naming
Rules
Assigning
Variables
Numbers
Strings
int()
float()
Local
Variables
Global
Variables
str()
bool()
list()
List
Tuple
Dictionaries
Multiple
Variables
Deleting
Variables
4. Unit-II Python Variables
@Dr. Arpana Chaturvedi
Python Variables
X=45
Type=Integer
name=“ JIMS ”
Type=String
nums=[ 1,3,5,7 ] Type=Lists
45
x
“ JIMS ”
name
[ 1,3,5,7 ]
nums
▰ A variable is a container for a value. It can be assigned a name, you can use it to
refer to it later in the program.
▰ Based on the value assigned, the interpreter decides its data type. You can always
store a different type in a variable.
5. Unit-II Python Variable Naming Convention
There are certain rules to what you can name a variable(called an identifier).
▰ Python variables can only begin with a letter(A-Z/a-z) or an underscore(_).
▰ The rest of the identifier may contain letters(A-Z/a-z), underscores(_), and
numbers(0-9).
▰ Python is case-sensitive, and so are Python identifiers. Name and name are two
different identifiers.
▰ Reserved words (keywords) cannot be used as identifier names.
@Dr. Arpana Chaturvedi
7. Unit-II 2. Assigning and Reassigning Python
Variables
▰ To assign a value to Python variables, you don‟t need to declare its type.
▰ You name it according to the rules of variable naming convention, and type the
value after the equal sign(=).
@Dr. Arpana Chaturvedi
You can‟t put the identifier on the right-hand side of the equal sign, though. The
following code causes an error.
Neither can you assign Python variables to a keyword.
8. Unit-II 3. Multiple Assignment
▰ You can assign values to multiple Python variables in one statement.
▰ Or you can assign the same value to multiple Python variables.
@Dr. Arpana Chaturvedi
9. Unit-II 4. Swapping Python Variables
▰ Swapping means interchanging values. To swap Python variables, you don‟t need
to do much.
@Dr. Arpana Chaturvedi
10. Unit-II 4.Deleting Python Variables
▰ You can also delete Python variables using the keyword „del‟.
@Dr. Arpana Chaturvedi
11. Unit-II Python Datatypes
▰ Although we don‟t have to declare a type for Python variables, a value does have a
type. This information is vital to the interpreter.
▰ Python supports the following data types.
1. Python Numbers
2. Strings
3. Python Lists
4. Python Tuples
5. Dictionaries
7. Sets
@Dr. Arpana Chaturvedi
12. Unit-II 1. Python Numbers
▰ There are four numeric Python data types.
a. int
b. float
c. long
d. complex
@Dr. Arpana Chaturvedi
13. Unit-II 1. Python Numbers
a. int
▰ int stands for integer. This Python Data Type holds signed integers. We can use the
type() function to find which class it belongs to.
@Dr. Arpana Chaturvedi
An integer can be of any length, with the only limitation being the available memory.
14. Unit-II 1. Python Numbers
b. float
▰ This Python Data Type holds floating-point real values. An int can only store the
number 3, but float can store 3.25 if you want.
@Dr. Arpana Chaturvedi
c. long
This Python Data type holds a long integer of unlimited length. But this construct does
not exist in Python 3.x.
15. Unit-II 1. Python Numbers
d. complex
This Python Data type holds a complex number. A complex number looks like this: a+bj
Here, a and b are the real parts of the number, and j is imaginary.
@Dr. Arpana Chaturvedi
Use the isinstance() function to tell if Python variables belong to a particular class. It
takes two parameters- the variable/value, and the class.
16. Unit-II 2. Strings
▰ A String is a sequence of characters. Python does not have a char data type, unlike
C++ or Java. You can delimit a string using single quotes or double-quotes.
@Dr. Arpana Chaturvedi
a. Spanning a String Across Lines
▰ To span a string across multiple lines, you can use triple quotes.
As you can see, the quotes
preserved the formatting (n is the
escape sequence for newline, t is
for tab).
17. Unit-II 2. Strings
@Dr. Arpana Chaturvedi
b. Displaying Part of a String
You can display a character from a string using its index in the string. Remember,
indexing starts with 0.
You can also display a burst of characters in a string using the slicing operator [].
This prints the characters from 0 to 5.
18. Unit-II 2. Strings
@Dr. Arpana Chaturvedi
c. String Formatters
String formatters allow us to print characters and values at once. You can use the %
operator.
Or you can use the format method.
A third option is to use f-strings.
19. Unit-II 2. Strings
@Dr. Arpana Chaturvedi
d. String Concatenation
You can concatenate(join) strings.
However, you cannot concatenate values of different types.
20. Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
A list is a collection of values. Remember, it may contain different types of values.
To define a list, you must put values separated with commas in square brackets. You
don‟t need to declare a type for a list either.
a. Slicing a List
You can slice a list the way you‟d slice a string- with the slicing operator.
Indexing for a list begins with 0, like for a string. A Python doesn‟t have arrays.
21. Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
c. Length of a List
Python supports an inbuilt function to calculate the length of a list.
c. Reassigning Elements of a List
A list is mutable. This means that you can reassign elements later on.
22. Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
d. Iterating on the List
To iterate over the list we can use the for loop. By iterating, we can access each
element one by one which is very helpful when we need to perform some operations
on each element of list.
23. Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
e. Multidimensional Lists
A list may have more than one dimension.
24. Unit-II Python Tuples
▰ A tuple is like a list. You declare it using parentheses instead.
@Dr. Arpana Chaturvedi
a. Accessing and Slicing a Tuple
You access a tuple the same way as you‟d access a list. The same goes for slicing
it.
25. Unit-II Python Tuples
@Dr. Arpana Chaturvedi
b. A tuple is Immutable
Python tuple is immutable. Once declared, you can‟t change its size or elements.
26. Unit-II 5. Dictionaries
▰ A dictionary holds key-value pairs. Declare it in curly braces, with pairs separated
by commas. Separate keys and values by a colon(:).
▰ The type() function works with dictionaries too.
@Dr. Arpana Chaturvedi
27. Unit-II 5. Dictionaries
▰ a. Accessing a Value
▰ To access a value, you mention the key in square brackets.
▰ b. Reassigning Elements
▰ You can reassign a value to a key.
@Dr. Arpana Chaturvedi
28. Unit-II 5. Dictionaries
▰ c. List of Keys
▰ Use the keys() function to get a list of keys in the dictionary.
@Dr. Arpana Chaturvedi
29. Unit-II 6. bool
▰ A Boolean value can be True or False.
@Dr. Arpana Chaturvedi
30. Unit-II 7. Sets
▰ 7. Sets
▰ A set can have a list of values. Define it using curly braces.
@Dr. Arpana Chaturvedi
It returns only one instance of any value present more than once.
However, a set is unordered, so it doesn‟t support indexing.
31. Unit-II 7. Sets
▰ 7. Sets
▰ Also, it is mutable. You can change its elements or add more. Use the add() and
remove() methods to do so.
@Dr. Arpana Chaturvedi
32. Unit-II Python Type Conversion
▰ Since Python is dynamically-typed, you may want to convert a value into another
type. Python supports a list of functions for the same.
▰ 1. int()
▰ It converts the value into an int.
@Dr. Arpana Chaturvedi
Notice how it truncated 0.7 instead of rounding the number off to 4. You can also turn
a Boolean into an int.
33. Unit-II Python Type Conversion
▰ However, you cannot turn a string into an int. It throws an error.
@Dr. Arpana Chaturvedi
However, if the string has only numbers, then you can.
34. Unit-II Python Type Conversion
▰ 2. float()
▰ It converts the value into a float.
@Dr. Arpana Chaturvedi
However, this number works even without the float() function.
You can also use ‘e’ to denote an exponential number.
35. Unit-II Python Type Conversion
▰ 3. str()
▰ It converts the value into a string.
@Dr. Arpana Chaturvedi
36. Unit-II Python Type Conversion
▰ 4. bool()
▰ It converts the value into a Boolean.
@Dr. Arpana Chaturvedi
37. Unit-II 5. Python Sets
▰ 5. set()
▰ It converts the value into a set.
@Dr. Arpana Chaturvedi
38. Unit-II 6. Python list()
▰ 6. list()
▰ It converts the value into a list.
@Dr. Arpana Chaturvedi
39. Unit-II 7. Python tuple
▰ 7. tuple()
▰ It converts the value into a tuple.
@Dr. Arpana Chaturvedi
You can try your own combinations. Also
try composite functions.
40. Unit-II Python Local and Global Variables
▰ 1. Python Local Variables
▰ When you declare a variable in a function, class, or so, it is only visible in that
scope. If you call it outside of that scope, you get an „undefined‟ error.
@Dr. Arpana Chaturvedi
Here, the variable num is local to function func1().
41. Unit-II Python Local and Global Variables
▰ 2. Global Variables
▰ When you declare a variable outside any context/scope, it is visible in the whole
program.
@Dr. Arpana Chaturvedi
42. Unit-II Python Local and Global Variables
▰ 2. Global Variables
▰ You can use the „global‟ keyword when you want to treat a variable as global in a
local scope.
@Dr. Arpana Chaturvedi
43. Unit-II Python Variables Assignment
▰ What are variables and data types in Python?
▰ What is type () in Python?
▰ What are Local and Global variables in Python?
▰ Explain various naming rules for Python Variables.
▰ How to display part of a string?
@Dr. Arpana Chaturvedi