DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


Chapter 6 - Data Types & Variables

Chapter 6 - Data Types & Variables

  • 3 minutes read
  • 164 Views
Chapter 6 - Data Types & Variables

    Table of Contents

Variables and Identifiers in Python

If we go by the dictionary meaning ‘Variable’ is something which is ‘able to be changed or adapted’. Which is true to much extent in terms of Python programming language also. Variable is basically a reference to the memory location where an object is stored by a computer program. The names which we use for the variables and even for the programs/classes etc. are called identifiers and there are certain rules which we must follow while naming such identifiers:-

  1. They can be of any length.
  2. They can contain any number of uppercase alphabets(A-Z),lowercase alphabets(a-z), digits(0-9) and underscore(_).
  3. However they must start with an alphabet or an underscore i.e. we can not use the digits at the very beginning e.g. ‘_a_valiable’ and ‘onevariable’ are allowed, but ‘1variable’ is not allowed.
  4. The identifiers are case sensitive i.e. ‘a_variable’ and ‘A_variable’ are different identifiers.

SaralGyaan_learn_python_for_free.png

  1. There are certain words which are reserved for special use in Python. Such words are called keywords. Identifiers can not be keywords. The following image contains the list of all the keywords. You needn’t cram all of them. Open an iteractive shell and type following code to get the list of keywords:-
help()

keywords

SaralGyaan_learn_python_for_free_beginners.png

Assigning values to the Variables

Unlike C and C++ you do not need to declare the variables and its data type at the very beginning. If you need a variable at any time in your program, just think of a name and assign a value to it and start using it and the interpreter will automatically detect the data-type e.g.

a_variable = "hello"

Watch our one minute video on variables and identifiers in python

One thing you need to note here is that the equal”=” sign should not be interpreted as “is equal to” however it should be read as “is set to” because we use “==” as a comparison operator to see the value is equal to something.

You can assign values to single variable or mutliple variable at a time.

x = "single assignment"

y,z = "multiple assignments 1", "mutliple assignments 2"

print(x)
print(y)
print(z)

Output will be:-

single assignment
multiple assignments 1
mutliple assignments 2

Another salient feature of the variable in Python is that its value as well as data-type can be changed at any time in the program. In the below example, the variable is first set to 28 then changed to 29 and thereafter to 29.1 (which also changed its type to float) and finally it was changed into a string with value ‘a_string’.

SaralGyaan_python_variables_data_types.png

Datatypes in Python

The standard Data-Types of Python are:-

  1. Numbers - They can be integer, long integer (Python 2.7 only. They have been discontinued in Python 3), float or complex number.
  2. Stings
  3. Lists
  4. Tuples
  5. Dictionaries
  6. Boolean - True and False.

We will be learning more about each data-type in the upcoming lessons.

Table of Contents

Chapter 5 - Indentation

Chapter 7- Numbers, Conversions and Operators


Related Posts

If __name__ == '__main__' in Python
By Udit Vashisht

What does if __name__ == ‘__main__‘in python means?

In python, you must have come accross the following code :-

if __name__ == '__main__': main() 

In this tutorial, we will learn in detail what does the above statement means. As per official documentation:

‘__main__‘ is the name of the ...

Read More
Python unittest module - How to test your python code?
By Udit Vashisht

Python unittest module

Why to unit test your python source code?

All programmers want their code to be impeccable, but as the saying goes, to err is human, we make mistakes and leave bugs in our source code. Here is where the unit testing comes to our rescue. If you ...

Read More
Chapter 3 - Executing a Python Script
By Udit Vashisht

How to run a Python script?

In our previous chapter, we have strongly recommended you to ditch IDLE and use some code-editor and terminal(MacOS/Linux) or Powershell(Windows) to write and execute your python script. In this chapter we will tell you how to do that.

First and foremost for the ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python