adding an if/else section

master
manetta 4 years ago
parent b33f8eebc7
commit a6327efa92

@ -333,6 +333,122 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## if/else"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check if a word is in a list\n",
"words = ['hello', 'language', 'weaving']\n",
"word = 'quilting'\n",
"if word in words:\n",
" print(f'{word} is in the list!')\n",
"else:\n",
" print(f'{word} is NOT in the list!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check if a letter is in a word\n",
"words = ['hello', 'language', 'weaving']\n",
"letter = 'a'\n",
"for word in words:\n",
" if letter in word:\n",
" print(f'the letter \"{letter}\" is in the word \"{word}\"!')\n",
" else:\n",
" print(f'the letter \"{letter}\" is NOT in the word \"{word}\"!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check if a word is in a sentence\n",
"sentences = [\n",
" 'Software and language are intrinsically related, since software may process language, and is constructed in language.', \n",
" 'Yet language means different things in the context of computing: formal languages in which algorithms are expressed and software is implemented, and in so-called “natural” spoken languages.', \n",
" 'There are at least two layers of formal language in software: programming language in which the software is written, and the language implemented within the software as its symbolic controls.'\n",
"]\n",
"word = 'programming'\n",
"for sentence in sentences:\n",
" print('---')\n",
" if word in sentence:\n",
" print(f'the word \"{word}\" is in the sentence \"{sentence}\"!')\n",
" else:\n",
" print(f'the word \"{word}\" is NOT in the sentence \"{sentence}\"!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check if a word ends with \"ing\"\n",
"words = ['hello', 'language', 'weaving']\n",
"for word in words:\n",
" if word.endswith('ing'):\n",
" print(f'YES, the word \"{word}\" ends with \"-ing\"!')\n",
" else:\n",
" print(f'NO, the word \"{word}\" DOES NOT end with \"-ing\"!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
@ -438,20 +554,9 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['word'])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"dict.keys()"
]

Loading…
Cancel
Save