-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
105 lines (78 loc) · 1.96 KB
/
Copy pathpython.py
File metadata and controls
105 lines (78 loc) · 1.96 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
###Control statements
num = 10
if num> 0 :
print("This number is positive")
elif num == 0 :
print("This number is zero")
else:
print("This number is negative")
####
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1> num2 :
print(num1, "is greater than" , num2)
elif num2 > num1 :
print(num2, "is greater than", num1)
else:
print("Both numbers are equal")
###Loops
fruits = ["apples", "bananas", "cherries", "dates" ]
for fruit in fruits:
print(fruit)
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
###Using while loop
count = 1
while count <= 5:
print(count)
count += 1 # incriment the count by 1
##Loop control statements
fruits = ["apples", "bananas", "cherries", "dates"]
for fruit in fruits:
if fruit == "cherries":
break # Exit the loop when cherries are found
print(fruit)
print()
for fruit in fruits:
if fruit == "cherries":
continue # Skip the iteration when cherries are found
print(fruit)
print()
for fruit in fruits:
if fruit == "cherries":
pass # Do nothing when cherries are found
print(fruit)
count = 0
while count < 5:
print(count)
count += 1
if count == 3:
break # Exit the loop when count is reached 3
### VAriables(must start with a letter or underscore, must be lowercase, use descriptive names)
my_variable = 10
total_price = 99.99
user = 'John'
# Operators
#Addition(+)
#Subtraction(-)
#Multiplication(*)
#Division(/)
#Modulus(%)
#Exponentiation(**)
x = 10
y = 5
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
# operators with strings
str1 = 'Hello'
str2 = 'World'
print(str1 + " " + str2)
print(str1 * 3) # Repeats the string 3 times