Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Understand Variables in Python and the Main Types of Variables

    What will we cover?

    You will learn what a Python variable is. How you should name the variables, and finally, the main types of variables in Python.

    In this tutorial, you will:

    • Understand Python Variables: Learn the concept of variables in Python, which are used to store and manipulate data.
    • Master Variable Naming Conventions: Discover best practices for naming variables in Python, including guidelines for choosing meaningful and descriptive names.
    • Explore Main Types of Variables: Gain knowledge about the main types of variables in Python, such as integers, floats, strings, booleans, and more.

    By the end of the tutorial, you will have a solid understanding of Python variables, including their purpose, naming conventions, and the different types available. This knowledge will enable you to effectively work with data and utilize variables in your Python programs.

    Watch tutorial

    Step 1: What is a Python variable?

    A variable can be defined as follows.

    • A variable is a reserved memory location.
    • A variable in a python program gives data to the computer for processing.

    When you work with data, you need somewhere to keep it. That is what variables are used for.

    A simple example of a variable can be demonstrated as follows.

    s = input('What is your name')
    print('Hi', s)
    

    Here, s, is a variable. It takes input from the user and stores it there. Then it prints the value of the variable on the next line.

    Notice that we used similar variables in our first lesson.

    In Python, we have the following basic types.

    • Integers
    • Floats
    • Strings
    • Boolean

    We will work more with them later – so don’t worry that you do not understand them yet.

    Step 2: Naming convention of Variables in Python

    A variable name in Python should fulfill the following

    • Use lowercase
    • Separate words with an underscore.

    A variable name in Python can be

    • A single lowercase letter
    • A lowercase word
    • Or lowercase words separated by an underscore.

    Examples of variable names

    • x
    • i
    • cars
    • my_cars

    Note that other programming languages can have other naming conventions.

    You can declare variables as follows.

    s = "This is a string"
    a = 2
    b = 2.4
    

    And get the type of a variable as follows.

    type(b)
    

    Step 3: Python type Integer

    An integer is a number that is not a fraction; a whole number.

    Examples of integers: 

    • -23
    • -5
    • 0
    • 1
    • 78
    • 1,980,350

    Examples of non-integers: 

    • 3.14
    • 0.001
    • 9,999.999

    Try the following.

    a = 2
    b = 3
    

    Then you can make arithmetic operations with them.

    c = a + b
    

    Now c is an integer with the value of 5.

    Not convinced? Try to get the type as follows.

    type(a + b)
    

    Step 4: Python type Float

    A float is a number with digits on both sides of a decimal point. This is in contrast to the integer data type, which houses an integer or whole number.

    Examples of floats:

    • 3.13
    • 9.99
    • 0.0001

    Note that a float data type in Python can represent a whole number.

    • 1.0
    • 3.0

    An example could be.

    a = 1.1
    b = 2.3
    

    You can also make arithmetic operations.

    c = a + b
    

    Then c will be a float.

    Funny enough, if you have a float and add an integer, then you get a float. Let’s try.

    d = 1
    e = 1.1
    f = d + e
    

    Try to take the type of all of them to make sure they are the expected types.

    type(d), type(e), type(f)
    

    Notice you can comma-separate statements like that, and it will output it in a comma-separated line.

    Step 5: A few useful Math functions

    When working with integers and floats it can be handy with a few helpful mathematical functions.

    Here we will look at.

    • abs() Returns absolute value of a number
    • pow() Raises a number to a power
    • round() Rounds a floating-point value

    The abs() function returns the absolute value of an integer or float.

    a = -2
    abs(a)
    

    The pow() returns the number to a power.

    pow(2, 3)
    

    Notice that this can also be accomplished as follows.

    2**3
    

    Finally, round() is a very useful function used all the time. It rounds the value of a float.

    f = 1.234456778
    round(f, 4)
    

    Where the second argument of round is the precision after the decimal point.

    Step 6: String in Python

    A string in Python is a sequence of characters.

    Examples of strings in Python include the following.

    • “I am a string!”
    • ‘I am another string’

    That is, you can both use single and double-quoted strings. The standard is to use single-quoted strings but double-quoted are used in special cases.

    • “I’m a double-quoted string”

    As you see it can have a quote inside the string.

    That said, many, myself included, use single and double-quoted strings inconsistently.

    s1 = 'I am a string'
    s2 = "I am another string"
    

    Given the two strings above you can use addition to concatenate them.

    s1 + s2
    

    Yes, that is right. You can add strings together. But you cannot add a string and an integer or float.

    Step 7: Print Strings, Integers, and Floats variables with Python

    The easiest way to print variables of different types in Python is to comma separate them.

    a = 1
    b = 1.1
    c = "I am a string"
    print(a, b, c)
    

    This will work fine, while something like print(a + b + c) will fail.

    A great way to print formatted output is as follows.

    a = 1
    print(f'I am a string with an int here {a + 2} - and more text')
    

    Hence, if you put an f in front of the quoted string, then you can add {} with integer or float statements – see it can be a variable or an arithmetic expression.

    Step 8: Boolean variables in Python

    If you are new to programming, then the boolean expressions can be quite difficult to understand. Or actually not. Most people think they get them, but do not understand the full content of the power of boolean expression.

    Actually, boolean expressions are what make programming possible. Boolean expressions take simple scripts that do the exact same every time they are executed to full programs, which behave differently depending on input and context.

    So what are boolean values?

    • True or False (case sensitive)

    And a boolean expression is something that evaluates to either True or False.

    That said, everything in your life is the same.

    Say, every morning you look out the window to see if it rains. If it rains you prepare yourself for that. If not, well, you enjoy it not raining.

    Some object to this. They say it is not always easy to say if it rains or not. Well, you can then break it down to, whether you bring your umbrella or not.

    • Either it rains or not -> Either you take your umbrella or not

    Somehow all your decisions can be translated into a boolean expression.

    But let’s get started simply.

    b = 10 > 5
    

    What? Well, you assign the boolean expression to 10 > 5. If 10 is larger than 5, then it will be True.

    Check the value in b, it is True.

    How can this be powerful, you might ask?

    Well, when we look at conditional flows, you will be able to make powerful programs that can do whatever you want them to.

    Step 9: What next?

    I am happy you asked. You can get another lesson here where you will learn about type conversion in Python.

    But if this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.

    The course is structured with the following resources to improve your learning experience.

    • 17 video lessons teaching you everything you need to know to get started with Python.
    • 34 Jupyter Notebooks with lesson code and projects.
    • A FREE eBook┬áto support your Python learning.

    See the full FREE course page here.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment