Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82ef777

Browse files
Added If else
1 parent f0fab3e commit 82ef777

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed

‎.DS_Store‎

0 Bytes
Binary file not shown.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## 1. If/Else\n",
8+
"Decision-making is required when we want to execute a code only if a certain condition is satisfied.\n",
9+
"\n",
10+
"The `if...elif...else` statement is used in Python for decision-making."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 4,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"3 is a positive number.\n",
23+
"This is always printed.\n",
24+
"This is also always printed.\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"#Example of If/Else statement\n",
30+
"#If the number is positive, we print an appropriate message\n",
31+
"\n",
32+
"num = 3\n",
33+
"if num > 0:\n",
34+
" print(num, \"is a positive number.\")\n",
35+
"print(\"This is always printed.\")\n",
36+
"\n",
37+
"num = -1\n",
38+
"if num > 0:\n",
39+
" print(num, \"is a positive number.\")\n",
40+
"print(\"This is also always printed.\")"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 5,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"Positive number\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"#Example of If/Elif/Else statement\n",
58+
"'''In this program,\n",
59+
"we check if the number is positive or\n",
60+
"negative or zero and\n",
61+
"display an appropriate message'''\n",
62+
"\n",
63+
"num = 3.4\n",
64+
"\n",
65+
"# Try these two variations as well:\n",
66+
"# num = 0\n",
67+
"# num = -4.5\n",
68+
"\n",
69+
"if num > 0:\n",
70+
" print(\"Positive number\")\n",
71+
"elif num == 0:\n",
72+
" print(\"Zero\")\n",
73+
"else:\n",
74+
" print(\"Negative number\")"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 6,
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"Enter a number: 10\n",
87+
"Positive number\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"#Example of nested If statement\n",
93+
"'''In this program, we input a number\n",
94+
"check if the number is positive or\n",
95+
"negative or zero and display\n",
96+
"an appropriate message\n",
97+
"This time we use nested if statement'''\n",
98+
"\n",
99+
"num = float(input(\"Enter a number: \"))\n",
100+
"if num >= 0:\n",
101+
" if num == 0:\n",
102+
" print(\"Zero\")\n",
103+
" else:\n",
104+
" print(\"Positive number\")\n",
105+
"else:\n",
106+
" print(\"Negative number\")"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": []
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "Python 3",
120+
"language": "python",
121+
"name": "python3"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 3
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython3",
133+
"version": "3.8.5"
134+
}
135+
},
136+
"nbformat": 4,
137+
"nbformat_minor": 4
138+
}

‎2. Flow_control/1. If-Else.ipynb‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## 1. If/Else\n",
8+
"Decision-making is required when we want to execute a code only if a certain condition is satisfied.\n",
9+
"\n",
10+
"The `if...elif...else` statement is used in Python for decision-making."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 4,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"3 is a positive number.\n",
23+
"This is always printed.\n",
24+
"This is also always printed.\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"#Example of If/Else statement\n",
30+
"#If the number is positive, we print an appropriate message\n",
31+
"\n",
32+
"num = 3\n",
33+
"if num > 0:\n",
34+
" print(num, \"is a positive number.\")\n",
35+
"print(\"This is always printed.\")\n",
36+
"\n",
37+
"num = -1\n",
38+
"if num > 0:\n",
39+
" print(num, \"is a positive number.\")\n",
40+
"print(\"This is also always printed.\")"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 5,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"Positive number\n"
53+
]
54+
}
55+
],
56+
"source": [
57+
"#Example of If/Elif/Else statement\n",
58+
"'''In this program,\n",
59+
"we check if the number is positive or\n",
60+
"negative or zero and\n",
61+
"display an appropriate message'''\n",
62+
"\n",
63+
"num = 3.4\n",
64+
"\n",
65+
"# Try these two variations as well:\n",
66+
"# num = 0\n",
67+
"# num = -4.5\n",
68+
"\n",
69+
"if num > 0:\n",
70+
" print(\"Positive number\")\n",
71+
"elif num == 0:\n",
72+
" print(\"Zero\")\n",
73+
"else:\n",
74+
" print(\"Negative number\")"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 6,
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"Enter a number: 10\n",
87+
"Positive number\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"#Example of nested If statement\n",
93+
"'''In this program, we input a number\n",
94+
"check if the number is positive or\n",
95+
"negative or zero and display\n",
96+
"an appropriate message\n",
97+
"This time we use nested if statement'''\n",
98+
"\n",
99+
"num = float(input(\"Enter a number: \"))\n",
100+
"if num >= 0:\n",
101+
" if num == 0:\n",
102+
" print(\"Zero\")\n",
103+
" else:\n",
104+
" print(\"Positive number\")\n",
105+
"else:\n",
106+
" print(\"Negative number\")"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": []
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "Python 3",
120+
"language": "python",
121+
"name": "python3"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 3
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython3",
133+
"version": "3.8.5"
134+
}
135+
},
136+
"nbformat": 4,
137+
"nbformat_minor": 4
138+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /