-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture2.py
99 lines (73 loc) · 1.86 KB
/
lecture2.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
### Recap from Lec 1
pi=355/113
print(pi)
### Intro to Strings
b=":"
c=")"
s1=b+2*c
print(s1)
f="a"
g=" b"
h="3"
s2=(f+g)*int(h)
print(s2)
### len() is a function to retrieve length of string
s="abc"
print(len(s))
### Slicing to get a Substring
alphab= 'abcdefghijklmnopqrstuvwxyz'
print(alphab[3:6])
print(alphab[::-1])
print(alphab[4:1:-2])
s="ABC d3f ghi"
print(s[3:len(s)-1])
print(s[4:0:-1])
print(s[6:3])
### Printing multiple objects
print(pi, alphab)
###Input function to bind value to a variable
x=input()
print(x+'... something an idiot would say')
##Second example
verb=input("enter verb: ")
# Coe is commenting out your code so it doesn't run this whole thing twice.
# print("I can",verb,"better than you!")
# print((verb+" ")*5)
## Or, by defining a function that performs the above script but only uses one line of code:
def butBetter():
print("I can",verb,"better than you!")
print((verb+" ")*5)
### F-Strings
num=3000
fraction= 1/3
print(num*fraction, 'is', fraction*100, '% of', num)
print(num*fraction, 'is', str(fraction*100)+'% of', num)
print(f'{num*fraction} is {fraction*100}% of {num}')
###Conditions for Branching -> Logical Operators
##Remember that for this to work, both objects must be same Type
#here we had to change the int 4 to a str
secret = '4'
guess=input("Guess the secret number between 1-10: ")
if guess==secret :
print('Correct')
else :print('Wrong')
###Branching Example 1
pset_time=12
sleep_time=2
if (pset_time+sleep_time)>24:
print('impossible!')
elif(pset_time+sleep_time)>=24:
print('full schedule!')
else:
leftover=abs(24-pset_time-sleep_time)
print(leftover,'h of free time!')
print('end of day')
##Branching Example 2
snumber=6
mguess=int(input("Guess a number: "))
if mguess<snumber:
print('Too low')
elif mguess>snumber:
print('Too high')
elif mguess==snumber:
print("That's exactly it")