Course Five - Transformation models in Python
In Python, variables are reserved locations in memory used to store values. When you create a variable, Python automatically allocates memory and determines what kind of data it can hold, such as integers, floating-point numbers, or strings.
Assigning Values to Variables
Variables in Python do not need explicit declaration. Memory is allocated automatically when a value is assigned using the =
operator.
# Python example
counter = 100 # Integer assignment
miles = 1000.0 # Floating-point assignment
name = "John" # String assignment
print(counter)
print(miles)
print(name)
Output:
100
1000.0
John
Multiple Assignment
Python allows assigning the same value to multiple variables:
a = b = c = 1
You can also assign different values to multiple variables at once:
a, b, c = 1, 2, "John"
Standard Data Types
Python supports several standard data types that define how values are stored and manipulated:
-
Numbers
-
Strings
-
Lists
-
Tuples
-
Dictionaries
Numbers in Python
Numbers are stored as numeric objects when assigned:
var1 = 1
var2 = 10
You can delete numeric variables using del
:
del var1
del var_a, var_b
Python supports four numeric types:
Type | Description | Example |
---|---|---|
int | Integer values | 10 |
long | Long integers | 51924361L |
float | Floating-point numbers | 15.20 |
complex | Complex numbers | 3.14j |
Note: Python allows lowercase
l
for long integers, but uppercaseL
is preferred to avoid confusion with1
.
Strings in Python
Strings are sequences of characters enclosed in single ('
) or double ("
) quotes. Multi-line strings use triple quotes (""" """
).
str1 = 'Hello World!'
print(str1) # Complete string
print(str1[0]) # First character
print(str1[2:5]) # Characters from index 2 to 4
print(str1[2:]) # From index 2 to end
print(str1 * 2) # Repeat string
print(str1 + " TEST") # Concatenate
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World! TEST
Lists in Python
Lists are ordered collections of elements enclosed in square brackets []
. Elements can be of different types, unlike arrays in languages like C.
list1 = ['abcd', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
print(list1) # Full list
print(list1[0]) # First element
print(list1[1:3]) # Elements from 2nd to 3rd
print(list1[2:]) # From 3rd element to end
print(tinylist * 2) # Repeat list
print(list1 + tinylist) # Concatenate lists
Output:
['abcd', 786, 2.23, 'john', 70.2]
'abcd'
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Tuples in Python
Tuples are similar to lists but are immutable and are enclosed in parentheses ()
. They store ordered data and can contain multiple types of values.
tuple1 = (123, "John", 3.14)
print(tuple1)
Output:
(123, 'John', 3.14)
This post covers Python variables, standard data types, and basic operations on numbers, strings, lists, and tuples. These are the foundation of Python programming and crucial for creating more complex programs.
Comments
Post a Comment