1+ if 5 > 3 :
2+ print ("correct" )
3+ x = "awesome"
4+
5+ def myfunc ():
6+ print ("Python is " + x )
7+
8+ myfunc ()
9+
10+ x = 1
11+ y = 2.8 # float
12+ z = 1j # complex
13+
14+ #convert from int to float:
15+ a = float (x )
16+
17+ #convert from float to int:
18+ b = int (y )
19+
20+ #convert from int to complex:
21+ c = complex (x )
22+
23+ print (a )
24+ print (b )
25+ print (c )
26+
27+ print (type (a ))
28+ print (type (b ))
29+ print (type (c ))
30+
31+ import random
32+
33+ print (random .randrange (1 , 10 ))
34+
35+ b = "Hello, World!"
36+ print (b [2 :5 ])
37+
38+ b = "Hello, World!"
39+ print (b [- 5 :- 2 ])
40+
41+ # Check if the phrase "ain" is present in the following text:
42+
43+ txt = "The rain in Spain stays mainly in the plain"
44+ x = "ain" in txt
45+ print (x )
46+
47+ # Check if the phrase "ain" is NOT present in the following text:
48+
49+ txt = "The rain in Spain stays mainly in the plain"
50+ x = "ain" not in txt
51+ print (x )
52+
53+
54+ # The format() method takes unlimited number of arguments, and are placed into the respective placeholders:
55+
56+ # Example
57+ quantity = 3
58+ itemno = 567
59+ price = 49.95
60+ myorder = "I want {} pieces of item {} for {} dollars."
61+ print (myorder .format (quantity , itemno , price ))
62+
63+ txt = "We are the so-called \" Vikings\" from the north."
64+ print (txt )
65+
66+ # # CODE 1
67+ # # pyramid pattern
68+
69+ # def printPattern(n) :
70+ # k = 0
71+ # for i in range(1,n) : #row 6
72+
73+ # # Print spaces
74+ # for j in range(i,n-1) :
75+ # print(' ', end='')
76+
77+ # # Print #
78+ # while (k != (2 * i - 1)) :
79+ # if (k == 0 or k == 2 * i - 2) :
80+ # print('*', end='')
81+ # else :
82+ # print(' ', end ='')
83+ # k = k + 1
84+ # k = 0;
85+ # print ("") # print next row
86+
87+ # # print last row
88+ # for i in range(0, 2 * n -3) :
89+ # print ('*', end = '')
90+
91+ # # Driver code
92+ # n = 10
93+ # printPattern(n)
94+
95+ # print("\n")
96+ #CODE 2
97+ # Returns sum of n + nn + nnn + .... (m times)
98+ # def Series(n):
99+
100+ # # Converting the number to string
101+ # str_n = str(n)
102+
103+ # # Initializing result as number and string
104+ # sums = n
105+ # sum_str = str(n)
106+
107+ # # Adding remaining terms
108+ # for i in range(1, n):
109+
110+ # # Concatenating the string making n, nn, nnn...
111+ # sum_str = sum_str + str_n
112+
113+ # # Before adding converting back to integer
114+ # sums = sums + int(sum_str)
115+
116+ # return sums
117+
118+ # # Driver Code
119+ # n = 2
120+ # total = Series(n)
121+ # print(total)
122+
123+
124+ # #CODE 3
125+ # # Python program to count squares between a and b
126+
127+ # def CountSquares(n,a, b):
128+
129+ # cnt = 0 # initialize result
130+
131+ # # Traverse through all numbers
132+ # for i in range (a, b + 1):
133+ # j = 1;
134+ # while j ** n <= i:
135+ # if j ** n == i:
136+ # cnt = cnt + 1
137+ # j = j + 1
138+ # i = i + 1
139+ # return cnt
140+
141+ # # Driver Code
142+ # n=2
143+ # a = 49
144+ # b = 65
145+ # print ("Count of squares is:", CountSquares(n,a, b) )
146+
147+ #CODE 4
148+
149+ def checkPattern (string , pattern ):
150+
151+ # len stores length of the given pattern
152+ l = len (pattern )
153+
154+ # if length of pattern is more than length
155+ # of input string, return false;
156+ if len (string ) < l :
157+ return False
158+
159+ for i in range (l - 1 ):
160+
161+ # x, y are two adjacent characters in pattern
162+ x = pattern [i ]
163+ y = pattern [i + 1 ]
164+
165+ # find index of last occurrence of
166+ # character x in the input string
167+ last = string .rindex (x )
168+
169+ # find index of first occurrence of
170+ # character y in the input string
171+ first = string .index (y )
172+
173+ # return false if x or y are not present
174+ # in the input string OR last occurrence of
175+ # x is after the first occurrence of y
176+ # in the input string
177+ if last == - 1 or first == - 1 or last > first :
178+ return False
179+
180+ # return true if
181+ # string matches the pattern
182+ return True
183+
184+ # Driver Code
185+ string = "a rabbit jumps joyfully"
186+ first = "a"
187+ second = "j"
188+ pattern = first + second
189+ print (checkPattern (string , pattern ))
190+
191+ #CODE 5
192+
193+ # import datetime
194+
195+ # def is_date_the_nth_friday_of_month(nth, date=None):
196+ # #nth is an integer representing the nth weekday of the month
197+ # #date is a datetime.datetime object, which you can create by doing datetime.datetime(2016,1,11) for January 11th, 2016
198+
199+ # if not date:
200+ # #if date is None, then use today as the date
201+ # date = datetime.datetime.today()
202+
203+ # if date.weekday() == 4:
204+ # #if the weekday of date is Friday, then see if it is the nth Friday
205+ # if (date.day - 1) // 7 == (nth - 1):
206+ # #We use integer division to determine the nth Friday
207+ # #if this integer division == 0, then date is the first Friday,
208+ # # if the integer division is...
209+ # # 1 == 2nd Friday
210+ # # 2 == 3rd Friday
211+ # # 3 == 4th Friday
212+ # # 4 == 5th Friday
213+ # return True
214+
215+ # return False
216+ # date=2,2020
217+ # is_date_the_nth_friday_of_month(12, date)
0 commit comments