Member-only story
Python Essentials: Understanding Variables and Datatypes
In Python, variables are used to store and manipulate data. They act as placeholders for values that can be accessed and modified throughout the program. Python is a dynamically typed language, which means that variables can hold values of different data types.
Definition:
A variable in Python is created with a name and assigned a value using the assignment operator (=
). The name of the variable should follow certain rules that are as follows:
#Creating a variable 'name'
name = "John"
print(name)
Rules for variables:
- Variable names must start with a letter or the underscore character (_). For example,
_count
,name
,age
. - Variable names cannot start with a number. They should begin with a letter or underscore. For example,
1number
is not a valid variable name. - Variable names can only contain alphanumeric characters (letters and numbers) and underscores. Special characters and spaces are not allowed. For example,
my_var
,count_1
,name2
. - Variable names are case-sensitive, which means that
my_var
andMy_Var
are considered as two different variables. - Variable names cannot be a keyword, as keywords are reserved words in Python that have special meanings. For example,
if
,for
,while
are keywords and cannot be used as variable names.
Other notes: Python also allows you to assign values to…