Variables and Data Types of Python by Black Box AI | Ballu Python 835430


 BLACKBOXAI

In Python, variables are used to store data values, and data types define the type of data that can be stored in a variable. Python is dynamically typed, meaning you don't need to declare the type of a variable when you create it. Here’s a detailed overview of variables and data types in Python:

Variables

  • Definition: A variable is a name that refers to a value. You can think of it as a container for storing data.

  • Naming Rules:

    • Must start with a letter (a-z, A-Z) or an underscore (_).
    • Can be followed by letters, numbers (0-9), or underscores.
    • Case-sensitive (e.g., myVar and myvar are different variables).
    • Cannot be a keyword (reserved word in Python).
  • Example:

    python
    1age = 25 2name = "Alice" 3is_student = True

Data Types

Python has several built-in data types, which can be categorized into the following main types:

1. Numeric Types

  • int: Represents integer values (whole numbers).

    • Example:
      python
      1x = 10 2y = -5
  • float: Represents floating-point numbers (decimal values).

    • Example:
      python
      1pi = 3.14 2temperature = -10.5
  • complex: Represents complex numbers, which have a real and an imaginary part.

    • Example:
      python
      1z = 2 + 3j # 2 is the real part, 3 is the imaginary part

2. Sequence Types

  • str: Represents strings, which are sequences of characters.

    • Example:
      python
      1greeting = "Hello, World!"
  • list: Represents an ordered, mutable collection of items. Lists can contain mixed data types.

    • Example:
      python
      1fruits = ["apple", "banana", "cherry"] 2numbers = [1, 2, 3, 4.5, "five"]
  • tuple: Represents an ordered, immutable collection of items. Like lists, tuples can also contain mixed data types.

    • Example:
      python
      1coordinates = (10.0, 20.0)

3. Mapping Type

  • dict: Represents a collection of key-value pairs. Dictionaries are unordered and mutable.
    • Example:
      python
      1student = { 2 "name": "Alice", 3 "age": 25, 4 "is_student": True 5}

4. Set Types

  • set: Represents an unordered collection of unique items. Sets are mutable.

    • Example:
      python
      1unique_numbers = {1, 2, 3, 4, 5}
  • frozenset: Represents an immutable version of a set.

    • Example:
      python
      1immutable_set = frozenset([1, 2, 3, 4, 5])

5. Boolean Type

  • bool: Represents Boolean values, which can be either True or False.
    • Example:
      python
      1is_active = True 2is_logged_in = False

Type Checking and Conversion

  • Checking Data Type: You can check the type of a variable using the type() function.

    python
    1print(type(age)) # Output: <class 'int'> 2print(type(name)) # Output: <class 'str'>
  • Type Conversion: You can convert between different data types using built-in functions like int()float()str(), etc.

    python
    1num_str = "123" 2num_int = int(num_str) # Converts string to integer 3print(num_int) # Output: 123

Summary

In summary, Python provides a variety of data types to handle different kinds of data, and variables are used to store these data types. Understanding how to use variables and data types is fundamental to programming in Python.

Comments

Popular posts from this blog

Lession 2: Control Structure in Python by BlackBox AI

Basic Syntax Of Python by Chat GPT | Ballu Python 835430

Day 1: ChatGPT Python Lession | Ballu Python 835430