Skip to main content

Command Palette

Search for a command to run...

Chapter 1: Introduction to Basic Python Programming - Variables and Datatypes

Published
4 min read
Chapter 1: Introduction to Basic Python Programming - Variables and Datatypes
T

Data & AI Transformation Lead • 15+ years of experience in AI-driven Quality Engineering, Data Analytics, and Test Automation. • Expertise in AI/ML, Data Testing, Cloud Automation, ETL Pipelines, and Observability tools. • Hands-on leader in AI-driven automation, predictive analytics, and cloud data validation. • Strong technical proficiency in Snowflake, DBT, Power BI, Python, AWS, SQL, and MLOps pipelines. • Proven success in reducing defects, optimizing performance, and streamlining data workflows. • Expertise in integrating security into CI/CD pipelines, automated security scanning, vulnerability assessment, and compliance-driven quality engineering. • Expertise in JMeter, BlazeMeter, Snowflake SQL Profiler, optimizing application & data performance testing for high-scale environments. • Strong expertise in SAFe, Scrum, and Kanban methodologies, ensuring seamless collaboration between development, testing, and operations teams.

Excercise 1 :

  • Create a variable monthly_savings, equal to 10 and num_months, equal to 4.

  • Multiply monthly_savings by num_months and assign it to new_savings.

  • Print the value of new_savings.

    In Python, semicolons at the end of a statement are optional and usually omitted unless multiple statements are written on a single line.

Excercise 2:

CategoryData TypeLayman ExplanationExampleMax ValueMin Value
NumericintWhole numbers without decimal pointsx = 10No fixed limit (limited by system memory)No fixed limit (negative)
NumericfloatNumbers with decimal pointsy = 3.141.8e308-1.8e308
NumericcomplexNumbers with real and imaginary partsz = 2 + 3jDepends on float partDepends on float part
BooleanboolEither True or Falseflag = True1 (True)0 (False)
TextstrSequence of characters or textname = "Alice"No fixed limit0 (empty string)
SequencelistOrdered collection you can changenumbers = [1, 2, 3]No fixed limit0 elements
SequencetupleOrdered collection you cannot changecoords = (5, 10)No fixed limit0 elements
SequencerangeRepresents a range of numbersr = range(5)Depends on range bounds0-length range
SetsetUnordered collection of unique valuess = {1, 2, 3}No fixed limit0 elements
SetfrozensetImmutable version of setfs = frozenset([1, 2, 3])No fixed limit0 elements
MappingdictKey-value pairs like a dictionaryperson = {"name": "Bob"}No fixed limit0 pairs
BinarybytesImmutable sequence of bytes (e.g., images, files)b = b"hello"Depends on system memory0 bytes
BinarybytearrayMutable sequence of bytesba = bytearray([65, 66])Depends on system memory0 bytes
BinarymemoryviewView over binary data for efficiencymv = memoryview(b"hello")Depends on buffer0-length view
NoneNoneTypeRepresents no value or empty statex = NoneOnly one value: NoneOnly one value: None
CustomclassUser-defined type (custom object)class Car: passDepends on classDepends on class

🔹 1. Dynamic Typing

  • Python is dynamically typed, meaning you don’t need to declare data types.

      x = 5     # int
      x = "Hi"  # now it's a str!
    

🔹 2. Mutability

Data TypeMutable?
list✅ Yes
dict✅ Yes
set✅ Yes
str❌ No
tuple❌ No
frozenset❌ No
bytes❌ No
bytearray✅ Yes

🧠 Why it matters: Mutable types can be changed after creation, affecting performance and behavior when passed into functions.


🔹 3. Type Conversion

  • You can convert between types easily:

      int("5")         # 5
      str(100)         # "100"
      list("hello")    # ['h', 'e', 'l', 'l', 'o']
    

🔹 4. Memory Efficiency

  • tuple is more memory-efficient than list.

  • Use frozenset when you don’t want the set to change.

  • range() is memory-efficient for large loops as it generates numbers on demand (lazy evaluation).


🔹 5. NoneType Usage

  • None is used for default values in functions or to represent “no value”.

      def greet(name=None):
          if name:
              print("Hi", name)
          else:
              print("Hi there!")
    

🔹 6. Identity vs Equality

  • Use == to compare values, is to check identity (same memory location).

      a = [1, 2]
      b = a
      a == b  # True
      a is b  # True
    

🔹 7. Custom Types

  • You can define your own data types using class:

      class Car:
          def __init__(self, brand):
              self.brand = brand