Search This Blog

Python Data Types

  Python provides several built-in data types that allow you to store, manipulate, and organize data efficiently. Below is a quick overview of the most commonly used ones.


Core Python Data Types

  • Integers (int)
    Whole numbers without a decimal point, such as 3300, or 200.

  • Floating Point Numbers (float)
    Numbers with decimals, such as 2.34.6, or 100.0.

  • Strings (str)
    Ordered sequences of characters, such as "hello"'Sammy'"2000", or even Unicode strings like "楽しい".

  • Lists (list)
    Ordered collections that can store multiple object types:
    [10, "hello", 200.3]

  • Dictionaries (dict)
    Unordered collections of key-value pairs:
    {"mykey": "value", "name": "Frankie"}

  • Tuples (tuple)
    Ordered collections that are immutable (cannot be changed):
    (10, "hello", 200.3)

  • Sets (set)
    Unordered collections of unique elements:
    {"a", "b"}

  • Booleans (bool)
    Logical values that represent True or False.

Working with Numbers

Python mainly works with two types of numbers:

  • Integers (whole numbers)

  • Floating point numbers (decimals)

You can perform basic mathematical operations and store numeric values using variables, which makes your code easier to read and reuse.

Variable Assignment in Python

Variables allow you to give meaningful names to data so it can be reused later in your program.

Example:

my_dogs = 2

Rules for Naming Variables

  • Variable names cannot start with a number

  • No spaces are allowed—use underscores (_) instead

  • Avoid special characters like @!$, etc.

  • Use lowercase letters (PEP8 best practice)

  • Do not use reserved Python keywords like list or str

Dynamic Typing in Python

Python uses dynamic typing, meaning you can reassign variables to different data types.

Example:

my_dogs = 2

my_dogs = ["Sammy", "Frankie"]

Pros of Dynamic Typing

  • Easy and flexible to work with

  • Faster development time

Cons of Dynamic Typing

  • Can lead to unexpected bugs

  • Requires awareness of data types using type()

This flexibility is different from statically typed languages, where changing a variable’s type would cause an error.

Strings in Python

Strings are ordered sequences of characters created using single or double quotes.

'hello'

"Hello"

"I don't do that"

String Indexing and Slicing

Because strings are ordered, you can:

  • Index to retrieve a single character

  • Slice to retrieve a range of characters

Slicing syntax:

[start:stop:step]

start: where the slice begins
stop: where it ends (not included)
step: how many characters to jump

String Formatting for Printing

Often, you’ll want to insert variables into strings—this is called string interpolation.

Example:

my_name = "Jose"

print("Hello " + my_name)

Python offers cleaner methods:

  • .format() method

  • f-strings (formatted string literals)


Lists

Lists are ordered, mutable collections that can hold different data types.

[1, 2, 3, 4, 5]

Lists support indexing, slicing, nesting, and a wide range of useful built-in methods.

Dictionaries

Dictionaries store data using key-value pairs rather than positions.

Example:

{'key1': 'value1', 'key2': 'value2'}

When to Use Lists vs Dictionaries

  • Lists: Ordered data accessed by position

  • Dictionaries: Unordered data accessed by key name

Tuples

Tuples are similar to lists but are immutable, meaning their contents cannot be changed once created.

Example:

(1, 2, 3)

They are useful when you want to ensure data remains constant.

Sets

Sets are unordered collections of unique elements. Duplicate values are automatically removed.

They are useful for membership testing and eliminating duplicates.

Booleans

Booleans represent True or False values and are essential for:

  • Logic

  • Comparisons

  • Control flow (if statements, loops)

No comments:

Post a Comment

Data science Q & A

 1. What is Data Science? a. The process of designing and building computer hardware to store and manage large volumes of data b. A branch o...