-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython Basics.py
180 lines (146 loc) · 3.24 KB
/
Python Basics.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
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
print("Hello World!")
#strings in python
print("""Hello
World!""")
#integers in python
print(15)
#errors in printing data
print(1,000) #wrong way to print 1000
print(1_000) #right way to print 1000
#floats in python
print(3.14)
#boolean in python
#True
#False
#how to check certain datatype
print(type(12))
#variables in python
myName = "hazik"
print(myName)
n = 15
print(n)
#opeeatrors in python
#Arithmetic operators +-*/
#using / will give float value and using floor division// will give integer value
print(4/2)
print(4//2)
#using ** will give raise power value
print(4*2)
print(4**2)
#priority in python
#Parenthesis
#Exponents
#Multiplication
#Division
#Addition
#Subtraction
#modulus in python used to get remainder
print("Remainder is 11/3 is ")
print(11%3)
#concatenation of strings
print("hello " + "world")
#multiplying strings
print(5*"hazik ")
#taking user input
#input("what is your name ")
#print("hello " + input("whats your name"))
#newline \n
print("hello\nworld")
#length of string
#name = input("enter string to find its length")
#print(len(name))
#concatenating int and string
#name = input("enter string to find its length")
#length = len(name)
#newLength = str(length)
#print("your name has " + newLength + " characters")
# x = input("enter 1st no")
# y = input("enter 2nd no")
# z = int(x) + int(y)
# newX = str(x)
# newY = str(y)
# newZ = str(z)
# print("the sum of " + newX + " and " + newY + " is " + newZ )
# F STRINGS type f infront of a string
weeks = 52
year = 1
print(f"there are {weeks} weeks in {year} year")
#rounded off number
r = round(10/3)
print(f"rounded off number in 10/3 is {r}")
# name = input("whats your name")
# print("hello " + name)
# n = input("enter your fav color")
# y = input("enter your fav animal")
# print("your grp name is " +n+y)
#conditional if else statements
x = 55
y = 100
print(f"x = {x}")
print(f"y = {y}")
if x>y:
print("x > y")
elif x==y:
print("x = y")
else:
print("x < y")
if x%2==0:
print(f"{x} is even")
else:
print(f"{x} is odd")
if y % 2 == 0:
print(f"{y} is even")
else:
print(f"{y} is odd")
#nested conditions
xx = 77
yy = 66
print(xx)
print(yy)
if xx%2==0:
if xx > yy:
print(f"{xx} is even and is > {yy}")
else:
print(f"{xx} is even and is < {yy}")
else:
if xx > yy:
print(f"{xx} is odd and is > {yy}")
else:
print(f"{xx} is even and is < {yy}")
#chained conditional
#elif condition aka else if
#bmi calculator
weight = input("Enter your weight")
height = input("Enter your height")
intWeight = int(weight)
intHeight = float(height)
bmi = intWeight/(intHeight ** 2)
print(f"your BMI is {bmi}")
# Logical operators
# AND all conditions should be true
# OR any 1 condition should be true
# NOT should not be equal to given condition
# TRY AND EXCEPT
# TRY
# EXCEPT
# ELSE
# FINALLY
try:
salary = int(input("enter your salary"))
except:
print("there was an error")
else:
print("you are eligible")
finally:
print("thanks")
lp = int(input("enter a leap year to check"))
if lp % 4 == 0:
if lp % 100 == 0:
if lp % 400 == 0:
print("it is a leap year")
else:
print("it is not a leap year")
else:
print("it is a leap year")
else:
print("it is not a leap year")