Introduction

In Python, a variable is a container for storing data values. Unlike other programming languages that require explicit declaration to reserve memory space, Python variables do not need explicit declaration to reserve memory. Memory allocation happens automatically when you assign a value to a variable.

Numbers, Strings, and Booleans

Numbers

Python supports various numeric types including integers, floating-point numbers, and complex numbers:

  • Integers (int) are whole numbers, positive or negative, without decimals, of unlimited magnitude.
  • Floating-point numbers (float) represent real numbers and are written with a decimal point dividing the integer and fractional parts.
  • Complex numbers (complex) are written with a “j” as the imaginary part, e.g., 1 + 2j.

Example:

  x = 3    # int
y = 3.5  # float
z = 1+2j # complex
  

Strings

Strings in Python are arrays of bytes representing Unicode characters. Python has no character data type; a single character is simply a string with a length of one. Strings are created by enclosing characters in either single quotes or double quotes.

Example:

  a = "Hello"
b = 'World'
  

String operations and slicing are important features:

  print(a + " " + b)  # Concatenation
print(a * 2)        # Repetition
print(a[1])         # Indexing
print(a[1:4])       # Slicing
  

Booleans

Booleans represent one of two values: True or False. Boolean expressions include operations like:

  print(10 > 9)        # Returns True
print(10 == 9)       # Returns False
print(10 < 9)        # Returns False
  

Type Conversion

Type conversion refers to converting one data type into another. Python provides several built-in functions that allow for explicit conversion of one data type to another, which can be very useful in data manipulation.

  • Implicit Conversion: Python automatically converts one data type to another without any user involvement.
  • Explicit Conversion: This requires the use of predefined functions like int(), float(), str(), etc.

Example:

  num_int = 123     # int
num_flo = 1.23    # float

num_new = num_int + num_flo
print("datatype of num_new:", type(num_new))  # Automatically converts int to float

num_str = "456"   # string

# Converting string to int
print("datatype of num_str before:", type(num_str))
num_str = int(num_str)
print("datatype of num_str after:", type(num_str))
  

Conclusion

Understanding variables and data types is crucial for mastering Python as they form the basis of data manipulation and functionality within any program. This guide has covered the essential aspects, from declaring variables and exploring basic data types to performing type conversions, providing you with a solid foundation for more advanced programming concepts.

Last updated 04 May 2024, 04:36 UTC . history