4 TH
4 TH
Department BSCS
Programming Fundamental
Semester 1st
Lecture #4
Topic : Data types in python
Built-in library
Data Type
The concept of a data type in programming is essential because it
determines the kind of data a variable can store and the operations
that can be performed on that data.
• In Python, data types are fundamental because they help the
interpreter allocate memory, decide on operations, and ensure
that data is processed accurately.
Basic Data Types
•Integers (int): Whole numbers, positive or negative, with no
decimal point, e.g., 5, -3.
•Floating-Point Numbers (float): Numbers with a decimal
point, useful for representing real numbers e.g., 3.14, -0.001.
•Booleans (bool): Represents binary values True and False,
often used in conditional statements and logical operations.
Sequence Data Types
•Strings (str): A sequence of characters, enclosed in
single, double, or triple quotes, e.g., 'hello', "Python".
•Lists (list): Ordered, mutable collections that can hold
different data types, e.g., [1, "apple", 3.5].
•Tuples (tuple): Similar to lists but immutable, meaning
once defined, their values cannot change, e.g., (1, 2,
"banana").
Mapping Data Types
•Dictionaries (dict): Key-value pairs, useful for
associating unique keys with values, e.g., {"name":
"Alice", "age": 25}.
Set Data Types
•Sets (set): Unordered collections of unique items, used
for membership testing and removing duplicates, e.g.,
{1, 2, 3, 3} results in {1, 2, 3}.
Variables in a Programming Language
•
:
In programming, an identifier (or variable) is a name given to a storage location in
memory that holds data, which can be manipulated and used throughout a
program. Variables allow programmers to store, retrieve, and manipulate data
without needing to know the memory location.
Use of Variables in a Programming Language
• Data Storage: Variables temporarily hold data that a program needs to function.
• Readability: Meaningful variable names make code easier to read and understand.
• Reusability and Flexibility: Variables allow data to be reused, modified, and
adapted in different parts of a program without hardcoding values.
• Modularity: Using variables allows for structured and modular code, where values
can be changed in one place and reflected across the program.
Valid Variable :
Defining Valid Variable Names in Python
Python has specific rules for defining valid variable names,
which help prevent errors and make code more readable:
Alphabetic and Numeric Characters
A variable name can contain letters (A-Z, a-z), digits (0-9), and
underscores (_).
python
1variable = 20
No Reserved Keywords
Python has no reserved keywords (like class, if, while,
etc.) that cannot be used as variable names.
python
for = 5
Avoid Special Characters: Variable names cannot
contain spaces or special characters like @, #, $, &, etc.,
except for the underscore
Valid: name, _name, age2, total_sum, max_value
Specifier
Using % Format Specifier
•The % operator allows format specifiers
within a string.
•Common format specifiers include:
%d: Integer
%f: Floating-point number (default 6
decimal places)
%.nf: Floating-point with n decimal places,
%.2f for 2 decimal places
%s: String
Example :
age = 25
name = "Alice"
print("Hello, %s! You are %d years old." % (name, age))
Example 2
name = "Bob“
height = 5.9