Data type conversion and operators

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:48 AM on 5/1/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

8 Terms

1
New cards
<p>Implicit Type Conversion</p>

Implicit Type Conversion

Implicit type conversion takes place at run time/ it is performed by python interpreter only/ automatic conversion of one data type into another takes place

THIS IS USED TO CONVERT VALUES

Example

convert integer to float/ do this by adding integer to a float

Num1 = 85

Num2 = 15.5

Add = num1 + num2

Print(add)

Print(type(add))

Output:

100.5

<p><strong>Implicit type conversion </strong>takes place at run time/ it is performed by python interpreter <strong>only/ </strong>automatic conversion of one data type into another takes place</p><p><strong>THIS IS USED TO CONVERT VALUES</strong></p><p>Example</p><p>convert integer to float/ do this by adding integer to a float</p><p>Num1 = 85</p><p>Num2 = 15.5</p><p>Add = num1 + num2</p><p>Print(add)</p><p>Print(type(add))</p><p>Output:</p><p>100.5</p>
2
New cards
<p>Explicit type conversion</p>

Explicit type conversion

Converting a value from one data type to another(manually) using built in functions such as tuple(), list(y), set(y), dict(y), ord(y), etc…

Example

Num1 = 85

Num2 = “55”

Print(“Data type of num1:” type(num1))

Print(“Data type of num2 before type casting:” type(num2))

Num2 = int(num2) //type casting

Print(“Data type of num2 after type casting:”, type(num2))

Add = num1 + num2

3
New cards
<p>Data operators</p>

Data operators

Arithmetic operators

Addition+ /subtraction- /multiplication* division/ /modulus%/exponent**

Comparison(relational) operators

Assignment operators

Bitwise operators

4
New cards
<p>Arithmetic operators - basic math to perform calculations</p>

Arithmetic operators - basic math to perform calculations

Addition + : it is used to find the sum of two numbers : num1 = 25 num2 = 15

num1+num2=40

Subtraction - : it is used to find the difference of two numbers : num1 = 25 num2 = 5

num1-num2 = 5

Multiplication * : it is used to find the multiplication of two numbers : num1 = 30 num2 = 3

num1*num2 = 90

Division / : it is used to find the divided result of 2 numbers : num1= 100 num2= 5

num1/num2 = 20

Modulus % : it is used to find the remainder of 2 numbers// divide first/ whatever the remainder is of the division is the answer

num1 = 50 num2= 20

num1%num2 = 10

Exponent ** multiply the base number by itself the number of times of the exponent

num1 = 5 num2= 7

num1**num2 = 78,125

Integer divide // divides the two values and only gives you the integer result, excludes the decimal result

x=50;y=4

x // y

12… actual answer is 12.5

5
New cards
<p>Comparison operators - symbols used in math and programming to compare two values and return a Boolean result(true or false) = &gt; &lt; !</p>

Comparison operators - symbols used in math and programming to compare two values and return a Boolean result(true or false) = > < !

if both values are equal == // true

num1 = 25 num2 = 15

(num1 == num2) is false


if both values are not equal != // true

num1 = 25 num2 = 15

(num1 != num2) is true


if left operant is greater than right operant > // true

num1 = 25 num2 = 15

(num1 > num2) is true


if left operant is less than right operant < // false

num1 = 25 num2 = 15

(num1 < num2) is false


Greater than or equal to left operator >=

num1 = 25 num2 = 15

(num1 >= num2) is true


Less than or equal to operator <=

num1 = 25 num2 = 15

(num1 <= num2) is false

6
New cards
<p>Assignment operator - this operator is used to assign values to the operands</p><p>= + - <em>/ % </em>** *</p>

Assignment operator - this operator is used to assign values to the operands

= + - / % ** *

Equal to operator // =

It is used to assign values from right side operands into left side operands

num3 = num1 + num2 // assigns value of num1/2 into num3


Add AND operator // +=

It is used to add the value of the right operand to the left operand and assign the result to the left operand

num1 += num2 is equivalent to

num1 = num1 + num2


subtract AND operator // -=

It is used to subtract the value of the right operand from the left operand and assign the result of the left operand

num1 -= num2 is equivalent to

num1 = num1 - num2


Multiply AND operator *=

It is used to multiply the value of the right operand with the left operand and assign the result to the left operand

num1 *= num2 is equivalent to

num1 = num1 * num2


Divide AND operator /=

It is used to divide the value of the left operand with the right operand and assign the value to the left operand

num1 /= num2 is equivalent to

num1 = num1 / num2


Modulus AND operator

It is used to take the modulus using two operands and assign the result to the left operand

num1 %= num2 is equivalent to

num1 = num1 % num2


Exponents AND operator

It is used to perform the exponential(power) calculation on operators and assign the value to the left operand

num1 **= num2 is equivalent to

num1 = num1 ** num2

7
New cards

Bitwise operator - this operator is used to perform bit by bit operation & | ^ ~

Bitwise - example

num1 = 15 num2 = 8

Binary form will be

num1 = 00001111

num2 = 00001000

Divide your number by 2 until you get to the smallest number. However many times you divided it, is how many bits it is. look for the remainder after division and mark it 1, if there is no remainder mark it 0

Example - 15(odd number each division)

1111


Binary AND operator // &

This operator is used to copy a bit to the result if it exists in both operands. That means 1 AND 1 = 1

(num1 & num2) = 0000100


Binary OR operator // |

It Is used to copy a bit if it exists in either operand. That means 1 OR 0 = 1, 1 OR 1 = 1, 0 AND 1 = 1

(num1 | num2) = 00001111

Binary XOR operator // ^

It is used to copy the bit if it is set in one operand but not both. That means

1^1 = 0

Binary ones complement operator // |

It is unary and flips the values

(~num1) = 11110000

8
New cards