This part of the content will discuss the concepts and principles related to variables and objects in Python. These concepts revolve around how you store, reference, and manipulate data in a Python script.
Python Variables Are Pointers
Python variables just point to various objects. For example, x = 10 means you are essentially defining a pointer named x that points to some other bucket containing the value 10.
# assign 4 to the variable x
x = 10
#You can also assign a value to multiple variables at the same time:
x = y = z = 10
Python is dynamically typed
Variable names can point to objects of any type.
It allows objects to change their type during runtime.
x = 10# x is an integer
x = 'Hello World'# now x is a string
x = [10, 20, 30] # now x is a list
Everything Is an Object
Python is an object-oriented programming language in which everything is an object.
x = 10# Integer object
print(type(x)) # <class 'int'>
y = 3.14# Float object
print(type(y)) # <class 'float'>
# Class object
class Person:
pass
print(type(Person)) # <class 'type'>
Python Literals
In Python, literals are the basic building blocks used to represent different types of data. They are fixed values that are directly written into the code and can be assigned to variables.
Numeric Literals
Numeric literals represent numerical values in Python. They can be categorized into three main types: integers, floats, and complex numbers.
# Integer literals age = 25 # Float literals pi = 3.14 # Complex number literals complex_num = 2 + 3j
String literals
String literals represent textual data in Python. They are enclosed in either single quotes (' ') or double quotes (" ").
# String literals name = "John" message = 'Hello, World!' multiline = '''This is a multiline string.'''
Boolean literals
Boolean literals represent the truth values
True
andFalse
.# Boolean literals is_true = True is_false = False
None literals
The
None
literal represents the absence of a value or a null value. It is often used to indicate the absence of a meaningful result or the initialization of variables.result = None
Containers literals
Python also provides container literals that represent collections of values, such as lists, tuples, sets, and dictionaries.
# List literal fruits = ['apple', 'banana', 'orange'] # Tuple literal point = (10, 20) # Set literal colors = {'red', 'green', 'blue'} # Dictionary literal person = {'name': 'John', 'age': 25}
In conclusion, variables in Python act as named placeholders to store and manipulate data, while objects are the fundamental building blocks that represent data and possess associated behavior and attributes. Literals, on the other hand, are fixed values directly embedded in code, representing specific data types.