-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB1.py
More file actions
183 lines (174 loc) · 2.61 KB
/
LAB1.py
File metadata and controls
183 lines (174 loc) · 2.61 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Python Syntax
# ex1
print("Hello World")
# ex2
if 5 > 2:
print("Five is greater than two!")
# Python Comments
# ex1
# This is a comment
# ex2
"""
This is a comment
written in
more that just one line
"""
# Python Variables
# ex1
carname = "Volvo"
# ex2
x = 50
# ex3
x = 5
y = 10
print(x + y)
# ex4
x = 5
y = 10
z = x + y
print(z)
# ex5
myfirst_name = "John"
# ex6
x = y = z = "Orange"
# ex7
def myfunc():
global x
x = "fantastic"
# Python Data Types
# ex1
x = 5
print(type(x))
# the data type would be int
# ex2
x = "Hello World"
print(type(x))
# the data type would be str
# ex3
x = 20.5
print(type(x))
# the data type would be float
# ex4
x = ["apple", "banana", "cherry"]
print(type(x))
# the data type would be list
# ex5
x = ("apple", "banana", "cherry")
print(type(x))
# the data type would be tuple
# ex6
x = {"name": "John", "age": 36}
print(type(x))
# the data type would be dict
# ex7
x = True
print(type(x))
# the data type would be bool
# Python Numbers
# ex1
x = 5
x = float(x)
# ex2
x = 5.5
x = int(x)
# ex3
x = 5
x = complex(x)
# Python Strings
# ex1
x = "Hello World"
print(len(x))
# ex2
txt = "Hello World"
x = (txt[0])
# ex3
txt = "Hello World"
x = txt[2:5]
# ex4
txt = "Hello World"
x = txt.strip()
# ex5
txt = "Hello World"
txt = txt.upper()
# ex6
txt = "Hello World"
txt = txt.lower()
# ex7
txt = "Hello World"
txt = txt.replace("H", "J")
# ex8
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
# Python Booleans
# ex1
print(10 > 9)
# The statement would print a Boolean value True
# ex2
print(10 == 9)
# The statement would print a Boolean value False
# ex3
print(10 < 9)
# The statement would print a Boolean value False
# ex4
print(bool("abc"))
# The statement would print a Boolean value True
# ex5
print(bool(0))
# The statement would print a Boolean value False
# Python Operators
# ex1
print(10 * 5)
# ex2
print(10 / 2)
# ex3
fruits = ["apple", "banana"]
if "apple" in fruits:
print("Yes, apple is a fruit!")
# ex4
if 5 != 10:
print("5 and 10 is not equal")
# ex5
if 5 == 10 or 4 == 4:
print("At least one of the statements is true!")
# Python If...Else
# ex1
a = 50
b = 10
if a > b:
print("Hello World")
# ex2
a = 50
b = 10
if a != b:
print("Hello World")
# ex3
a = 50
b = 10
if a == b:
print("Yes")
else:
print("No")
# ex4
a = 50
b = 10
if a == b:
print("1")
elif a > b:
print("2")
else:
print("3")
# ex5
a = b = c = d = 1
if a == b and c == d:
print("Hello")
# ex6
if a == b or c == d:
print("Hello")
# ex7
if 5 > 2:
print("Five is greater than two!")
# ex8
if 5 > 2: print("Five is greater than two!")
# ex9
print("Yes") if 5 > 2 else print("No")