#4 Variables

Previous post:  Next post: https://codinghelp.site/knowledgebase/python/5-if-statement/ The “variables” Variables in python are used to store data-values throughout the code. They will not have the same value once we stop running the code. Their values are “per session”. The variables syntax In python, there’s no specific command for declaring a variable. They’re created once a value is assigned to them. They do not need to be declared by a specific type, if we do not specify the type python will convert them to an appropriate data-type (you will learn data-types later on).
Here, we are assigning ammar to my_name variable. Output of this code would be ammar.
These two are the same. Python will convert both to a string (datatype, you’ll learn in the next tutorial dw).
This is called variable overwriting. When we print the variable foo (in the same indent), the output will be hey 2 because we replaced the value of the variable.
Variables are case-sensitive. Variable “ A “ and variable “ a “ are completely different variables with different values. In python, there are illegal variable names as well. Here are basic variable rules: -variables may not start with a number -variables may not have a space in them -variables may not be named as the built-in functions (shadowing) -variables may not be named as the python syntax  -variables may not have operators in them  Here is a list of some things you can’t include in your variables:
To get the data-type of a variable, you can use the built-in function type()
The output would tell us that the type of the variable here is a string. Example
The output of this code would tell us that variable my_name is a string and its value, ammar. Exercise Write a variable and set its value to “hello” then print it. Afterward, print the type of that variable. Answer key image

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.