Skip to content

Instantly share code, notes, and snippets.

@salamituns
Created January 29, 2021 19:25
Show Gist options
  • Save salamituns/e6239da5b3d157db6af7ef96ff67e78e to your computer and use it in GitHub Desktop.
Save salamituns/e6239da5b3d157db6af7ef96ff67e78e to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Comparison operations compare some value or operand and, based on a condition, they produce a Boolean."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"equal: ==\n",
"not equal: !=\n",
"greater than: >\n",
"less than: <\n",
"greater than or equal to: >=\n",
"less than or equal to: <="
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Condition Equal\n",
"\n",
"a = 5\n",
"a == 6\n",
"#the result will be False because a, which is 5, doesn't equal to 6. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 6\n",
"i > 5\n",
"#the result will be True because i, which is 6, is logically greater than 5. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 2\n",
"i > 5\n",
"#the result will be False because i, which is 2, is logically less than 5. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" not equal: !=\n",
" \n",
"The inequality test uses an exclamation mark preceding the equal sign, if two operands are not equal then the condition becomes True."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 2\n",
"i != 6\n",
"#The result will ofcourse be True because the operands 2 and 6 are logically not equal."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 6\n",
"i != 6\n",
"#And this will be False, because the operands are logically equal."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Using 'Equality' sign to compare the strings\n",
"\n",
"\"ACDC\" == \"Michael Jackson\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Using 'Inequality' sign to compare the strings\n",
"\n",
"\"ACDC\" != \"Michael Jackson\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"A = 65\n",
"B = 66\n",
"'B' > 'A'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"\n",
"'BA' > 'AB'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Branching\n",
"Branching allows us to run different statements for different inputs. \n",
"It is helpful to think of an if statement as a locked room, if the statement is True we can enter the room and your program will run some predefined tasks, but if the statement is False the program will ignore the task."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"IF statement"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"you can enter\n",
"move on\n"
]
}
],
"source": [
"# If statement example\n",
"\n",
"age = 19\n",
"#age = 18\n",
"\n",
"if age > 18:\n",
"#expression that can be true or false\n",
" \n",
" #within an indent, we have the expression that is run if the condition is true\n",
" print(\"you can enter\" )\n",
"\n",
"#The statements after the if statement will run regardless if the condition is true or false \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above code says the required age is 18.\n",
"and my age is 19.\n",
"so if my age is greater than '>'\n",
" The program will print : 'you can enter'\n",
"print 'move on'. This will be printed regardless of my age."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Else statement\n",
"The else statement runs a block of code if none of the conditions are True before this else statement."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see Meat Loaf\n",
"move on\n"
]
}
],
"source": [
"# Else statement example\n",
"\n",
"age = 18\n",
"# age = 19\n",
"\n",
"if age > 18:\n",
" print(\"you can enter\" )\n",
"else:\n",
" print(\"go see Meat Loaf\" )\n",
" \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above code says the required age is 19.\n",
"and my age is 18.\n",
"so if my age is greater than '>' 18\n",
" The program will print : 'you can enter'\n",
" But my age is just 18, which is also not greater than 18\n",
" For this reason, the print('you can enter') will be skipped.\n",
" \n",
" Therefore, the Else statement will be checked.\n",
" i.e.: print(go see meat loaf) will be printed because the if condition statement was not True.\n",
"print 'move on'. This will be printed regardless of my age."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ElseIF\n",
"The elif statement, short for else if, allows us to check additional conditions if the condition statements before it are False. If the condition for the elif statement is True, the alternate expressions will be run."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see Pink Floyd\n",
"move on\n"
]
}
],
"source": [
"# Elif statment example\n",
"\n",
"age = 18\n",
"\n",
"if age > 18: #which is not true, therefore the next line will be skipped.\n",
" print(\"you can enter\" )\n",
"elif age == 18: #This is particularly true, therefore the next line will be printed.\n",
" print(\"go see Pink Floyd\")\n",
"else: #This will as well be skipped, therefore the next line will be skipped.\n",
" print(\"go see Meat Loaf\" )\n",
" \n",
"print(\"move on\") #This will be printed regardless of my age."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"album_year = 1970\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
" \n",
"print('do something..')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is greater than 1980\n",
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"#album_year = 1970\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
"else:\n",
" print(\"less than 1980\")\n",
"\n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you want to check more than one condition at once. \n",
"For example, you might want to check if one condition and another condition is True. \n",
"Logical operators allow you to combine or modify conditions.\n",
"\n",
"and......The 'and' statement is only True when both conditions are true.\n",
"or.......The 'or' statement is true if one condition is True.\n",
"not......The 'not' statement outputs the opposite truth value."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year was in between 1980 and 1989\n",
"\n",
"Do Stuff..\n"
]
}
],
"source": [
"# Condition statement example for 'and'\n",
"\n",
"album_year = 1980\n",
"\n",
"if(album_year > 1979) and (album_year < 1990):\n",
" print (\"Album year was in between 1980 and 1989\")\n",
" \n",
"print(\"\") #this prints blank space.\n",
"print(\"Do Stuff..\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album was not made in the 1980's\n"
]
}
],
"source": [
"# Condition statement example for 'or'\n",
" \n",
"album_year = 1990\n",
"\n",
"if(album_year < 1980) or (album_year > 1989):\n",
" print (\"Album was not made in the 1980's\")\n",
"else:\n",
" print(\"The Album was made in the 1980's \")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is not 1984\n"
]
}
],
"source": [
"# Condition statement example for 'not'\n",
"\n",
"album_year = 1983\n",
"\n",
"if not (album_year == '1984'):\n",
" print (\"Album year is not 1984\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tasks"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This album is Amazing!\n"
]
}
],
"source": [
"#Write an if statement to determine if an album had a rating greater than 8. \n",
"#Test it using the rating for the album “Back in Black” that had a rating of 8.5. \n",
"#If the statement is true print \"This album is Amazing!\"\n",
"\n",
"Back_in_black = 8.5\n",
"\n",
"if (Back_in_black > 8):\n",
" print('This album is Amazing!')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This album is Amazing!\n"
]
}
],
"source": [
"#Write an if-else statement that performs the following. \n",
"#If the rating is larger then eight print “this album is amazing”. \n",
"#If the rating is less than or equal to 8 print “this album is ok”.\n",
"\n",
"Back_in_black = 8.5\n",
"\n",
"if (Back_in_black > 8):\n",
" print('This album is Amazing!')\n",
"else:\n",
" ('this album is ok')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This album came out in the year 1979\n"
]
}
],
"source": [
"#Write an if statement to determine if an album came out before 1980 or in the years: 1991 or 1993. \n",
"#If the condition is true print out the year the album came out.\n",
"\n",
"album_year = 1979\n",
"\n",
"if (album_year < 1980) or (album_year == 1991) or (album_year == 1993):\n",
" print('This album came out in the year %d' %album_year)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment