-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture2.py
More file actions
101 lines (82 loc) · 2.61 KB
/
Copy pathlecture2.py
File metadata and controls
101 lines (82 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
# str -> sequence of case sensitive characters enclosed within ' ' " " or ''' '''
a = "Jaidev"
b = 'EEE'
c = a + ''' ''' + b # concatenate use +
print(c) # Jaidev EEE
# multiline strings are stored in triple quotes
d = "jaidev
eee"
print(d) # error
e = 'HI '
print (e * 3) # repeat use * (HI HI HI)
print((e + "Jaidev ") * 3) # HI Jaidev HI Jaidev HI Jaidev
i = ':'
f = ')'
print(i + 2*f)
# len() -> function used to retrive the length of string
len("Jaidev") # 6
m = "Jaidev EEE 1st Year"
len(m) # 19
m[0] # 'J' 0 based indexing
m[6] # ' '
m[-1] # 'r'
m[-5] # ' '
m[0] == m[-19] # True
# slicing -> to get substring [start:stop:step] -> step not necessary if not specified by default 1
l = "abcdefgh"
l[3:6] # def
l[3:6:2] # df
l[:] # abcdefgh
l[::-1] # hgfedcba
l[4:1:-2] # ec
# input output in python
print("Hello World!") # Hello World!
get = input("Enter a word : ") # Hi
print(get) # Hi
num = int(input("Enter a number : ")) # the default type in string in input fn
print(num) # 15 or whatever no. i give
print(type(num)) # <class 'int'>
num2 = input("Enter 2nd number : ") # 20
print(num2) # 20
print(type(num2)) # <class 'str'>
# given 5 for both num
print(num * 3) # 15
print(num2 * 3) # 555
# Newton's method to find roots eg (g^3-x=0)
x = int(input("Enter a number to find cube root:"))
g = int(input("Enter a guess to start with:"))
print("The cube of current guess is:",g**3)
next_g = g - ((g**3-x)/(3*g**2))
print("The next guess is", next_g) # finds the next guess
# F string -> formatted string bracketed by {}
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}')
# branching -> if, if else, if elif else
number = 3
guess = int(input("enter a number : "))
print(number == guess)
num = 5
guess= int(input("Guess a number : "))
if num == guess: # True / False
print("Both numbers are same")
elif num > guess:
print("Guess is too low")
else: # can use this also as elif else is not mandatory like c++ (in else if ladder)
print("Guess is too high")
# Finger Ex lecture 2
''' Assume you are given a variable named number (has a numerical value). Write a piece of Python code that prints out one of the following strings:
positive if the variable number is positive
negative if the variable number is negative
zero if the variable number is equal to zero '''
number = int(input("Enter a number : "))
if number > 0:
print(f"{number} is positive")
elif number == 0:
print(f"{number} is equal to zero")
else:
print(f"{number} is negative")
# Enter a number : 10
# 10 is positive