Docsity

Variables, Data Types and Operators, Lecture notes of Programming Languages

Partial preview of the text

Download Variables, Data Types and Operators and more Lecture notes Programming Languages in PDF only on Docsity!

Unit II Variables,^ Data^ Types^ and^ Operators

Variables

Variable is a name given to a memory location to store data. We can

create a variable by assigning a value using the “=“ operator.

SYNTAX: variable_name = value

Examples

Name = “Ngawang”

X = 10

Age = 25

City = “Thimphu”

Is_student = True

Subject = “PGL101”

Note:

Variables can store different types of data:

int, float, str, bool etc.

Variable Naming Rules

 (^) A variable name can contain letters (A-Z, a-z), digits (0- 9 ) and underscore ( _ ).

 (^) A variable name must start with a letter or underscore ( _ ), not with a digit.

 (^) A variable name cannot be a Python keyword. (like: if, else, for, while etc).

 (^) Spaces are not allowed in variable names.

 (^) Variable names are case-sensitive. (age, Age and AGE are different)

 (^) Use meaningful and descriptive names(Example: total_marks instead of a)

 (^) Avoid using special characters except underscore ( _ ). (e.g. @, #. %, &, $,

are not allowed)

Identifiers

An identifier is a name that is given to a variable or function or class etc.

Identifier can include letter, number and underscore ( _ ) character.

They should always start with a nonnumeric character.

Example : salary, name11, gross_income etc.

Special symbols such as ?, #, $, %, @ and etc. are not allowed in

identifier

Literals

A literal is a constant value that is stored into a variable in a program.

a = 15

Here ‘a’ is a variable into which the constant value ‘15’ is store. Hence the value

‘15’ is called ‘Literal’.

Cont………….

Salary = 15000. 75

This is a variable. Its name is

‘salary”. Hence this name

‘salary’ is identifier.

This symbol store right side literal into

left side variable. Thus, it is performing

an operations. Hence it is called

‘Operator’

This is the float number stored

into variable. Hence it is called

‘floating point literal’ Constant

A constant is similar to a variable, but its value cannot be change or modified during

the program execution.

eg: import math

math.pi

Defining the constant is not possible in python.

Datatypes in Python

A datatypes represents the type of data store into a variable or memory.

Datatypes

Built-in User-define

The datatypes which can be

created by the programmer is called

User-define datatypes.

The datatypes which are already

available in Python language

There are FIVE types of Built-in

datatypes, such as:

Numeric, None Type, Sequences,

Mapping and Set.

Examples: Array, class or

Module

Built-in Datatypes.

1. None Type

It represent an object that does

not contains any value.

We store ‘None’ to represent an

empty object.

In Boolean expression, ‘None’

value represents ‘False’

Examples:

User_statuse =

None

2. Numeric Type

It represent number. There are THREE

types.

 int

 Float

 complex

2.1.int Datatype: It represents an integer number.

An integer number is a number

without any decimal/fraction

part,

Example:

age =

3. Sequence Datatype Cont.

3.1 str Datatypes

A sequence represent a group of

elements or item.

There are SIX types of sequences.

str list

bytes tuple

byte array range

str represent string datatype. It is

represented by the group of character.

String are enclosed in single or double

quotes. Eg: name = “ sonam ”

3.2 range Datatypes

It represents a sequence of numbers. range()

is a function that generate a sequence of

number and returns them in an object.

So, range() is also called a ‘ generator

function’

Examples:

r = range( 10 )

for i in r :

print( i )

Cont.……

3.3. list Datatypes

List in Python are similar in C or Java. A list represents a group of elements.

The main difference between a list and array is that array can store only one types of

elements.

List can grow dynamically in memory, but the size of an array is fixed, and they cannot

grow at runtime.

It is represented using square brackets [ ] and the elements are written in [ ], separated by

comma.

Examples: List = [ ]

List = [ 10 ]

an empty list

a list with one

List =[ 10 , - 20 , element 15.5, ‘karma’, ”sonam” ] # different elements

Cont.……

3.5. byte Datatypes

A bytes Datatypes represents a group of byte numbers just like an array does. A byte

number is any positive integer from 0 to 255 (inclusive)

elements = [ 10, 20, 0, 40, 15 ]

x = bytes(elements)

print(x[0])

list of byte number

convert list to bytes array

display 0 th^ element, i.e

3.6. bytearray Datatypes

It is similar to bytes datatype. Th difference is that bytes type cannot be modified but

bytearray can be modified. It means any elements of bytearray can be modified.

elements = [ 10, 20, 0, 40, 15 ]

x = bytearray(elements)

print(x[0])

list of byte number

convert list into bytes array

display 0 th^ element, i.e

x[0] = 88 # replace 0 10

th element by 88

Cont.……

4. set

A set is the unorder collection of element. The order of the elements is not

maintained in the sets. It means the elements may not appear in the same order as

they entered the set.

It does not accept the duplicate elements.

There are TWO types of set Datatypes

  1. set datatypes
    1. forzenset datatypes

Cont.……

4.2. frozen set Datatypes

It is same as set datatypes. The main difference is that the element in the set data

types can be modified whereas the element of the frozenset cannot be modified.

Example: s = { 10, 20, 30, 20, 50 }

print(s) #may display as { 50, 10, 20,

30 } fs = frozenset(s)

print(fs) # may display as

Cont.……

  1. Mapping

A map represents a group of elements in the form of key – value pairs so that

when the key is given, we can retrieve the value associated with it.

The key and its value should be separated by a colon ( : ) and every pair should be

separated by a comma.

All the elements should be enclosed inside curly

brackets { } Example: dict datatypes

The ‘dict’ represent a ‘dictionary’ that contains pairs of element (key and

value)

eg: d = { 10 : ‘karma’, 11 : ‘Pema’}

Cont.……

  1. Explicit Type Casting(Manual)

The programmer converts the data type using built-in function

Common casting functions:

Function Convert to Example

Int() Integer Int(“25”) 25

float() float float(“3.14”) 3.

str() string str(100) “100”

bool() Boolean Bool(1) True

List() list List((1,2,3)) [1,2,3]

Tuple() tuple Tuple([1,2,3]) (1,2,3)

Set() set Set([1,2,2,3]) {1,2,3}

Operators in Python

Operators are special symbols that performs operations on one, two or more

operands and return a result.

Types of operators

 Arithmetic Operators

 (^) Comparison Operators

 (^) Logical Operators

 (^) Assignment Operators

 (^) Bitwise Operators

 (^) Membership Operators

 (^) Identity Operators

Order of Operation :

  1. Parenthesis are evaluated.
  2. Exponential are done next
  3. Multiplication. Division,

modulus and floor division are

equal.

  1. Addition and subtraction
  2. Assignment operation