-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython4.py
More file actions
133 lines (89 loc) · 2.67 KB
/
Copy pathpython4.py
File metadata and controls
133 lines (89 loc) · 2.67 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
# python 4: indexing, while loop, for loop, nested loop
# indexing [start: end: step]
credit_number = "1234-5678-9012-4567"
print(credit_number[:3])
print(credit_number[5:9])
print(credit_number[5:])
print(credit_number[::3]) #will print evry 3rd character
last_digits = credit_number[-4:]
print(f"XXXX-XXXX-XXXX-{last_digits}")
credit_number = credit_number[::-1] #print backwards
print(credit_number)
# format specifiers = {value:flag} format a value onthe basis of flag inserted
# :10 = allocate spaces
# :< = left justify
# :>= right justify
# :^ = center align
# :+ = use plus sign to indicate positive
# := = place sign to leftmost position
# : = insert a space
# :, = comma seperator
price1 = 30005.145673
price2 =-9.5634
price3 = 12.536
print(f"Price 1 is ${price1:.3f}")
print(f"Price 2 is ${price2:.2f}")
print(f"Price 3 is ${price3:.2f}")
print(f"Price 1 is ${price1:+,.2f}")
print(f"Price 2 is ${price2:^10}") # keep in center
print(f"Price 3 is ${price3: }")
# while loop (the condition will run till it bcm true)
# 1
age = int(input("Enter your age: "))
while age < 0:
print("Age can't be negative")
age = int(input("Enter your age: "))
print(f"You are {age} years old")
# 2
food = input("Enter a food you like (q for quit): ")
while not food == "q":
print(f"You like {food}")
food = input("Enter another food you like (q for quit): ")
print("Bye")
# 3
num = int(input("Enter a number between 1 and 10: "))
while num < 1 or num > 10 :
print(f"{num} is not valid")
num = int(input("Enter a number between 1 - 10: "))
print(f"You number is {num}")
#for loop (iterate a block of code in fixed number of times)
# 1
for x in range(1,11):
print(x)
print("HAPPY NEW YEAR!")
# 2
for x in reversed(range(1,11)):
print(x)
print("HAPPY NEW YEAR!")
# 3
for x in range(1,11,2): # with two gap
print(x)
print("HAPPY NEW YEAR!")
# 4
credit_card = "1234-5678-90123"
for x in credit_card:
print(x)
print("Here your credit card number")
# 5
for x in range(1,21):
if x == 13:
continue # will skip the 13 / break will stop till 12
else:
print(x) # this printing numbers
#nested loop (outer loop and then inner loop)
# 1
for x in range(1,10):
print(x, end=" ") # in one line and add spaces
# 2
for x in range(3):
for x in range(1,10):
print(x, end="")
print()
# 3
rows = int(input ("Enter number of rows: "))
columns = int(input ("Enter number of columns: "))
symbol = input("Enter a symbol to use: ")
for x in range(rows):
for y in range(columns):
print(symbol, end="")
print()