Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Conditions in Phyton</h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h2>Comparisons Operator</h2>"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Condition Equal\n",
- "\n",
- "a = True\n",
- "a == False"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Greater than Sign\n",
- "\n",
- "i = 6\n",
- "i > 5"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Inequality Sign\n",
- "\n",
- "i = 'Circle'\n",
- "i != 'Rectangle'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Compare characters\n",
- "# Note the ASCII code for A is 101 and B is 102\n",
- "\n",
- "'B' > 'A'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Comparisons of Characters: upper case letters have different ASCII code than lower case letters\n",
- "\n",
- "'ab'>'AB'"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h2>Branching</h2>"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# If statement example\n",
- "\n",
- "age = 19\n",
- "#age = 18\n",
- "\n",
- "#expression that can be true or false\n",
- "if age > 18:\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": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "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": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Elif statment example\n",
- "\n",
- "age = 17\n",
- "\n",
- "if age > 18:\n",
- " print(\"you can enter\" )\n",
- "elif age == 18:\n",
- " print(\"go see Pink Floyd\")\n",
- "else:\n",
- " print(\"go see Meat Loaf\" )\n",
- " \n",
- "print(\"move on\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "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": [
- "<h2>Logical Operators</h2>"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": false,
- "jupyter": {
- "outputs_hidden": false
- }
- },
- "outputs": [],
- "source": [
- "# Condition statement example\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(\"\")\n",
- "print(\"Do Stuff..\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "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.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement