PYTHON
UNIT-II
UNIT-II
PYTHON INTERPRETER AND INTERACTIVE MODE, DEBUGGING; VALUES AND TYPES :
Int, float, Boolean , string, and list (Examples) variables (Examples) expressions
(Examples) statements (Examples) tuple assignment (Examples) precedence of
operators (Examples) comments (Examples) ILLUSTRATIVE PROGRAMS : exchange the
values of two variables (Example) circulate the values of n variables (Example)
Distance between two points (Example)
DATA, EXPRESSIONS, STATEMENTS
Source code → Interpreter →Output
DATA, EXPRESSIONS, STATEMENTS
DATATYPES: A datatype refers to the type of data that a
particular variable or object can hold. It determines the kind of
operations that can be performed on the data and how the data
is stored in memory.
DATA, EXPRESSIONS, STATEMENTS
DATATYPES:
1.Numeric types
2.Sequence types
3. Mapping type
4.Set types
5. Boolean type
6.NoneType
DATATYPES
1.Numeric types:
•int: Integer numbers (e.g., 5, -10, 0)
EXAMPLE :
x = 10
print(x) Output: 10
•float: Floating-point numbers (e.g., 3.14, -2.5, 0.0)
EXAMPLE :
y = 3.14
print(y) Output: 3.14
2.Sequence types:
•str: String of characters (e.g., "Hello", 'Python', "123")
EXAMPLE :
name = "Alice“
print(name) Output: Alice
DATATYPES
•list: Ordered collection of items (e.g., [1, 2, 3], ['a', 'b', 'c’])
EXAMPLE :
numbers = [1, 2, 3, 4]
print(numbers) Output: [1, 2, 3, 4]
3.Mapping type:
•Dict : Collection of key-value pairs (e.g., {'name': 'Alice', 'age': 30})
EXAMPLE:
person = {'name': 'Bob', 'age': 25}
print(person) Output: {'name': 'Bob', 'age': 25}
DATATYPES
4.Set types:
•set: Unordered collection of unique items (e.g., {1, 2, 3}, {'a', 'b', 'c’})
EXAMPLE:
unique_numbers = {1, 2, 3, 4}
print(unique_numbers) Output: {1, 2, 3, 4}
5.Boolean type:
•bool: Represents truth values, either True or False
EXAMPLE:
is _valid = True
print(is _valid) Output: True
.
DATATYPES
6.NoneType:
•None: Represents a null or empty value.
EXAMPLE:
empty_ value = None
print(empty _value) Output: None
DATA, EXPRESSIONS, STATEMENTS
Question:
Python program that demo ? instates the usage of various
datatypes?
Program
# Numeric types
x = 42 # int
y = 3.14 # float
print(f"Integer: {x}")
print(f"Float: {y}")
# Sequence types
name = "Alice" # str
numbers = [1, 2, 3, 4] # list
print(f"String: {name}")
print(f"List: {numbers}")
Program
# Mapping type
person = {'name': 'Bob', 'age': 25} # dict
print(f"Dictionary: {person}")
# Set type
unique_numbers = {1, 2, 3, 4} # set
print(f"Set: {unique_numbers}")
Program
# Boolean type
is_valid = True # bool
print(f"Boolean: {is_valid}")
# NoneType
empty_value = None # NoneType
print(f"NoneType: {empty_value}")
# Demonstrating some operations
print(f"Sum of int and float: {x + y}") # Operations with numeric types
print(f"First character of string: {name[0]}") # String indexing
print(f"List after appending 5: {numbers + [5]}") # List operation
print(f"Value for key 'name' in dictionary: {person['name']}") # Dictionary access
print(f"Set after adding 5: {unique_numbers.union({5})}") # Set operation
print(f"Negation of boolean: {not is_valid}") # Boolean operation
program
# Checking the types of the variables
print(f"Type of x: {type(x)}")
print(f"Type of y: {type(y)}")
print(f"Type of name: {type(name)}")
print(f"Type of numbers: {type(numbers)}")
print(f"Type of person: {type(person)}")
print(f"Type of unique_numbers: {type(unique_numbers)}")
print(f"Type of is_valid: {type(is_valid)}")
print(f"Type of empty_value: {type(empty_value)}")
Variable