Variables in python

Python variables
Python variables | kitoinnovations

Variables in the Python Programming Language

In this topic, we’ll show you the different variables in python and how it is used.

In the previous topic, we talked about indentation in python, how to use indentation. Click here to read the previous topic.

What is a variable?
A variable is the location in memory where our values are stored. It is a container for our values.
For example: name = “favor”, age = 18, year = “2024”
These are variables.
In Python, variables are created when you assign a value to it. Python has no command for declaring a variable. A variable can be changed even after declaring the variable

Data types in python:

We have different data types in python,

  • Integers
  • Strings
  • Float
  • Booleans

An Integer is a whole number, example: 2, 84, 100, 7837, etc.

A String is a a sequence of characters enclosed in either single quotes (”) or double quotes (“”). Example: “genetics”, “category”, ‘human’, ‘specific’, etc.

A float is a decimal number, example: 2.2, 5.6, 0.1, 0.4242, 8494.38, etc.

A Boolean are true or false statements, example: True, False.]

Printing Variables:

name = “favor”
age = 18
year = 2024
height = ‘6ft’

print(name)
print(age)
print(year)
print(height)

CASTING

Casting is used to specify the data type of a variable.

For example:

name = int(favor)
age = float(18)
year = str(2024)
height = int(6ft)

How to get the data type of a variable:

You can get the data type of a variable with the type() function.

For example:

name = “favor”
age = 18
year = 2024
height = ‘6ft’

print(type(name))
print(type(age))
print(type(year))
print(type(height))

Case-Sensitive:

You should know that variable names are case sensitive:

For example:

x = 9
X = 9

x will not overwrite X, because they are different. x is a different variable from X.

 

Commenting in Python:

What are comments?

Comments are used to explain python codes. They are used to prevent execution when running the code.

Comments makes the python code more readable.

How to create a python comment:

Python comment starts with a #
Once a code starts with # , python would ignore the code.

For example:

# this is a comment

Comments can also be placed at the end of a line and python would ignore the rest of the line.

For example:

print(“Hello World”) # this is a comment

Multiple line comments:

To comment multiple lines in python, insert in each line of the code and python would ignore the code, since python doesn’t really have a syntax for multiple line comment.

But, for an easier way, you can highlight the lines that you want to comment and press ctrl+/

For example:

# this is a multiple line comment
# simply insert # in each line of code
# and python would ignore the code

You can also used a multiple line string, since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it. If the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.

For example:

“””
this is a multiple line comment
add triple quotes in your code
and place your comment inside it
“””

 

1 comment
Leave a Reply

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

You May Also Like