Skip to content

Commit 6aad585

Browse files
committed
learning
0 parents  commit 6aad585

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed

basic_progs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
print("AREA OF CIRCLE!!")
2+
3+
r = input("Radius: ")
4+
r = float(r)
5+
def area(r):
6+
pi = 3.142
7+
return pi*( r * r )
8+
print("The area of the circle of radius {1} is {0}".format(area(r), r))
9+
10+
11+
print("___________________________________")
12+
print("PRIME NUMBERS!!")
13+
14+
p = input("Prime numbers upto: ")
15+
p = int(p)
16+
for i in range (1, p+1):
17+
if i > 1:
18+
for n in range(2, p):
19+
if (i%n)==0:
20+
break
21+
else :
22+
print(i)
23+
24+
print("____________________________________")
25+
print("PATTERNS!!")
26+
27+
n = input("Number of rows:")
28+
n = int(n)
29+
for i in range(0, n):
30+
for j in range(0, i+1):
31+
print("* ", end = "")
32+
print(" ")
33+
x = 1
34+
for i in range(0, n):
35+
for j in range(0, i+1):
36+
print(x , end = "")
37+
x+=1
38+
print(" ")
39+

hello.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
msg = "Hello python world!!"
2+
print msg

learning.py

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
print("py\t""thon")
2+
print(3*"un"+"ium")
3+
print("This is cool.")
4+
word = "python data"
5+
number = 1
6+
print (word[0])#this is used to get one character from string
7+
print("The ninth character is '%s'" %word[9])
8+
print(word[-1])
9+
print(word[-2])
10+
print(word[-3])
11+
print(word[-4])
12+
print(word[1:4])#yth this is used to get one or more character from a string #this can also be used for [1,2,3] kind of strings
13+
print(word[:4])#pyth
14+
print(len(word[:4]))#this is used to find the length of the specified string
15+
print("The lenght of the string is '%s'"%len(word))#this can be used to find the length of the string
16+
print("The lenght of PRINT is '%s'"%len("print"))
17+
print(word.index("t"))#2#this is used to find the positon of the character
18+
print(word.index("p"))#0
19+
print(word.count("p"))#counts the nos of specified letters
20+
print("a occurs %d times" % word.count("a"))#occurence of the specified char
21+
print(word[2:5:2])#to #this will skip 2nd character #this used for skiping a given no of terms#[start:stop:step]
22+
print(word[2:5:3])#t #this will 2nd and 3rd character
23+
print("The reverse of the string is '%s'"%word[::-1])#this is used to reverse the string
24+
print("The characters with odd index are '%s'" %word[1::2]) #0-based indexing
25+
print(word.lower())#this is to convert it into lowercase
26+
print(word.upper())#this is to convert it into uppercase
27+
print(word.startswith("pyth"))#this will print TRUE#this is used to find if a string starts with a specified string
28+
print(word.startswith("data"))#FALSE
29+
print(word.endswith("data"))#TRUE
30+
words=word.split(" ")#this is used to split a string at the specified character 'here " "' and to make it a list
31+
print(words)
32+
if word=="python data" and number==1:#we can use boolean operators 'and' & 'or'
33+
print("The word is %s and the number is %d"%(word,number))#which allows to make us to make complex boolean expressions
34+
if number == 1 or number == 2:
35+
print("The number is either 1 or 2 ")
36+
word1 = "python"
37+
if word1 in words:#this is used to check whether the specified object is in the iterable object container
38+
print("Your word is either pyhton or data")
39+
print((not False)==(True))#True#NOT is a boolean operator which inverts the expression
40+
if not number:#this can also be use to check if the numbers is 0/1 i.e. true/false
41+
print("Number is 1")#this won't print as the number is "not" zero
42+
if number:
43+
print("The number is 1")#this will print as the number is 1
44+
45+
a,b=3,4
46+
print(a**b/a)#this is used for operations
47+
print(a==3)#True #this is used for checking if the value matches by printing if it's true/false
48+
49+
50+
print("\t\t FIBONACCI SERIES\n")
51+
a,b=0,1
52+
while a<1000:#instead of curly brakets like in c we use "Tab space" indentation(meaning tab space for keeping things in loop/function)
53+
print(a,end=',')#end=',' is used to put a comma after everything before it
54+
a,b=b,a+b#this type of equality is used for, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.
55+
56+
57+
name = "Harshit "#string
58+
name += "Soni"#adding things in a string
59+
age = 18
60+
college = "Tsec"
61+
print("\nMy name is %s"% name)
62+
print("\nMy name is %s.And i'm %d.I'm currently studying in %s"%(name, age, college))
63+
mystr=[1,2,3]#string representationn
64+
mystr += [4,5,6]#adding things in a string
65+
print("\n My string is %s"% mystr)
66+
data = ("Harshit", "soni",18, "Tsec")#data representation
67+
string = "\nMy name is %s %s.And I'm %d.I'm currently studying in %s" #string representation
68+
print(string % data)
69+
#Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.
70+
print("""\
71+
\n message:this can be used for writing multiple line of string
72+
do you wanna cont?
73+
1.yes 2.no
74+
""")#this can be used for mutliple lines of strings
75+
76+
#looping
77+
78+
x = [i for i in range(10)]#this is use for making a list using 'for' loop and variable assignment
79+
print(x)
80+
81+
for x in range(3,8):
82+
print(x)
83+
84+
for x in range(3,8,2):
85+
print(x)
86+
87+
#WHILE loop
88+
count=0
89+
while count<5:
90+
print("NUmbers using while loop %d"%count)
91+
count +=1
92+
93+
#break
94+
count= 0
95+
while 1:
96+
print("NUmbers using break while looping %d"%count)
97+
count+=1
98+
if count>=5:
99+
break
100+
101+
#contiune
102+
for x in range(10):
103+
if x % 2==0:
104+
continue #skips the choosen number/thing
105+
print("Odd numbers using contiune and for loop %d"%x)#prints odd numbers
106+
print("Number of odd numbers are %d" %count)
107+
108+
for x in range(10):
109+
if not x % 2==0:
110+
continue #skips the choosen number/thing
111+
print("Even numbers using contiune and for loop %d"%x)#prints even numbers
112+
113+
114+
# Prints out 0,1,2,3,4 and then it prints "count value reached 5"
115+
# "else" can be used with loops
116+
count=0
117+
while(count<5):
118+
print(count)
119+
count +=1
120+
else:
121+
print("count value reached %d" %(count))
122+
123+
# Prints out 1,2,3,4
124+
for i in range(1, 10):
125+
if(i%5==0):
126+
break
127+
print(i)
128+
else:
129+
print("this is not printed because for loop is terminated because of break but not due to fail in condition")
130+
131+
for i in range(10):
132+
print(i)
133+
else:
134+
print("this 10")
135+
136+
#Functions
137+
138+
def my_function():#defining a function
139+
print("This is a function.")
140+
141+
my_function()#calling a function
142+
143+
username = "har"
144+
greeting = "shit"
145+
def my_function_with_args(username, greeting):
146+
print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
147+
148+
my_function_with_args(username, greeting)
149+
150+
def adding_two_strings(username, greeting):
151+
return username + greeting
152+
print(adding_two_strings(username, greeting))
153+
154+
a = 2
155+
b = 3
156+
def sum_two_numbers(a, b):
157+
return a + b
158+
print(sum_two_numbers(a, b))
159+
160+
#classes and object
161+
class Myclass:
162+
variable = "shit"
163+
164+
def function(self):
165+
print("This mesage is inside the class.")
166+
167+
objectx = Myclass()#Now the variable "objectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".
168+
print(objectx.variable)
169+
objectx.function()#calling a function from a class
170+
171+
class MyClass:
172+
variable = "blah"
173+
174+
def function(self):
175+
print("This is a message inside the class.")
176+
177+
myobjectx = MyClass()
178+
myobjecty = MyClass()
179+
180+
myobjecty.variable = "yackity"
181+
182+
# Then print out both values
183+
print(myobjectx.variable)
184+
print(myobjecty.variable)
185+
186+
# define the Vehicle class
187+
class Vehicle:
188+
name = ""
189+
kind = "car"
190+
color = ""
191+
value = 100.00
192+
def description(self):
193+
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
194+
return desc_str
195+
196+
# your code goes here
197+
car1 = Vehicle()
198+
car1.name = "Fer"
199+
car1.color = "red"
200+
car1.kind = "convertible"
201+
car1.value = 60000.00
202+
203+
car2 = Vehicle()
204+
car2.name = "Jump"
205+
car2.color = "blue"
206+
car2.kind = "van"
207+
car2.value = 10000.00
208+
209+
# test code
210+
print(car1.description())
211+
print(car2.description())
212+
213+
#modules
214+
#modules are python files with extension .py
215+
#Modules are imported from other modules using the "import" command.
216+
217+
#packages
218+
#Each package in Python is a directory which MUST contain a special file called __init__.py.
219+
#If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar.
220+
#import foo.bar
221+
#OR
222+
#from foo import bar

sumof2nums.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print("Adding two numbers")
2+
3+
number1 = input("First number: ")
4+
number2 = input("Second number: ")
5+
6+
sum = float(number1) + float(number2)
7+
print( 'the sum of {0} and {1} is:{2}' .format(number1, number2, sum))

0 commit comments

Comments
 (0)