-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice_assignment_python_session_1.py
More file actions
158 lines (123 loc) · 4.13 KB
/
practice_assignment_python_session_1.py
File metadata and controls
158 lines (123 loc) · 4.13 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""Practice_Assignment : Python - Session 1.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1xlQ5kaRCZdXoYtXT3Ea1646OAeu9qR-I
## Section 1: Understanding Variables
1. Define a Variable
"""
fullName = "Suraj Pawar Mamidi"
print(fullName)
"""2. Swapping two Variables without using third vaiable."""
a = 5; b = 10
a_type = type(a)
b_type = type(b)
if a_type == int and b_type == int :
a = a + b
b = a - b
a = a - b
elif a_type == str or b_type == str:
a, b = b, a
print(f'Swapped items a ={a}, b={b}')
"""## Section 2: Working with Data Types
3. Identify Data Types
"""
x = 25; y = 3.14; z = 'Python'; is_valid = True
print(f'x = {type(x)}, y = {type(y)}, z = {type(z)}, is_valid = {type(is_valid)}')
"""4. Convert Data Types"""
num = 50; string = '123'
print(str(num))
print(float(num))
print(int(string))
"""5. Basic Arithmetic Operations"""
import random
int_1 = random.randint(1, 100)
int_2 = random.randint(100, 200)
print(f'Addition of two integers {int_1} and {int_2} is {int_1 + int_2}')
float_1 = random.uniform(1, 100).__round__(4)
float_2 = random.uniform(100, 200).__round__(4)
print(f'Addition of two floats {float_1} and {float_2} is {float_1 + float_2}')
print(f'Multiplication of an integer {int_1} and a float {float_1} is {(int_1 * float_1).__round__(4)}')
print(f'Division of an integer {int_1} and a float {float_1} is {(int_1 / float_1).__round__(4)}')
print(f'Remainder of dividing 15 by 4 is {15 % 4}')
"""## Section 3 Boolean & Conditional Statements
6. Boolean Expressions
"""
x = 10; y = 20
print(x > y)
print(x < y)
print(x == y)
print(x != y)
"""7. If-Else Statement"""
input_num = ''
while not bool(input_num) or input_num.isalpha():
input_num = input('Enter a number: ')
if bool(input_num) and input_num.isalpha():
print('Invalid input. Please enter a valid number.')
input_num = int(input_num)
if input_num % 2 == 0:
print(f'{input_num} is an even number')
else:
print(f'{input_num} is an odd number')
"""8. User Input and Data Processing"""
entered_age = ''
def valid_input(input_value):
return bool(input_value) and input_value.isnumeric()
while not valid_input(entered_age):
entered_age = input('Enter your age: ')
if not valid_input(entered_age):
print('Invalid input. Please enter a valid age.')
entered_age = int(entered_age)
if entered_age >= 18:
print('You are eligible to vote.')
else:
print('You must be 18+ to be eligible to vote.')
"""9. Simple Calci"""
def perform_calculation(first_input, second_input, operation):
match operation:
case '+':
return first_input + second_input
case '-':
return first_input - second_input
case '*':
return first_input * second_input
case '/':
if second_input == 0:
return "Error: Division by zero!"
return first_input / second_input
def give_operation(input_operation):
parsed_input = input_operation.strip().lower()
match parsed_input:
case '+' | 'add':
return '+'
case '-' | 'sub' | 'subtract':
return '-'
case '*' | 'x' | 'mul' | 'multiply':
return '*'
case '/' | '÷' | 'div' | 'divide':
return '/'
case _:
return None
def input_loop(input_type):
while True:
user_input = input(f'Enter {input_type}: ')
if input_type == 'operator':
operation_symbol = give_operation(user_input)
if operation_symbol:
return operation_symbol
else:
print('Invalid operation. Please enter a valid operator.')
elif input_type in ('first_input', 'second_input'):
if valid_input(user_input):
return int(user_input)
else:
print('Invalid input. Please enter a valid number.')
else:
print(f'Error: Unknown input type requested: {input_type}')
calci_dic = dict(first_input = '', second_input = '', operator = '')
for key in calci_dic:
calci_dic[key] = input_loop(key)
if key == 'operator':
break
result = perform_calculation(calci_dic['first_input'], calci_dic['second_input'], calci_dic['operator'])
print(f'Result of {calci_dic["first_input"]} {calci_dic["operator"]} {calci_dic["second_input"]} is {result}')