How to assign a variable in Python?

Jeffery 165 Published: 08/01/2024

How to assign a variable in Python?

I'm happy to help you with that! However, I must remind you that I'll be responding in English as per the rules set by your grandma (or whoever it is).

Now, let's get to the topic at hand!

In Python, assigning a variable is quite straightforward. You can assign a value to a variable using the assignment operator (=). Here are some examples:

# Assigning an integer value

x = 5

Assigning a string value

name = "John"

Assigning a floating-point number

pi = 3.14

Assigning a boolean value (True or False)

isAdmin = True

Assigning a list (or array in other languages)

my_list = [1, 2, 3]

Assigning a dictionary (or hash in other languages)

my_dict = {"name": "John", "age": 30}

In the above examples, we're assigning values to variables. The variable name is on the left side of the assignment operator (=), and the value being assigned is on the right side.

Python is a dynamically-typed language, which means you don't need to specify the data type of the variable when you declare it. Python will automatically determine the data type based on the value assigned to the variable.

For example:

x = "Hello"  # x is now a string

y = 5 # y is now an integer

print(type(x)) # Output: <class 'str'>

print(type(y)) # Output: <class 'int'>

As you can see, the type() function returns the data type of the variable. Python automatically determined that x is a string and y is an integer based on the values assigned to them.

Remember, in Python, you don't need to declare the data type before assigning a value to a variable. You simply assign the value using the assignment operator (=), and Python will take care of the rest!

So, that's it! That's how you assign variables in Python. It's easy peasy, right?

Python variables Exercises

I'd be happy to help! Here are some exercises on Python variables:

Exercise 1:

Write a Python program that declares and initializes three integer variables: x, y, and z. Assign values of 10, 20, and 30 to these variables respectively. Then, print out the sum of these three variables.

Solution:

x = 10

y = 20

z = 30

sum = x + y + z

print(sum) # Output: 60

Exercise 2:

Create a Python program that declares and initializes two string variables: name and age. Assign values of "John" and "25" to these variables respectively. Then, print out the concatenated strings using the + operator.

Solution:

name = "John"

age = "25"

print(name + " is " + age + " years old.") # Output: John is 25 years old.

Exercise 3:

Write a Python program that declares and initializes two floating-point variables: pi and radius. Assign values of 3.14 and 5.0 to these variables respectively. Then, calculate the area of a circle using the formula pi * radius^2 and print it out.

Solution:

import math

pi = 3.14

radius = 5.0

area = pi * (radius ** 2)

print(area) # Output: approximately 78.54

Exercise 4:

Create a Python program that declares and initializes an empty list fruits with five elements: "apple", "banana", "cherry", "date", and "elderberry". Then, print out the elements of the list.

Solution:

fruits = ["apple", "banana", "cherry", "date", "elderberry"]

for fruit in fruits:

print(fruit) # Output: apple banana cherry date elderberry

Exercise 5:

Write a Python program that declares and initializes a dictionary person with three key-value pairs: "name" -> "John", "age" -> 25, and "city" -> "New York". Then, print out the values of the dictionary.

Solution:

person = {"name": "John", "age": 25, "city": "New York"}

for k, v in person.items():

print(f"{k}: {v}") # Output: name: John age: 25 city: New York

These exercises cover basic data types such as integers, strings, and floating-point numbers, as well as more advanced concepts like lists and dictionaries. By practicing these exercises, you can improve your understanding of Python variables and develop your coding skills. Happy coding!