Sei sulla pagina 1di 1

Type Variables and Operators

In the last chapter, you learned a little bit about the history of Python. You
learned the steps to install Python and some basic syntax of the language. In the
basic syntax, you learned about types of comments that can be used in the code,
various types of quotes, escape sequence that can be handy, and finally, you
learned about the formatting of strings. In this chapter, you will learn about
assignment statements, arithmetic operators, comparison operators, assignment
operators, bitwise operators, logical operators, membership operators, and identity
operators.
Variables So, what is a variable? Consider that your house needs a name. You place
a nameplate at the front gate of your house. People will now recognize your house
through that nameplate. That nameplate can be considered as variable. Like a
nameplate points to the house, a variable points to the value that is stored in
memory. When you create a variable, the interpreter will reserve some space in the
memory to store values. Depending on the data type of the variable, the interpreter
allocates memory and makes a decision to store a particular data type in the
reserved memory. Various data types, such as integers, decimals, or characters, can
be stored by assigning different data types to the variables. Python variables are
usually dynamically typed, that is, the type of the variable is interpreted during
runtime and you need not specifically provide a type to the variable name, unlike
what other programming languages require. There are certain rules or naming
conventions for naming variables. The following are the rules: Reserved key words
such as if, else, and so on cannot be used for naming variables
Type Variables and Operators
[ 29 ]
Variable names can begin with _, $, or a letter Variable names can be in lower case
and uppercase Variable names cannot start with a number White space characters are
not allowed in the naming of a variable You can assign values to the variable using
= or assignment operator. Syntax:
<variable name>= < expression >
Single assignment Here, we will illustrate the use of the assignment operator (=)
with an example:
city='London' # A string variable assignment. money = 100.75 # A floating point
number assignment count=4 #An integer assignment In this case, we assigned three
different values to three variables using the = operator.
Multiple assignment A single value can be assigned to several variables
simultaneously. For example:
a = b = c = 1
Data types in Python What is a data type in any programming language? Let's try to
understand with a real life problem. We use water, oil, liquid soap, syrups, and so
on in our day to day life. How do you categorize these items? Let's take another
set of examples of bar soap, cell phone, and so on; what classification would you
like to give these items? Answer to all these questions is simple: solid, liquid,
and gases. Yes, we have these three broader classifications for any item that we
have heard about or used in our day to day life. Same is the case in the
programming world. Each and every thing needs to be categorized under different
types. There are many types of data, such as numbers, strings, character, images,
and so on.
Type Variables and Operators
[ 30 ]
Data types can be broadly categorized into five different types, listed as follows:
Numbers String Tuples List Dictionary

Potrebbero piacerti anche