0% found this document useful (0 votes)
8 views42 pages

18 +Python+Data+Types

Uploaded by

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

18 +Python+Data+Types

Uploaded by

Sarvan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LEARN PYTHON PROGRAMMING – BEGINNER TO MASTER

Section: 3. Python Data Types

Lecture: 18. Python Data Types

Section 1: Lecture Summary

This lecture introduces the various fundamental data types available in Python,
emphasizing their categories and important properties. It starts with numeric types that
hold single scalar values like integers, floats, booleans, and complex numbers. Then the
focus shifts to sequence types such as lists, tuples, and strings, which are collections
supporting indexed access, differing in mutability. Sets and dictionaries are explained as
unordered collections: sets contain unique elements without indexing, while dictionaries
store key-value pairs with unique keys and maintain order. The lecture highlights the
mutability and indexing features of each data type, provides syntax examples for declaring
variables, and includes memory structure visualizations. This foundational overview sets up
for more detailed exploration later.

Section 2: Key Concepts and Explanations

1. Numeric Data Types (Scalar values — store a single value):

- int (Integer): Whole numbers without decimals, e.g., `x = 10`.

- float (Floating-point): Numbers with decimal points, e.g., `y = 19.25`.

- bool (Boolean): Logical values, either `True` or `False`. Also can be represented
numerically as `1` (True) or `0` (False). Useful for conditional expressions.

- complex: Numbers with a real and imaginary part, e.g., `c = 7 + 9j`. Supports mathematics
with complex numbers.

2. Sequence Data Types (Collections of values, index-based):

- Common property: Ordered and indexed starting from zero (`0, 1, 2,...`).
- Allow mixed data types in collection.

- list:

- Syntax: Use square brackets `[]`.

- Mutable: Values can be changed, added, or removed.

- Example: `L = [2, 4, 6, 8, 10]`.

- tuple:

- Syntax: Use round brackets `()`.

- Immutable: Cannot modify, add, or remove values after creation.

- Appears similar to a list but fixed.

- Example: `t = (9, 7, 4, 2, 5)`.

- string:

- Syntax: Enclosed in single or double quotes `''` or `""`.

- Immutable: Contents cannot be changed once created.

- Represents a sequence of characters.

- Example: `s = "python"`.

3. Set Data Type (Collection of unique values, no index):

- Stores distinct elements only, duplicates are automatically removed.

- Unordered: No guaranteed sequence.

- Mutable: Elements can be added or removed but individual elements cannot be changed
by index.

- Syntax: Curly braces `{}`.

- Example: `S = {3, 7, 2, 9, 5}` (duplicates like another `9` are ignored).

4. Dictionary Data Type (Key-value pairs):

- Stores data as unique keys associated with values.


- Keys must be unique; values can be duplicated.

- Ordered: Preserves insertion order.

- Mutable: Values for existing keys can be updated; keys cannot be duplicated.

- Syntax: Curly braces with `key: value` pairs.

- Example: `d = {'name': 'john', 'roll': 126, 'dept': 'cse'}`.

Section 3: Example Code and Use Cases

Numeric Types

List (mutable, indexed, can hold mixed types)

Tuple (immutable, indexed)

t[2] = 8 Error: cannot modify tuple

String (immutable, indexed)

s[1] = 'a' Error: strings are immutable

Set (mutable, unordered, unique elements)

Dictionary (mutable, ordered, key-value pairs)


x = 10 # int
y = 19.25 # float
b = True # bool (also can be 1/0)
c = 7 + 9j # complex (real + imaginary part)

L = [2, 4, 6, 8, 10]
print(L[0]) # Access by index: prints 2
L[3] = 7 # Modify element at index 3
[Link](12) # Add element at end
print(L)

t = (9, 7, 4, 2, 5)
print(t[2]) # Access by index: prints 4

s = "python"
print(s[1]) # Access by index: prints 'y'

S = {3, 7, 2, 9, 5, 9} # Duplicate 9 ignored


print(S) # Order not guaranteed
[Link](8) # Add element
[Link](7) # Remove element
print(S)

d = {'name': 'john', 'roll': 126, 'dept': 'cse'}


print(d['name']) # Access value by key: 'john'
d['name'] = 'kate' # Modify value for key 'name'
print(d)

Each code block demonstrates declaration, access, and modification behavior consistent
with the characteristics explained.

Section 4: Key Takeaways

- Python data types are categorized as numeric, sequence, set, and dictionary types.

- Numeric types hold single scalar values: int, float, bool, complex.

- Sequence types (list, tuple, string) support indexed access; lists are mutable, tuples and
strings are immutable.

- Sets hold unordered collections of unique elements, mutable but no indexing.

- Dictionaries store ordered collections of key-value pairs with unique keys; keys are
immutable but values can be updated.

- Understanding mutability, indexing, and ordering is crucial to using these types effectively.

- Syntax conventions:

- Lists: square brackets `[]`

- Tuples: parentheses `()`

- Strings: quotes `''` or `""`

- Sets and Dictionaries: curly braces `{}`, with dictionaries requiring key:value pairs.

- This foundational introduction prepares for deeper exploration of these data types in
Python programming.
Lecture in Slides
Please Note: This document presents a curated selection of slides from the lecture; others have been excluded for conciseness.

You might also like