{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOUsQcropHYUEv7NAsfzRLn",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"source": [
"# **Python Assignment Operators**\n",
"Python Assignment operators are used to assign values to variables. Following is a table which shows all Python assignment operators.\n",
"\n",
"The following table contains all assignment operators with their symbols, names, and examples −\n",
"\n",
"Operator\tExample\tSame As\n",
"=\ta = 10\ta = 10\n",
"\n",
"+=\ta += 30\ta = a + 30\n",
"\n",
"-=\ta -= 15\ta = a - 15\n",
"\n",
"*=\ta *= 10\ta = a * 10\n",
"\n",
"/=\ta /= 5\ta = a / 5\n",
"\n",
"%=\ta %= 5\ta = a % 5\n",
"\n",
"**=\ta **= 4\ta = a ** 4\n",
"\n",
"//=\ta //= 5\ta = a // 5\n",
"\n",
"&=\ta &= 5\ta = a & 5\n",
"\n",
"|=\ta |= 5\ta = a | 5\n",
"\n",
"^=\ta ^= 5\ta = a ^ 5\n",
"\n",
"=\ta >>= 5\ta = a >> 5\n",
"\n",
"<<=\ta <<= 5\ta = a << 5"
],
"metadata": {
"id": "d0clUn3EBjq8"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KU7s4aECBcUt",
"outputId": "7f41b4ff-2310-4c04-edcb-91237feb8069"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Subtotal: $77.97\n",
"Tax: $6.24\n",
"Total: $84.21\n",
"Change: $15.79\n",
"After deposit: $1250.00\n",
"After withdrawal: $1175.00\n",
"After interest: $1180.87\n",
"Final balance: $1157.26\n",
"Student passed: True\n",
"Honor roll: False\n",
"Perfect score: False\n",
"Needs improvement: False\n",
"Above class average: True\n",
"Admin access granted: True\n",
"Basic access granted: True\n",
"System available: True\n",
"Full access granted: True\n",
"list1 is list3: True\n",
"list1 is list2: False\n",
"list1 is not list2: True\n",
"No user data: True\n",
"Has user data: True\n",
"Message contains blocked words: False\n",
"Message contains 'spam': True\n",
"'product' is safe to use: True\n",
"User has permission: False\n",
"User is banned: False\n",
"User permissions: 6\n",
"Has read permission: True\n",
"Has write permission: True\n",
"Has execute permission: False\n",
"Updated permissions: 7\n",
"After removing write: 5\n",
"5 doubled using left shift: 10\n"
]
}
],
"source": [
"# Shopping cart total calculation\n",
"item_price = 25.99\n",
"quantity = 3\n",
"tax_rate = 0.08\n",
"\n",
"# Calculate subtotal using multiplication\n",
"subtotal = item_price * quantity\n",
"\n",
"# Calculate tax using multiplication\n",
"tax_amount = subtotal * tax_rate\n",
"\n",
"# Calculate total using addition\n",
"total = subtotal + tax_amount\n",
"\n",
"# Calculate change from $100 using subtraction\n",
"payment = 100.00\n",
"change = payment - total\n",
"\n",
"# Display results\n",
"print(f\"Subtotal: ${subtotal:.2f}\")\n",
"print(f\"Tax: ${tax_amount:.2f}\")\n",
"print(f\"Total: ${total:.2f}\")\n",
"print(f\"Change: ${change:.2f}\")\n",
"\n",
"\n",
"# Initial account balance\n",
"account_balance = 1000.00\n",
"\n",
"# Deposit money using addition assignment\n",
"deposit_amount = 250.00\n",
"account_balance += deposit_amount\n",
"print(f\"After deposit: ${account_balance:.2f}\")\n",
"\n",
"# Withdraw money using subtraction assignment\n",
"withdrawal_amount = 75.00\n",
"account_balance -= withdrawal_amount\n",
"print(f\"After withdrawal: ${account_balance:.2f}\")\n",
"\n",
"# Apply monthly interest using multiplication assignment\n",
"monthly_interest_rate = 1.005 # 0.5% interest\n",
"account_balance *= monthly_interest_rate\n",
"print(f\"After interest: ${account_balance:.2f}\")\n",
"\n",
"# Calculate service fee using division assignment\n",
"service_fee_rate = 0.02 # 2% service fee\n",
"fee_amount = account_balance\n",
"fee_amount *= service_fee_rate\n",
"account_balance -= fee_amount\n",
"print(f\"Final balance: ${account_balance:.2f}\")\n",
"\n",
"\n",
"\n",
"# Student test scores\n",
"student_score = 87\n",
"passing_score = 60\n",
"honor_roll_score = 90\n",
"perfect_score = 100\n",
"\n",
"# Check if student passed using greater than or equal\n",
"passed = student_score >= passing_score\n",
"print(f\"Student passed: {passed}\")\n",
"\n",
"# Check if student made honor roll using greater than or equal\n",
"honor_roll = student_score >= honor_roll_score\n",
"print(f\"Honor roll: {honor_roll}\")\n",
"\n",
"# Check if student got perfect score using equal\n",
"perfect = student_score == perfect_score\n",
"print(f\"Perfect score: {perfect}\")\n",
"\n",
"# Check if student needs improvement using less than\n",
"needs_improvement = student_score < passing_score\n",
"print(f\"Needs improvement: {needs_improvement}\")\n",
"\n",
"# Compare with class average\n",
"class_average = 82\n",
"above_average = student_score > class_average\n",
"print(f\"Above class average: {above_average}\")\n",
"\n",
"\n",
"# User credentials and permissions\n",
"user_authenticated = True\n",
"user_role = \"admin\"\n",
"account_active = True\n",
"maintenance_mode = False\n",
"\n",
"# Check if user can access admin panel using 'and'\n",
"admin_access = user_authenticated and user_role == \"admin\" and account_active\n",
"print(f\"Admin access granted: {admin_access}\")\n",
"\n",
"# Check if user can access system using 'or'\n",
"basic_access = user_authenticated and account_active or user_role == \"guest\"\n",
"print(f\"Basic access granted: {basic_access}\")\n",
"\n",
"# Check if system is available using 'not'\n",
"system_available = not maintenance_mode and account_active\n",
"print(f\"System available: {system_available}\")\n",
"\n",
"# Complex access control logic\n",
"full_access = (user_authenticated and account_active) and (user_role == \"admin\" or user_role == \"moderator\") and not maintenance_mode\n",
"print(f\"Full access granted: {full_access}\")\n",
"\n",
"\n",
"# Creating different types of objects\n",
"list1 = [1, 2, 3]\n",
"list2 = [1, 2, 3]\n",
"list3 = list1\n",
"\n",
"# Check if lists are the same object using 'is'\n",
"same_object = list1 is list3\n",
"print(f\"list1 is list3: {same_object}\")\n",
"\n",
"# Check if lists are different objects using 'is'\n",
"different_objects = list1 is list2\n",
"print(f\"list1 is list2: {different_objects}\")\n",
"\n",
"# Check if lists are not the same object using 'is not'\n",
"not_same_object = list1 is not list2\n",
"print(f\"list1 is not list2: {not_same_object}\")\n",
"\n",
"# Common use case: checking for None\n",
"user_data = None\n",
"no_data = user_data is None\n",
"print(f\"No user data: {no_data}\")\n",
"\n",
"# After loading data\n",
"user_data = {\"name\": \"John\", \"age\": 30}\n",
"has_data = user_data is not None\n",
"print(f\"Has user data: {has_data}\")\n",
"\n",
"\n",
"# Content filtering for a social media platform\n",
"blocked_words = [\"spam\", \"scam\", \"fake\", \"virus\"]\n",
"user_message = \"This is a legitimate message about our product\"\n",
"suspicious_message = \"This is a spam message with fake offers\"\n",
"\n",
"# Check if message contains blocked words using 'in'\n",
"contains_blocked = any(word in user_message.lower() for word in blocked_words)\n",
"print(f\"Message contains blocked words: {contains_blocked}\")\n",
"\n",
"# Check specific word using 'in'\n",
"has_spam = \"spam\" in suspicious_message.lower()\n",
"print(f\"Message contains 'spam': {has_spam}\")\n",
"\n",
"# Check if word is not in blocked list using 'not in'\n",
"safe_word = \"product\"\n",
"is_safe = safe_word not in blocked_words\n",
"print(f\"'{safe_word}' is safe to use: {is_safe}\")\n",
"\n",
"# Check user permissions\n",
"allowed_users = [\"admin\", \"moderator\", \"verified_user\"]\n",
"current_user = \"regular_user\"\n",
"has_permission = current_user in allowed_users\n",
"print(f\"User has permission: {has_permission}\")\n",
"\n",
"# Check if user is not in banned list\n",
"banned_users = [\"spammer123\", \"troll456\"]\n",
"is_banned = current_user in banned_users\n",
"print(f\"User is banned: {is_banned}\")\n",
"\n",
"\n",
"# Permission flags for a file system\n",
"READ = 4 # 100 in binary\n",
"WRITE = 2 # 010 in binary\n",
"EXECUTE = 1 # 001 in binary\n",
"\n",
"# User permissions using bitwise OR to combine flags\n",
"user_permissions = READ | WRITE # 110 in binary (6 in decimal)\n",
"print(f\"User permissions: {user_permissions}\")\n",
"\n",
"# Check if user has read permission using bitwise AND\n",
"has_read = bool(user_permissions & READ)\n",
"print(f\"Has read permission: {has_read}\")\n",
"\n",
"# Check if user has write permission using bitwise AND\n",
"has_write = bool(user_permissions & WRITE)\n",
"print(f\"Has write permission: {has_write}\")\n",
"\n",
"# Check if user has execute permission using bitwise AND\n",
"has_execute = bool(user_permissions & EXECUTE)\n",
"print(f\"Has execute permission: {has_execute}\")\n",
"\n",
"# Add execute permission using bitwise OR\n",
"user_permissions = user_permissions | EXECUTE\n",
"print(f\"Updated permissions: {user_permissions}\")\n",
"\n",
"# Remove write permission using bitwise XOR\n",
"user_permissions = user_permissions ^ WRITE\n",
"print(f\"After removing write: {user_permissions}\")\n",
"\n",
"# Double a number using left shift (equivalent to multiplying by 2)\n",
"number = 5\n",
"doubled = number << 1\n",
"print(f\"{number} doubled using left shift: {doubled}\")\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"source": [
"# **Operators** in Python programming are special symbols or keywords that perform operations on variables and values. They are fundamental building blocks that allow developers to manipulate data, perform calculations, make comparisons, and control program flow. Python operators take one or more operands (values or variables) and produce a result based on the specific operation being performed. There are seven different operators in Python:\n",
"\n",
"Arithmetic Operators: Used to perform mathematical calculations like addition, subtraction, multiplication, and division.\n",
"Assignment Operators: Used to assign values to variables and update them with operations.\n",
"\n",
"Comparison Operators: Used to compare two values and return a Boolean result (True or False).\n",
"\n",
"Logical Operators: Used to combine multiple conditions and return a Boolean outcome.\n",
"\n",
"Bitwise Operators: Used to perform operations at the binary (bit) level on integers.\n",
"\n",
"Membership Operators: Used to test whether a value exists within a sequence such as a list or string.\n",
"\n",
"Identity Operators: Used to check whether two variables refer to the exact same object in memory."
],
"metadata": {
"id": "iUGzub2PB-9f"
}
},
{
"cell_type": "code",
"source": [
"a = 21\n",
"b = 10\n",
"# Addition\n",
"print (\"a + b : \", a + b)\n",
"# Subtraction\n",
"print (\"a - b : \", a - b)\n",
"# Multiplication\n",
"print (\"a * b : \", a * b)\n",
"# Division\n",
"print (\"a / b : \", a / b)\n",
"# Modulus\n",
"print (\"a % b : \", a % b)\n",
"# Exponent\n",
"print (\"a ** b : \", a ** b)\n",
"# Floor Division\n",
"print (\"a // b : \", a // b)\n",
"\n",
"\n",
"a = 4\n",
"b = 5\n",
"# Equal\n",
"print (\"a == b : \", a == b)\n",
"# Not Equal\n",
"print (\"a != b : \", a != b)\n",
"# Greater Than\n",
"print (\"a > b : \", a > b)\n",
"# Less Than\n",
"print (\"a < b : \", a < b)\n",
"# Greater Than or Equal to\n",
"print (\"a >= b : \", a >= b)\n",
"# Less Than or Equal to\n",
"print (\"a <= b : \", a <= b)\n",
"\n",
"\n",
"# Assignment Operator\n",
"a = 10\n",
"# Addition Assignment\n",
"a += 5\n",
"print (\"a += 5 : \", a)\n",
"# Subtraction Assignment\n",
"a -= 5\n",
"print (\"a -= 5 : \", a)\n",
"# Multiplication Assignment\n",
"a *= 5\n",
"print (\"a *= 5 : \", a)\n",
"# Division Assignment\n",
"a /= 5\n",
"print (\"a /= 5 : \",a)\n",
"# Remainder Assignment\n",
"a %= 3\n",
"print (\"a %= 3 : \", a)\n",
"# Exponent Assignment\n",
"a **= 2\n",
"print (\"a **= 2 : \", a)\n",
"# Floor Division Assignment\n",
"a //= 3\n",
"print (\"a //= 3 : \", a)\n",
"\n",
"a = 60 # 60 = 0011 1100\n",
"b = 13 # 13 = 0000 1101\n",
"# Binary AND\n",
"c = a & b # 12 = 0000 1100\n",
"print (\"a & b : \", c)\n",
"# Binary OR\n",
"c = a | b # 61 = 0011 1101\n",
"print (\"a | b : \", c)\n",
"# Binary XOR\n",
"c = a ^ b # 49 = 0011 0001\n",
"print (\"a ^ b : \", c)\n",
"# Binary Ones Complement\n",
"c = ~a; # -61 = 1100 0011\n",
"print (\"~a : \", c)\n",
"# Binary Left Shift\n",
"c = a << 2; # 240 = 1111 0000\n",
"print (\"a << 2 : \", c)\n",
"# Binary Right Shift\n",
"c = a >> 2; # 15 = 0000 1111\n",
"print (\"a >> 2 : \", c)\n",
"\n",
"x = 5\n",
"y = 10\n",
"if x > 3 and y < 15:\n",
" print(\"Both x and y are within the specified range\")\n",
"\n",
"fruits = [\"apple\", \"banana\", \"cherry\"]\n",
"if \"banana\" in fruits:\n",
" print(\"Yes, banana is a fruit!\")\n",
"else:\n",
" print(\"No, banana is not a fruit!\")\n",
"\n",
"x = 10\n",
"y = 5\n",
"if x is y:\n",
" print(\"x and y are the same object\")\n",
"else:\n",
" print(\"x and y are not the same object\")\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jPAyoY-gCEdl",
"outputId": "68007756-6474-4c63-d65a-cb94aef5f300"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"a + b : 31\n",
"a - b : 11\n",
"a * b : 210\n",
"a / b : 2.1\n",
"a % b : 1\n",
"a ** b : 16679880978201\n",
"a // b : 2\n",
"a == b : False\n",
"a != b : True\n",
"a > b : False\n",
"a < b : True\n",
"a >= b : False\n",
"a <= b : True\n",
"a += 5 : 15\n",
"a -= 5 : 10\n",
"a *= 5 : 50\n",
"a /= 5 : 10.0\n",
"a %= 3 : 1.0\n",
"a **= 2 : 1.0\n",
"a //= 3 : 0.0\n",
"a & b : 12\n",
"a | b : 61\n",
"a ^ b : 49\n",
"~a : -61\n",
"a << 2 : 240\n",
"a >> 2 : 15\n",
"Both x and y are within the specified range\n",
"Yes, banana is a fruit!\n",
"x and y are not the same object\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Python Arithmetic Operators**\n",
"Operator\tDescription\tExample\n",
"+\tused to add two numbers\tsum = a + b\n",
"\n",
"–\tused for subtraction\tdifference = a – b\n",
"\n",
"used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times.\tmul = a*b>>> “Hi”*5\n",
"‘HiHiHiHiHi’\n",
"\n",
"/\tused to divide two numbers\tdiv = b/a\n",
"\n",
"%\tmodulus operator, returns the remainder of division\tmod = a%b\n",
"\n",
"**\texponent operator"
],
"metadata": {
"id": "VbyDCtJwChhF"
}
},
{
"cell_type": "code",
"source": [
"# take two variable, assign values with assignment operators\n",
"a=3\n",
"b=4\n",
"\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a+b\n",
"a+=b\n",
"\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a*b\n",
"a*=b\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a/b\n",
"a/=b\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a%b\n",
"a%=b\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a**b ( exponent operator)\n",
"a**=b\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# it is equivalent to a=a//b ( floor division)\n",
"a//=b\n",
"print(\"a: \"+str(a))\n",
"print(\"b: \"+str(b))\n",
"\n",
"# create two variables\n",
"a=100\n",
"b=200\n",
"\n",
"# (==) operator, checks if two operands are equal or not\n",
"print(a==b)\n",
"\n",
"# (!=) operator, checks if two operands are not equal\n",
"print(a!=b)\n",
"\n",
"# (>) operator, checks left operand is greater than right operand or not\n",
"print(a>b)\n",
"\n",
"# (<) operator, checks left operand is less than right operand or not\n",
"print(a=) operator, checks left operand is greater than or equal to right operand or not\n",
"print(a>=b)\n",
"\n",
"# (<=) operator, checks left operand is less than or equal to right operand or not\n",
"print(a<=b)\n",
"\n",
"#create two variables\n",
"a=10 # binary 1010\n",
"b=7 # binary 0111\n",
"\n",
"# Binary AND (&) operator, done binary AND operation\n",
"print(a&b)\n",
"\n",
"# Binary OR (|) operator, done binary OR operation\n",
"print(a|b)\n",
"\n",
"# Binary XOR (^) operator, done binary XOR operation\n",
"print(a^b)\n",
"\n",
"# Binary ONEs Compliment (~) operator, done binary One's Compliment operation\n",
"print(~a)\n",
"\n",
"# Binary Left Shift (<<) operator, done binary Left Shift operation\n",
"print(a<<1)\n",
"# Binary Right Shift (>>) operator, done binary Right Shift operation\n",
"print(a>>1)\n",
"\n",
"\n",
"#take user input as int\n",
"a=int(input())\n",
"\n",
"# logical AND operation\n",
"\n",
"if a%4==0 and a%3==0:\n",
" print(\"divided by both 4 and 3\")\n",
"\n",
"# logical OR operation\n",
"if a%4==0 or a%3==0:\n",
" print(\"either divided by 4 or 3\")\n",
"\n",
"# logical NOT operation\n",
"if not(a%4==0 or a%3==0):\n",
" print(\"neither divided by 4 nor 3\")\n",
"\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KwhxVW3zChzs",
"outputId": "8b687b3f-bbcf-49b8-a7a2-3e420cd7c41f"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"a: 3\n",
"b: 4\n",
"a: 7\n",
"b: 4\n",
"a: 28\n",
"b: 4\n",
"a: 7.0\n",
"b: 4\n",
"a: 3.0\n",
"b: 4\n",
"a: 81.0\n",
"b: 4\n",
"a: 20.0\n",
"b: 4\n",
"False\n",
"True\n",
"False\n",
"True\n",
"False\n",
"True\n",
"2\n",
"15\n",
"13\n",
"-11\n",
"20\n",
"5\n",
"5\n",
"neither divided by 4 nor 3\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Below is a list** of operators indicating the precedence level. It’s in descending order. That means the upper group has more precedence than that of the lower group.\n",
"\n",
"Parenthesis – ()\n",
"\n",
"Exponentiation – **\n",
"\n",
"Compliment, unary plus and minus – ~, +, -\n",
"\n",
"Multiply, Divide, modulo – *, /, %\n",
"\n",
"Addition and Subtraction – +, -\n",
"\n",
"Right and Left Shift – >>, <<\n",
"\n",
"Bitwise AND – &\n",
"\n",
"Bitwise OR and XOR – |, ^\n",
"\n",
"Comparison Operators – ==, !=, >, <, >=, <=\n",
"\n",
"Assignment Operator- ="
],
"metadata": {
"id": "T957CooUC2LH"
}
},
{
"cell_type": "code",
"source": [
"a = [1,2,3,4,5]\n",
"\n",
"#Is 3 in the list a?\n",
"print (3 in a) # prints True\n",
"\n",
"#Is 12 not in list a?\n",
"print (12 not in a) # prints True\n",
"\n",
"str = \"Hello World\"\n",
"\n",
"#Does the string str contain World?\n",
"print (\"World\" in str) # prints True\n",
"\n",
"#Does the string str contain world? (note: case sensitive)\n",
"print (\"world\" in str) # prints False\n",
"\n",
"print (\"code\" not in str) # prints True\n",
"\n",
"a = 3\n",
"b = 3\n",
"c = 4\n",
"print (a is b) # prints True\n",
"print (a is not b) # prints False\n",
"print (a is not c) # prints True\n",
"\n",
"x = 1\n",
"y = x\n",
"z = y\n",
"print (z is 1) # prints True\n",
"print (z is x) # prints True\n",
"\n",
"str1 = \"FreeCodeCamp\"\n",
"str2 = \"FreeCodeCamp\"\n",
"\n",
"print (str1 is str2) # prints True\n",
"print (\"Code\" is str2) # prints False\n",
"\n",
"a = [10,20,30]\n",
"b = [10,20,30]\n",
"\n",
"print (a is b) # prints False (since lists are mutable in Python)\n",
"\n",
"\n",
"a = 6\n",
"b = 3\n",
"print(a + b) # prints 9\n",
"\n",
"x = 7\n",
"x *= 3\n",
"print(x)\n",
"\n",
"a = 10\n",
"b = 2\n",
"a /= b\n",
"print(a)\n",
"\n",
"print((5 + 3) < (2 * 5))\n",
"\n",
"# The AND operator returns True only if both conditions are True.\n",
"\n",
"a = 7\n",
"condition = a > 5 and a < 10 # Only True if a is greater than 5 AND less than 10\n",
"print(condition) # Output: True\n",
"\n",
"# The OR operator returns True if at least one of the conditions is True.\n",
"\n",
"a = 7\n",
"condition = a > 10 or a < 5 # True if a is greater than 10 OR less than 5\n",
"print(condition) # Output: False\n",
"\n",
"# The NOT operator reverses the result of the condition. If the condition is True, `not` makes it False. If the condition is False, `not` makes it True.\n",
"\n",
"a = 7\n",
"condition = not(a > 5) # Reverses the result of a > 5\n",
"print(condition) # Output: False\n",
"\n",
"result = 10 - 2 + 3 # Both - and + have the same precedence and are left-associative\n",
"print(result) # Output: 11 ((10 - 2) + 3 is evaluated as 8 + 3)\n",
"\n",
"result = 10 / 2 * 3 # Both / and * have the same precedence and are left-associative\n",
"print(result) # Output: 15.0 ((10 / 2) * 3 is evaluated as 5.0 * 3)\n",
"\n",
"result = 2 ** 2 ** 3 # Exponentiation is right-associative\n",
"print(result) # Output: 256 (2 ** (2 ** 3) is evaluated as 2 ** 8)\n",
"\n",
"print(2 ** 1 ** 3)\n",
"\n",
"x = 5\n",
"y = 6\n",
"z = 7\n",
"print(x + y + z) #output is 18\n",
"\n",
"print(x - y - z) #output is -8\n",
"\n",
"print(x * y * z) #output is 210\n",
"\n",
"print(x/y) #output is 0.8333333333333334\n",
"\n",
"print(x%y) #output is 5\n",
"\n",
"print(x**y) #output is 15625\n",
"print(x//y) #output is 0\n",
"\n",
"a = 15\n",
"b = 5\n",
"a += b\n",
"print(a) #output is 20\n",
"\n",
"a -= b\n",
"print(a) #output is 10\n",
"\n",
"a *= b\n",
"print(a) #output is 75\n",
"\n",
"a /= b\n",
"print(a) #output is 3\n",
"\n",
"a %= b\n",
"print(a) #output is 0\n",
"\n",
"a **= b\n",
"print(a) #output is 759375\n",
"\n",
"a //= b\n",
"print(a) #output is 3\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(a==b) #output is False\n",
"print(a!=b) #output is True\n",
"print(a>b) #output is True\n",
"print(a=b) #output is True\n",
"print(a<=b) #output is False\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(a<5 and b<4) #output is False\n",
"print(a>=5 or b>4) #output is True\n",
"print(not(a<5 and b<4)) #output is True\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(a&b) #output is 4\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(a|b)\n",
"#output is 5\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(a^b)\n",
"#output is 1\n",
"\n",
"a = 5\n",
"b = 4\n",
"print(~b)\n",
"#output is -5\n",
"\n",
"print(5 << 2)\n",
"#output is 20\n",
"\n",
"print(20 >> 2)\n",
"#output is 5\n",
"\n",
"a = 5\n",
"b = 5\n",
"print(a is b)\n",
"#output is True\n",
"print (a is not b)\n",
"#output is False\n",
"\n",
"a = 5\n",
"b = 5\n",
"print(id(a)) #output is 499361802696\n",
"print(id(b)) #output is 499361802696\n",
"\n",
"name = 'Praise'\n",
"print('P' in 'Praise') #output is True\n",
"print('q' not in 'Praise') #output is True\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XtNxvVuCDWzf",
"outputId": "95a1b109-c7ea-4e5a-af1a-2dfcbeaf8e2b"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"True\n",
"True\n",
"True\n",
"False\n",
"True\n",
"True\n",
"False\n",
"True\n",
"True\n",
"True\n",
"True\n",
"False\n",
"False\n",
"9\n",
"21\n",
"5.0\n",
"True\n",
"True\n",
"False\n",
"False\n",
"11\n",
"15.0\n",
"256\n",
"2\n",
"18\n",
"-8\n",
"210\n",
"0.8333333333333334\n",
"5\n",
"15625\n",
"0\n",
"20\n",
"15\n",
"75\n",
"15.0\n",
"0.0\n",
"0.0\n",
"0.0\n",
"False\n",
"True\n",
"True\n",
"False\n",
"True\n",
"False\n",
"False\n",
"True\n",
"True\n",
"4\n",
"5\n",
"1\n",
"-5\n",
"20\n",
"5\n",
"True\n",
"False\n",
"11654504\n",
"11654504\n",
"True\n",
"True\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<>:29: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
"<>:36: SyntaxWarning: \"is\" with 'str' literal. Did you mean \"==\"?\n",
"<>:29: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
"<>:36: SyntaxWarning: \"is\" with 'str' literal. Did you mean \"==\"?\n",
"/tmp/ipython-input-2336625969.py:29: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
" print (z is 1) # prints True\n",
"/tmp/ipython-input-2336625969.py:36: SyntaxWarning: \"is\" with 'str' literal. Did you mean \"==\"?\n",
" print (\"Code\" is str2) # prints False\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# **Importance of Python Operators**\n",
"Understanding Python operators is crucial for writing efficient and effective code. Python operators allow us to perform mathematical calculations, assign values to variables, compare values, and perform logical and bitwise operations. By mastering operators, we can write concise and readable code that easily accomplishes complex tasks."
],
"metadata": {
"id": "sucTkQ6PGEAa"
}
}
]
}