b.com python first unit
b.com python first unit
What Is an Algorithm?
Operating Systems
Program Translation
About Python
What Is a Literal?
• To take something literally is to take it at “face value.”
The same is
true of literals in programming.
• A literal is a sequence of one of more characters that
stands for itself, such as the literal 12. We look at numeric
literals in Python next.
• A literal is a sequence of one or more characters
that stands for itself
• Numeric Literals
• String Literals
• Control Characters
• String Formatting
NUMERIC LITERALS
A numeric literal is a literal containing only the digits 0–9,
an optional sign character (1 or 2), and a possible decimal
point.
Ex:
STRING LITERALS
Numerical values are not the only literal values in
programming. String literals, or “strings,” rep resent a
sequence of characters,
Keyword:
What Is an 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 1 (3 * k) An
expression can also consist of a single literal or variable.
Thus, 4, 3, and k are each expression.
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.
What Is an Operator?
Ex:
Arithmetic Operators
Arithmetic operators are used to perform arithmetic
operations between two operands. It includes
+ (addition), - (subtraction), *(multiplication), /(divide), %
(reminder), //(floor division), and exponent (**) operators.
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b
= 30
- (Subtraction) It is used to subtract the second operand from the first operand. If
the first operand is less than the second operand, the value
results negative. For
Comparison operator
Comparison operators are used to comparing the value of the two operands and returns Boolean
true or false accordingly. The comparison operators are described in the following table
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition
becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes
true.
< If the first operand is less than the second operand, then the condition becomes
true.
Assignment Operators
The assignment operators are used to assign the value of the right expression to the left operand. The
assignment operators are described in the following table.
Operator Description
+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+
= b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10 => a-
= b will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10, b
= 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and
assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a
% = b will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2
= 16 to a.
Bitwise Operators
The bitwise operators perform bit by bit operation on the values of the two operands. Consider the
following example.
For example,
if a = 7
b=6
then, binary (a) = 0111
binary (b) = 0110
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the
result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit
will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting
bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
<< (left shift) The left operand value is moved left by the number of bits present in the right
operand.
>> (right shift) The left operand is moved right by the number of bits present in the right
operand.
Logical Operators
Operator Description
and If both the expression are true, then the condition will be true. If a and b
are the two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b
are the two expressions, a → true, b → false => a or b → true.
not If an expression a is true, then not (a) will be false and vice versa.
Membership Operators
in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).
Identity Operators
The identity operators are used to decide whether an element certain class or type.
Operator Description
is not It is evaluated to be true if the reference present at both sides do not point
to the same object.
Operator Precedence
The precedence of the operators is essential to find out since it enables us to know which
operator should be evaluated first. The precedence table of the operators in Python is given
below.
Operator Description
** The exponent operator is given priority over all the others used in the
expression.