-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
129 lines (89 loc) · 2.3 KB
/
control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
"""control.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19DdvYTb2g2_fi8hS6x2ZFxTAhpZUIMDl
"""
# If the number is positive, we print an appropriate message
# Python if... Statement - 1 option
num = 30
if num > 0:
print(num, "is a positive number.")
print("printed number status.")
# Python if...else Statement - 2 option
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
# Python if...elif...else Statement - 3 Option
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [60, 25, 33, 85, 40, 22, 55, 43, 51]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
# Program to iterate through a list using indexing
music = ['pop', 'rock', 'jazz','classic']
# iterate over the list using index
for i in range(len(music)):
print("I like", music[i])
#combined with the len() function to iterate through a sequence using indexing.
# program to display student's marks from record
student_name = 'Soyuj'
marks = {'James': 90, 'Jules': 55, 'Arthur': 77}
for student in marks:
if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')
# Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
# Program to show the use of continue statement inside loops
for val in "string":
if val == "i":
continue
print(val)
print("The end")
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
# While loop with else
'''Example to illustrate
the use of else statement
with the while loop'''
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")