0% found this document useful (0 votes)
5 views

Section 3

The document provides an overview of numeric types in Python, including int, float, and complex numbers, as well as the built-in random module for generating random numbers. It details various operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators, along with their descriptions and examples. Additionally, it includes problems for practice related to number operations and comparisons.

Uploaded by

mudy1980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Section 3

The document provides an overview of numeric types in Python, including int, float, and complex numbers, as well as the built-in random module for generating random numbers. It details various operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators, along with their descriptions and examples. Additionally, it includes problems for practice related to number operations and comparisons.

Uploaded by

mudy1980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Numbering and

Operators
Numbers
• There are three numeric types in Python:
1. int
Int, or integer, is a whole number, positive or negative,
without decimals, of unlimited length.

2. float
- Float, or "floating point number" is a number, positive
or negative, containing one or more decimals.
Numbers cont.
- Float can also be scientific numbers with an "e" to
indicate the power of 10.

3. complex
Complex numbers are written with a "j" as the imaginary
part.
Random Number
• Python does not have a random() function to make a random
number, but Python has a built-in module called random that
can be used to make random numbers.
Operators

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Arithmetic operators
Arithmetic operators are used with numeric values

Operator Name Description Example


+ Addition x+y
- Subscription x–y
* Multiplication x*y
/ Division x/y
% Modulus Return the x%y
reminder
** Exponentiation Return the power x ** y
result
// Floor division rounds the result x // y
down to the
nearest whole
number
Assignment operators
Operator Example Same As
= x=4 x=4
+= x += 4 x=x+4
-= x -= 4 x=x–4
*= x *= 4 x=x*4
/= x /= 4 x=x/4
%= x %= 4 x=x%4
//= x =// 4 x = x // 4
**= x **= 4 x = x ** 4
&= x &= 4 x=x&4
|= x |= 4 x=x|4
^= x ^= 4 x=x^4
>>= x >>= 4 x = x >> 4
<<= x <<= 4 x = x << 4
Comparison operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical operators
• Logical operators are used to combine conditional statements.

Operator Description Example


and Returns True if both x < 5 and x < 10
statements are
true.
or Returns True if one x < 5 or x < 10
of the statements is
true.
not Reverse the result, not(x < 5 and x <
returns False if the 10)
result is true.
Identity operators
• Identity operators are used to compare the objects, not if they
are equal, but if they are actually the same object, with the
same memory location.

Operator Description
is Returns True if both variables are the
same object.
is not Returns True if both variables are not the
same object.
Identity example..
Membership operators
• Membership operators are used to test if a sequence is
presented in an object.

Operator Description
in Returns True if a sequence with the
specified value is present in the object.
not in Returns True if a sequence with the
specified value is not present in the object.
Membership example..
Bitwise operators
• used to compare (binary) numbers.

Operators Name Description


& And Sets each bit to 1 if both bits are 1.
| Or Sets each bit to 1 if one of two bits
is 1.
^ Xor Sets each bit to 1 if only one of
two bits is 1.
~ Not Inverts all the bits.
<< Zero fill left Shift left by pushing zeros in from
shift the right and let the leftmost bits
fall off.
>> Signed right Shift right by pushing copies of the
shift leftmost bit in from the left, and
let the rightmost bits fall off.
Problems
❑ Ask the user for a number. Depending on whether the
number is even or odd, print out an appropriate message to
the user. Hint: how does an even / odd number react
differently when divided by 2?
❑ Ask user for 2 numbers and do four different Arithmetic
operation to it and display the result.
❑ Ask user for 2 numbers “num1 , num2” and compare them, if
num1 is greater than num2, display the appropriate message
otherwise display another one.
❑ assigned to variable and do left shift and right shift and
display the results.
(Hint: apply this operation on each of decimal and binary)
Any Question…?

You might also like