Variables and Types: in Python -12TH CBSE

Variables and Types: One of the most powerful features of a programming language is the ability to
manipulate variables. When we create a program, we often like to store values so that it can be used later.
We use objects (variables) to capture data, which then can be manipulated by computer to provide
information. By now, we know that object/variable is a name which refers to a value.
Every object has:
An Identity,
A type, and
A value.
A. Identity of the object is its address in memory and does not get change once it is created. We may know
it by typing id (variable)
We would be referring to objects as variable for now.
B. Type (i.e data type) is a set of values, and the allowable operations on those values. It can be one of the
following:
2
2
2
3
Computer Science
4
1. Number: Number data type stores Numerical Values. This data type is immutable i.e. value of its
object cannot be changed. Numbers are of three different types:
Integer & Long (to store whole numbers i.e. decimal digits without fraction part)
Float/floating point (to store numbers with fraction part)
Complex (to store real and imaginary part)
2. None: This is special data type with a single value. It is used to signify the absence of value/false in a
situation. It is represented by None.
3. Sequence: A sequence is an ordered collection of items, indexed by positive integers. It is a
combination of mutable (a mutable variable is one, whose value may change) and immutable (an
immutable variable is one, whose value may not change) data types. There are three types of sequence
data type available in Python, they are Strings, Lists & Tuples.
3.1 String- is an ordered sequence of letters/characters. They are enclosed in single quotes (' ') or
double quotes ('' "). The quotes are not part of string. They only tell the computer about where the
string constant begins and ends. They can have any character or sign, including space in them.
These are immutable. A string with length 1 represents a character in Python.
3.2 Lists: List is also a sequence of values of any type. Values in the list are called elements / items.
These are mutable and indexed/ordered. List is enclosed in square brackets ([]).
3.3 Tuples: Tuples are a sequence of values of any type and are indexed by integers. They are
immutable. Tuples are enclosed in ().
4. Sets: Set is unordered collection of values of any type with no duplicate entry. It is immutable.
5. Mapping: This data type is unordered and mutable. Dictionaries fall under Mappings.
5.1 Dictionaries: It can store any number of python objects. What they store is a key -value pairs,
which are accessed using key. Dictionary is enclosed in curly brackets ({}).
C. Value: Value is any number or a letter or string. To bind value to a variable, we use assignment
operator (=).
2
2
2
Data Types
Numbers None Sequences Sets Mappings
Dictionary Integer Strings Tuple List
Boolean
Floating Complex
Point
Computer Science
Keywords - are used to give some special meaning to the interpreter and are used by Python
interpreter to recognize the structure of program.
A partial list of keywords in Python 2.7 is
and del from not
while as elif global
or with assert else
if pass Yield break
except import print class
exec in Raise continue
finally is return def

for lambda try

No comments:

Post a Comment