Logo
Unit 3 – Python Basics - Variables and Types

Python Basics - Variables and Types

Duration: 5 minutes

Introduction

Hello, future Pythonistas! After our initial “Hello” to Python, it’s time to dive a little further. You will learn about variables and data types, two important Python programming concepts, in this lesson.

Understanding Variables and Data Types

A variable in Python is a place to store values that we need to access and modify. While naming a variable is easy, there are some guidelines to follow:

  • Variable names must start with a letter or an underscore (_). They cannot begin with a number
  • They can contain underscores and alphanumeric characters (A-Z, 0-9, and _)
  • Variable names are case-sensitive
  • In Python, reserved words (keywords) cannot be used as variable names. Examples include for, while, if, else, import, pass, break, etc.

Python supports a variety of data types, however, the following are the most popular:

Data TypeDescriptionExample
intInteger numbers42, -17, 0
floatDecimal numbers3.14, -2.5, 1.0
strText strings”Hello”, ‘Python’
boolBoolean valuesTrue, False

Writing Your First Variables

Let’s write some Python code to declare different types of variables:

# an integer
num = 10
print(num)

# a float
pi_value = 3.14
print(pi_value)

# a string
greeting = "Hello, Python!"
print(greeting)

# a boolean
is_python_fun = True
print(is_python_fun)

Here, num, pi_value, greeting, and is_python_fun are variable names, and 10, 3.14, “Hello, Python!”, and True are their respective values. The print function is used to display the value of each variable.

Assignment

It’s your time now. Make four variables: a Boolean, a string, an integer, and a float. You may give them any name you want, assign values to them, and display the results. This will give you a practical understanding of Python data types and variables.

Conclusion

Congrats on finishing this module! You’ve mastered the fundamental programming skill of declaring and working with various kinds of variables in Python. We’ll explore more complex issues as we go along and expand on these fundamentals.

Next Tutorial: Python Basics: Operators

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!