Page cover image

Variables and DataTypes

Python is a dynamically typed language, which means you don't need to declare the data type of a variable explicitly; Python will automatically infer it based on the value assigned to the variable. However, Python does have several built-in data types that you can use for various purposes. Here are some of the most commonly used data types in Python:

  1. Integers (int): Integers represent whole numbers without a decimal point. For example:

    code = 5, y = -10
  2. Floating-Point Numbers (float): Floating-point numbers represent real numbers with a decimal point. For example:

    i = 3.14159 
    temperature = 98.6
  3. Strings (str): Strings represent sequences of characters, enclosed in single (' '), double (" "), or triple (''' ''' or """ """) quotes. For example:

    pame = "Alice" 
    message = 'Hello, World!'
  4. Boolean (bool): Boolean values represent either True or False. They are often used for logical operations and comparisons. For example:

    is_student = True 
    is_adult = False
  5. Lists (list): Lists are ordered collections of items, which can be of any data type. They are defined by square brackets [] and can contain elements separated by commas. For example:

    numbers = [1, 2, 3, 4, 5] 
    fruits = ["apple", "banana", "cherry"]
  6. Tuples (tuple): Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation. They are defined using parentheses (). For example:

    coordinates = (3.5, 4.2) 
    colors = ("red", "green", "blue")
  7. Dictionaries (dict): Dictionaries are collections of key-value pairs. Each key is unique and associated with a value. They are defined using curly braces {} and colons : to separate keys and values. For example:

    person = {"name": "Alice", "age": 30, "city": "New York"}
  8. Sets (set): Sets are unordered collections of unique elements. They are defined using curly braces {} or the set() constructor. For example:

    unique_numbers = {1, 2, 3, 4, 5} 
    vowels = set("aeiou")
  9. None (NoneType): None is a special data type that represents the absence of a value or a null value. It is often used to indicate that a variable or function returns nothing. For example:

    result = None
  10. Custom Classes: In addition to the built-in data types, Python allows you to define custom classes and create objects with their own data types and methods.

These are some of the fundamental data types in Python. Python's flexibility and dynamic typing make it easy to work with various data types, and you can perform operations and conversions between them as needed in your programs.

Last updated

Was this helpful?