0% found this document useful (0 votes)
6 views

Python Data Types & Data Structures

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Data Types & Data Structures

Uploaded by

ajoantony2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Data Types

& Data Structures


6.3.1 Numbers
The Number data type is used for storing numeric values. Numbers in
Python are immutable, meaning any modification results in a new object.
Examples include integers, floating-point numbers, and complex numbers.
# Integer
a=5
print(type(a)) # Output: <class 'int'>

# Floating Point
b = 2.5
print(type(b)) # Output: <class 'float'>

# Complex Number
y = 2 + 3j
print(type(y)) # Output: <class 'complex'>
print(y.real) # Output: 2.0
print(y.imag) # Output: 3.0
6.3.2 Strings
• A String is a sequence of characters. Strings can be of any length,
including empty strings. Strings are immutable in Python.
# Creating a string
s = "Hello World!"
print(type(s)) # Output: <class 'str'>

# String concatenation
t = "This is a sample program."
print(s + " " + t) # Output: Hello World! This is a sample program.

# Getting length of a string


print(len(s)) # Output: 12

# Convert string to integer


x = "100"
y = int(x)
print(y) # Output: 100
• # Convert to upper/lower case
• print(s.upper()) # Output: HELLO WORLD!
• print(s.lower()) # Output: hello world!

• # Accessing sub-strings
• print(s[6:]) # Output: World!
• print(s[:5]) # Output: Hello
6.3.3 Lists
A List is a compound data type that stores multiple items, which can be of different types. Lists are mutable, so
elements can be added, removed, or modified.
# Creating a list
fruits = ["apple", "orange", "banana", "mango"]
print(type(fruits)) # Output: <class 'list'>
print(len(fruits)) # Output: 4

# Accessing elements
print(fruits[1]) # Output: orange

# Appending and removing elements


fruits.append("pear")
print(fruits) # Output: ['apple', 'orange', 'banana', 'mango', 'pear']
fruits.remove("mango")
print(fruits) # Output: ['apple', 'orange', 'banana', 'pear']
6.3.4 Tuples
A Tuple is similar to a list, but it is immutable, meaning elements cannot be
changed once assigned. Tuples are typically used for fixed collections of items.

# Creating a tuple
fruits = ("apple", "mango", "banana", "pineapple")
print(type(fruits)) # Output: <class 'tuple'>
print(len(fruits)) # Output: 4

# Accessing elements
print(fruits[0]) # Output: apple

# Combining tuples
vegetables = ("potato", "carrot", "onion", "radish")
eatables = fruits + vegetables
print(eatables) # Output: ('apple', 'mango', 'banana', 'pineapple', 'potato', 'carrot', 'onion', 'radish')
6.3.5 Dictionaries
A Dictionary is a mapping data type that associates keys with values. Keys are unique, and values
can be of any data type. Dictionaries are mutable.

# Creating a dictionary
student = {"name": "Mary", "id": "8776", "major": "CS"}
print(type(student)) # Output: <class 'dict'>
print(len(student)) # Output: 3

# Accessing values by key


print(student["name"]) # Output: Mary

# Adding a new key-value pair


student["grade"] = "A"
print(student) # Output: {'name': 'Mary', 'id': '8776', 'major': 'CS', 'grade': 'A'}

# Checking if a key exists


print("name" in student) # Output: True
print("age" in student) # Output: False
6.3.6 Type Conversions
• Python supports conversion between different data types.

# Convert to string
a = 100
str_a = str(a)
print(str_a) # Output: '100'

# Convert to integer
b = "2013"
int_b = int(b)
print(int_b) # Output: 2013

# Convert to float
float_b = float(b)
print(float_b) # Output: 2013.0
• # Convert to list
• s = "apple"
• list_s = list(s)
• print(list_s) # Output: ['a', 'p', 'p', 'l', 'e']

• # Convert to set (removes duplicates)


• x = ["mango", "apple", "banana", "mango", "banana"]
• set_x = set(x)
• print(set_x) # Output: {'apple', 'mango', 'banana'}

You might also like