1.2. Python Variables#
1.2.1. Value#
In Python, a value is any data that can be stored in a variable. It can be a number (such as an integer or a float), a string (a sequence of characters), a Boolean (True or False), or other types of data. For example
x = 5 # x is a variable that holds an integer value of 5
y = "Hello World" # y is a variable that holds a string value of "Hello World"
z = True # z is a variable that holds a Boolean value of True
1.2.2. Variable#
A variable is a container that holds a value, which is like a label or a name given to the value that is stored inside it. You can use variables to store values and then use them later in your code. You can also change the value of a variable at any time. For example:
x = 5 # x is a variable that holds an integer value of 5
x = x + 2 # x now holds the value of 7
1.2.3. String#
A string is a sequence of characters, enclosed in quotation marks. You can use strings to store text, such as words and sentences. You can also use them to display messages to the user or to create strings that hold specific data, like a name or an address. Strings are a very important data type in python and you will use it very frequently. For example:
"Hello World"
'Hello World'
1.2.4. List#
A list in Python is a collection of values stored in a single object, similar to arrays in other programming languages. Lists can contain elements of any type, including numbers, strings, and other objects.
To create a list in Python, you can use square bracket [ ] notation and include the values you want to store in the list, separated by commas.
For a beginner, it’s important to remember the following when creating lists in Python:
Lists start with a square bracket [ ]
Values in the list are separated by commas
Lists can contain elements of any type, including numbers, strings, and other objects.
# create a list
my_list = [1, 2, 3.14, "Hello", True]
# print the list
print(my_list)
[1, 2, 3.14, 'Hello', True]
Indexing in Python is a way to access specific elements in a list or array. Think of a list as a row of boxes, where each box contains a value. The index is the number assigned to each box [ ] and it allows us to locate a specific value or object. Lists in Python are zero-indexed, meaning that the first element in the list is stored at index 0, the second element is stored at index 1, and so on. For example, we an print any element in out created list by specifying the index releated:
# access elements by index
print(my_list[0]) # prints the integer 1
print(my_list[3]) # prints the string "Hello"
print (my_list[2]) # prints the float 3.14
1
Hello
3.14
1.2.5. type () of data in python#
In Python, you can use the built-in type() function to determine the type of an object. For example:
x = 5
print(type(x)) # Output: <class 'int'>
y = "hello"
print(type(y)) # Output: <class 'str'>
z = [1, 2, 3]
print(type(z)) # Output: <class 'list'>
<class 'int'>
<class 'str'>
<class 'list'>
You can also check an object’s type very simply by:
x2 = 5
type(x2) # Output: int
int
y2 = 'hello'
type(y2) # Output: str
str
z2 = [1, 2, 3]
type(z2) # Output: list
list