top of page
Writer's pictureStrofl

Python Beginner Guides | Variables - Qpidi

Welcome to the beginner's guide on Python variables! In this article, we'll explore what variables are, how to use them in Python, and get hands-on with some examples and exercises to cement your understanding.


Python Beginner Guides - Variables
Python Beginner Guides

Python Beginner Guide Introduction to Variables

Think of a variable as a labeled jar where you can store some data. In Python, a variable is a reserved memory location that holds values. It's essentially a name attached to a particular object, and you can use this name to access the value stored in the variable.



In the above code, x and y are variables that hold the values 10 and "Hello". When creating a variable in Python, follow these naming rules.


  • A variable name must start with a letter or the underscore character.

  • A variable name cannot start with a number.

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).

  • Variable names are case-sensitive. name and Name are two different variables.

Here's an example of how to name a variable correctly.



1- Variables Data Types


In Python, there are different types of data types, including.


  • Integers: Whole numbers, positive or negative. Example: age = 21

  • Floats: Real numbers or floating-point numbers. Example: pi = 3.14159

  • Strings: Sequence of characters. Example: greeting = "Hello World!"

  • Booleans: True or False. Example: is_student = True


2- Variables Operations


You can perform different operations on variables in Python. Here are some examples.



Exercises for New Learners

Now, let's try some exercises to test your understanding of Python variables.


Exercises for New Learners
Exercises for New Learners

Exercise- 1

Create a variable temperature and assign it a float value of 98.6. Print out the value of temperature.


Answer


Exercise- 2

Create two variables, first_name and last_name. Assign your first name to first_name and your last name to last_name. Create a third variable full_name by concatenating first_name and last_name with a space in between. Print full_name.


Answer


Exercise- 3

Create two variables num1 and num2. Assign 10 to num1 and 5 to num2. Perform addition, subtraction, multiplication, and division on these variables and print the results.


Answer


Conclusion

Congratulations! You now understand the basics of variables in Python. Practice these concepts regularly, apply them to different problems, and you'll find yourself becoming proficient in Python programming. Happy coding!


10 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page