Taking a step further into Python, today we'll be covering variables and data types in Python. You'll learn along with examples and at the end of the tutorial you can execute every statement, I'll mention in this tutorial.
Keep your eyes open and pay attention. Let's start.
1. Variables
2. Data Types
- Integer
- Float
- Boolean
- String
1. Variables
Data is everything in programming, whether we input something or get something as output. Data is stored in computer memory and has an address, which can be accessed in our program by referring to the memory location.
# every data we use in our program does occupy memory in our machine.
>>>print("Neeraj")
>>>print(10)
Neeraj
10
If we want to use data once in our program then this direct approach is good, but when we have to use it multiple times, then we need a simpler approach. That approach is wrapping the data with a name, and we call that name a variable name.
So we can say that variable is a name that refers to a memory address which contains some known or unknown value.
In our example, we can also declare a variable, shown below.
# variable declaration
>>>name = "Neeraj"
>>>age = 21
Here, we have assigned two variables named "name" and "age", which contain the name and age values respectively.
Declaring a Variable
The syntax for declaring a variable in Python is variable_name = value.
The equals sign is known as assignment operator which assigns and binds the value on the Right Hand Side to Left Hand Side.
When we write
author_name = "Neeraj"
this means "author_name" variable holds the value "Neeraj". Whenever in our code we'll use author_name, the compiler will return its value. We can see this in the following line of code.
>>>print(author_name)
NeerajRules for Variable Name
-> Names can not start with a number but can end or contain a number
-> Names can not contain space or any of these symbols
:'\",<>/?|\\!@#%^&*~-+
-> It's considered best practice that names are lowercase with underscores
-> Avoid using Python built-in keywords like `list` and `str`
# Wrong variable name
0name = "Neeraj"
# Correct variable name
name0 = "Neeraj"
nam0e = "Neeraj"
-> Names can not contain spaces, use _ instead # if you want to add a number in the start of the variable name to it like this
_0name = "Neeraj"
author_name = "Neeraj"-> Names can not contain space or any of these symbols
:'\",<>/?|\\!@#%^&*~-+
# Wrong variable name
author name = "Neeraj"
author$name = "Neeraj"-> It's considered best practice that names are lowercase with underscores
# When using long variable names use _ to make it readable
author_name = "Neeraj"-> Avoid using Python built-in keywords like `list` and `str`
2. Data Types
Like every programming language, Python does have categorized data into some categories known as its Data Types.
-> Integer -int
-> Float -float
-> Complex -complex
-> Boolean -bool
-> String -str
Integer
Every numeric value in Python is stored as an Integer. Python has an advantage over other programming languages dealing with Integers, that is, an integer does not have any range in Python.
An integer variable can store a number of any length.
# All are valid declaration of Integer.
>>>num1 = 0
>>>num2 = 985471254688794115616165
>>>num3 = -95874
Float
Integer cannot store decimal values, to do that, we have Floated.
# decimal numbers are stored as float
>>>floating_number = 10.002
Complex
Python has another advantage over other programming languages that it supports complex numbers.
#declaring a complex number
>>>complex_number = 10 + 5j
Boolean
This datatype either store True or False.
#Boolean example
>>>boolean_1 = True
>>>boolean_2 = False
String
A string is a combination of alphanumeric characters and can also contain space and special characters. Strings in Python are always enclosed within Double Quotes(" ") or Single Quotes(' ').
#Declaring Strings
>>>name = "Neeraj"
>>>country = "India"
>>>random_text = "India has 28 states and 8 UT's."
With this, I wrap up today's class, hope you learned something new today. You can practice what you have just learned in the Python IDE below.
Thank You



0 Comments