Storing information using variables
Computers are useful for two purposes: storing information (also known as data) and performing
operations on stored data. While working with a programming language such as Python, data is
stored in variables. You can think of variables are containers for storing data. The data stored within
a variable is called its value. Creating variables in Python is pretty easy, as we've already seen in
the previous tutorial.
A variable is created using an assignment statement. It begins with the variable's name, followed by
the assignment operator = followed by the value to be stored within the variable. Note that the
assignment operator = is different from the equality comparison operator ==.
You can also assign values to multiple variables in a single statement by separating the variable
names and values with commas.
You can assign the same value to multiple variables by chaining multiple assignment operations
within a single statement.
,You can change the value stored within a variable by assigning a new value to it using another
assignment statement. Be careful while reassigning variables: when you assign a new value to the
variable, the old value is lost and no longer accessible.
The pattern var = var op something (where op is an arithmetic operator like +, -, *, /) is very
common, so Python provides a shorthand syntax for it.
Variable names can be short (a, x, y, etc.) or descriptive
( my_favorite_color, profit_margin, the_3_musketeers, etc.). However, you must follow
these rules while naming Python variables:
A variable's name must start with a letter or the underscore character _. It cannot begin with
a number.
A variable name can only contain lowercase (small) or uppercase (capital) letters, digits, or
underscores (a-z, A-Z, 0-9, and _).
Variable names are case-sensitive, i.e., a_variable, A_Variable, and A_VARIABLE are all
different variables.
Here are some valid variable names:
, Let's try creating some variables with invalid names. Python prints a syntax error if your variable's
name is invalid.
Syntax: The syntax of a programming language refers to the rules that govern the structure of a valid
instruction or statement. If a statement does not follow these rules, Python stops execution and
informs you that there is a syntax error. You can think of syntax as the rules of grammar for a
programming language.
Built-in data types in Python
Any data or information stored within a Python variable has a type. You can view the type of data
stored within a variable using the type function.
Python has several built-in data types for storing different kinds of information in variables.
Following are some commonly used data types:
1. Integer
2. Float
3. Boolean
4. None
5. String
6. List
Computers are useful for two purposes: storing information (also known as data) and performing
operations on stored data. While working with a programming language such as Python, data is
stored in variables. You can think of variables are containers for storing data. The data stored within
a variable is called its value. Creating variables in Python is pretty easy, as we've already seen in
the previous tutorial.
A variable is created using an assignment statement. It begins with the variable's name, followed by
the assignment operator = followed by the value to be stored within the variable. Note that the
assignment operator = is different from the equality comparison operator ==.
You can also assign values to multiple variables in a single statement by separating the variable
names and values with commas.
You can assign the same value to multiple variables by chaining multiple assignment operations
within a single statement.
,You can change the value stored within a variable by assigning a new value to it using another
assignment statement. Be careful while reassigning variables: when you assign a new value to the
variable, the old value is lost and no longer accessible.
The pattern var = var op something (where op is an arithmetic operator like +, -, *, /) is very
common, so Python provides a shorthand syntax for it.
Variable names can be short (a, x, y, etc.) or descriptive
( my_favorite_color, profit_margin, the_3_musketeers, etc.). However, you must follow
these rules while naming Python variables:
A variable's name must start with a letter or the underscore character _. It cannot begin with
a number.
A variable name can only contain lowercase (small) or uppercase (capital) letters, digits, or
underscores (a-z, A-Z, 0-9, and _).
Variable names are case-sensitive, i.e., a_variable, A_Variable, and A_VARIABLE are all
different variables.
Here are some valid variable names:
, Let's try creating some variables with invalid names. Python prints a syntax error if your variable's
name is invalid.
Syntax: The syntax of a programming language refers to the rules that govern the structure of a valid
instruction or statement. If a statement does not follow these rules, Python stops execution and
informs you that there is a syntax error. You can think of syntax as the rules of grammar for a
programming language.
Built-in data types in Python
Any data or information stored within a Python variable has a type. You can view the type of data
stored within a variable using the type function.
Python has several built-in data types for storing different kinds of information in variables.
Following are some commonly used data types:
1. Integer
2. Float
3. Boolean
4. None
5. String
6. List