Chapter-2 Data and Expressions
Chapter-2 Data and Expressions
By
G. Lavanya
Contents
• Literals
• Variables and Identifiers
• Operators
• Expressions and Data Types
Literals
• A literal is a sequence of one or more characters that stands for itself.
• Used to completely express a fixed value of a specific data type.
• Literals are constants that are self- explanatory and don’t need to be
computed or evaluated.
• They are used to provide variable values or to directly utilize them in
expressions.
Types of literals
1.String literals
2.Character literal
3.Numeric literals
4.Boolean literals
5.Literal Collections
6.Special literals
Numeric Literals
• Contains only the digits (0-9), an optional sign character(+ or -) and a
possible decimal point.
• Three types
• Integer
• Float
• Complex
• Contains decimal point→ floating point value(float)
• Otherwise integer values
• Commas are never used in numeric literals.
Numeric literals in python
Integer
• Both positive and negative numbers including 0.
• There should not be any fractional part.
• Example: 0b10100,50,0x12b,0o320
• 1st→ binary literal
• 2nd →decimal literal
• 3rd→hexa decimal literal
• 4th→ octa decimal literal
Examples
Real part is 0
Limits of Range in Floating-Point
Representation
• There is no limit to the size of an integer than can be represented in
Python.
• Floating point values have both a limited range and a limited
precision.
• Python uses a double precision standard format(IEEE 754) providing a
range of 10-308 to 10 308 with 16 to 17 digits of precision.
Limitations of Floating-point
representation
Result:0.0
Arithmetically correct
result:3.0e410
Indicates: Arithmetic
overflow has occurred
Limits of precision in Floating- Point
Representation
• Arithmetic overflow and arithmetic underflow are relatively easily
detected.
• The loss of precision that can result in a calculated result, however, is
a much more subtle issue.
• For example, 1/3 is equal to the infinitely repeating decimal
.33333333 . . ., which also has repeating digits in base two,
.010101010. . . .
• Floating point representation→ finite number of digits
• For floating point values , approximation of true value is stored
Decimal ends after 16 digits(Range)
Approximation
Capital Letters
• Center Justification:
• Blank Spaces:
• By Default: Left Justification
• Question: Print Hello World ……………………….. Have a nice day!
2.1.6 Implicit and Explicit Line joining
• Sometimes a program line may be too long to fit in the Python-
recommended maximum length of 79 characters.
• There are two ways in Python to do deal with such situations—
implicit and explicit line joining.
Implicit Line Joining
• In Python implicit line joining we uses parentheses, square
brackets or curly braces to split the expression in multiple
lines. For instance consider the code below.
Some points to be note
• i)There can be comment after the line.
2.Blank Lines are allowed to continue
Variable Updation
• Variables may also be assigned to the value of another variable
• Variables num and k are both associated with the same literal value
10 in memory
Declaration and Initialization
of Variables
Redeclaring variables in Python
# display
print("Before declare: ", Number)
a = b = c = 10
print(a)
print(b)
print(c)
Assigning different values to
multiple variables
• Python allows adding different values in a single line with “,”
operators.
• Can we use the same name for different types?
• If we use the same name, the variable starts referring to a new
value and type.
+ Operator
• The Python plus operator + provides a convenient way to add a value
if it is a number and concatenate if it is a string.
• If a variable is already created it assigns the new value back to the
same variable.
• Can we use + for different Datatypes also?
• No, use for different types would produce an error.
• a="Lavanya“
• b=2
• print(a+b)
id function
• The id function produces a unique number identifying a specific value
(object) in memory.
• Since variables are meant to be distinct, it would appear that this
sharing of values would cause problems.
• Specifically, if the value of num changed,
would variable k change along with it?
• This cannot happen in this case because the
variables refer to integer values, and integer
values are immutable
• An immutable value is a value that cannot be
changed. Thus, both will continue to refer to
the same value until one (or both) of them is
reassigned
If no other variable references the memory location of the original value, the memo
ry location is deallocated (that is, it is made available for reuse).
2.2.2 Variables assignment
through keyboard Input
• The value that is assigned to a given variable does not have to be
specified in the program.
• The value can come from the user by use of the input function .
• input() function is used to read the input from the user.
variable=input(“prompt ”)
• Variable name is assigned to
string type
• If the user hits enter without
entering any value, name would
be assigned to the empty string
(‘ ‘)
• All input is returned by the input function as a string type.
• For the input of numeric values, the response must be converted to the
appropriate type.
• Python provides built-in type conversion functions int() and float()
• line = input('How many credits do you have?’)
num_credits =int(line)
line = input('What is your grade point average?’)
gpa =float(line)
Here, the entered number of credits, say '24', is converted to the equivalent
integer value, 24, before being assigned to variable num_credits. For input of the
gpa, the entered value, say '3.2', is converted to the equivalent floating-point
value, 3.2
• The sep separator is used between the values.
• It defaults into a space character.
• After all values are printed, end is printed. It defaults into a new line.
• Ex:
print(1,2,3,4) Output: 1 2 3 4
print(1,2,3,4,sep='*‘) Output: 1*2*3*4
print(1,2,3,4,sep='#',end='&') Output: 1#2#3#4&
2.2.3 Identifiers
• Identifier is a user-defined name given to a variable, function,
class, module, etc.
• The identifier is a combination of character digits and an
underscore. They are case-sensitive i.e., ‘num’ and ‘Num’ and
‘NUM’ are three different identifiers in python.
• It is a good programming practice to give meaningful names to
identifiers to make the code understandable.
• Rules for Naming Python Identifiers
• It cannot be a reserved python keyword.
• It should not contain white space.
• It can be a combination of A-Z, a-z, 0-9, or underscore.
• It should start with an alphabet character or an underscore ( _ ).
• It should not contain any special character other than an
underscore ( _ ).
2.2.4 Keywords
• A keyword is an identifier that has predefined meaning in a
programming language and therefore cannot be used as a “regular”
identifier. Doing so will result in a syntax error.
2.3 Operators
• An operator is a symbol that represents an operation that may be
performed on one or more operands.
• Operators that take one operand are called unary operators.
• Operators that take two operands are called binary operators.
• +,- can be used as for both unary and binary operator.
Operators
Arithmetic Operators
• Python provides two types of division:
❑ True division
❑Truncating Division
• True Division:
✓ denoted by a single slash,/
✓25/10 evaluates to 2.5
• Truncating Division:
✓Denoted by double slash,//
✓Result is based on type of operands applied to.
• When both operands are integer values, the result is a truncated integer referred
to as integer division .
• When as least one of the operands is a float type, the result is a truncated
floating point.
• 25 // 10 evaluates to 2,
• 25.0 // 10 becomes 2.0
Also called Relational Operators
• == → determines two values are equal
• = →Assignment operator
• These operators not only for numericals , we can also use for
strings.→ Follows Lexographical (Dictionary ) Ordering.
• “Alen” is less than “Brenda”→ Unicode
A→65 B→66 (65<66)
• ‘alen’ is greater than ‘Brenda’(a→97 B→66) (97>66)
• How to get Unicode encoding value for any character → ord function
Also called as Boolean Operators
Boolean Logic Truth Table
Mathematical Vs Programming
Representation
• In Mathematics range is written as: 1<= num <=10(1to10)
• Programming Languages: the above representation does not make sense.
• Let’s see how the above representation works( Assume num=15)
1<=num <=10➔1<=15<=10
=> True <= 10 (∵1<=15 evaluates to 15)
=> True <=10(Non-Sense) Wrong Representation
in programming
languages
Correct Result
• Python automatically rewrites
Membership operators
Ex:
a1 = 3
b1 = 3
a2 = 'CS2’
b2 = 'CSE’
'print(a1 is not
b1)
print(a2 is b2)
2.4 Expressions and Data types
2.4.1 Expression
• An expression is a combination of symbols that evaluates to a value.
Expressions, most commonly, consist of a combination of operators
and operands.
• 4 + (3 * k)
• An expression can also consist of a single literal or variable.
• 4, 3, and k are each expressions.
• This expression has two subexpressions, 4 and (3 * k). Subexpression
(3 * k) itself has two subexpressions, 3 and k.
• Expressions that evaluate to a numeric type are called arithmetic
expressions .
• A subexpression is any expression that is part of a larger expression.
• Subexpressions may be denoted by the use of parentheses, as shown
above.
• Thus, for the expression 4 + (3 * 2), the two operands of the addition
operator are 4 and (3 * 2), and thus the result it equal to 10.
• If the expression were instead written as (4 + 3) * 2, then it would
evaluate to 14. Since a subexpression is an expression, any
subexpression may contain subexpressions of its own.
• If no parentheses are used, then an expression is evaluated according
to the rules of operator precedence in Python.
2.4.2 Operator Precedence
• Generally while we are writing expression we are following infix
notation
• 4+3 (Operator is in between two operands)
• There are another two types
• Prefix(operator before operands +ab)
• Postfix( operator after operands ab+)
• Consider an expression using infix notation
• 4+(3*5)
It contains two operators +,*.
Result:19
If we omit parenthesis:
Two possibilities:
Operator Precedence Table