0% found this document useful (0 votes)
77 views29 pages

Computer Project Akriti

The document provides an introduction to the Python programming language. It discusses that Python was created in 1991 and was influenced by ABC and Modula-3. It then describes Python as an easy to learn yet powerful object-oriented language that is also high-level, cross-platform, free, and open-source. The document also includes sample Python programs to demonstrate various concepts like pattern printing, checking if a number is prime, calculating a factorial, and more.

Uploaded by

Akriti Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views29 pages

Computer Project Akriti

The document provides an introduction to the Python programming language. It discusses that Python was created in 1991 and was influenced by ABC and Modula-3. It then describes Python as an easy to learn yet powerful object-oriented language that is also high-level, cross-platform, free, and open-source. The document also includes sample Python programs to demonstrate various concepts like pattern printing, checking if a number is prime, calculating a factorial, and more.

Uploaded by

Akriti Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

GETTING STARTED WITH

PYTHON ….

INTRODUCTION:
The word python isn’t it scary? Does it bring the image of big
reptile that we prefer to see either in jungles or zoo? Well, it’s
time to change the image. Now on, you’ll remember the word
python for its playfulness and pleasant productivity.
Python programming language was developed by Guido Van Rossum
in February 1991. Python is based on or influenced with two
programming languages:

 ABC language, a teaching language created as a replacement


of BASIC, and
 Modula-3

Python is an easy–to-learn yet powerful object-oriented


programming language. It is a very high-level programming
language yet as powerful as many other middle-level not so high-
level languages like C, C++, Java etc.
PYTHON – PLUSES:

Though Python language came into being in early 1990’s, yet it


is competing with ever-popular languages such as C, C++, Java
etc.in popularity index.

1. Easy to use: Python is compact and very easy to use object-


oriented language with very simple syntax rules. It is a very
high-level language and thus very-very programmer- friendly.
2. Expressive language: python is an expressive language- fewer
lines of code and simpler syntax.
3. Interpreted language: Python is an interpreted language, not
a compiled language. It makes Python an easy-to-debug
language and thus suitable for beginners to advanced users.
4. Completeness: For most types of required functionality is
available through various modules of python standard library.
5. Cross-platform Language: Python can run equally well on
platforms – Windows, Linux/UNIX, MacIntosh,
supercomputers, smartphones etc.
6. Free and Open-Source: Python is freely available with its
source code.
7. Variety of usage/applications: Python is being used in many
diverse fields/applications, some of which are:
 Scripting
 Web applications
 Game development etc.
PYTHON- SOME MINUSES:

1. Not the Fastest language: Python is an interpreted language


not a fully compiled one. Fully compiled languages are
faster than their interpreted counterparts. So, Python
offers faster development times but execution-times are
not that fast compared to some compiled languages.
2. Lesser Libraries than Java, C, Perl: Libraries still not
competent with languages like Java, C, Perl which in some
cases offers better and multiple solution.
3. Not Strong on Type-binding: Python interpreter is not
strong in catching ‘Typing-mismatch' issue.
4. Not easily convertible: Due to lack of syntax, python is
easy to learn but it has disadvantage too during translation
of program into other programming language.
 SOLVED PROGRAMS

1.Write a program to print the following pattern:

ANS: x = int (input (" Enter number"))

i = 1
while i <= x:

print()
j=1
while j<= i:

print ("*", end =" ")


j+=1
i+=1
2.Write a program to print following pattern:

Ans:
x= int (input (" Enter the number "))
i=1
while i<=x:
print ()
j=1
while j<=i :

print ( i , end = " ")

j+=1
i+=1
3. Write a program to print factorial of a number.
Ans:
num = int (input ("Enter the number : "))

fact = 1

a=1
while a <= num:
fact*=a
a+=1

print ("The factorial of ", num,"is",fact)


OUTPUT:
Enter the number : 7

The factorial of 7 is 5040


4. Write a program to check whether a
number is Armstrong or not.
Ans:
#check Armstrong of n digits

num = int (input (" Enter any number : "))


order = len (str (num))

sum = 0
temp = num

while temp>0:
dig = temp % 10
sum+=dig**order

temp//=10
if num ==sum:

print (num, " is an Armstrong number")

else:
print (num, " is not an Armstrong number ")

# check Armstrong number for 3 digits no

num = int (input (" Enter a three-digit number : "))

sum = 0
temp = num

while temp>0:
dig = temp % 10
sum+=dig**3
temp//=10
if num ==sum:
print (num, " is an Armstrong number")

else:
print (num, " is not an Armstrong number ")

OUTPUT:
5. Write a program to check whether number
is palindrome or not.
Ans:
num = int (input (" Enter number : "))
num1=num
rev = 0
while num1>0:
dig = num1%10
rev = rev *10 +dig
num1//=10
if num==rev:
print (num, "is a palindrome ")
else:
print (num, " is not a palindrome ")

OUTPUT:
6. Write a program to print Fibonacci series.
Ans:
n = int (input (" How many terms? : "))
first = int (input ("Enter the first number : "))
second = int (input (" Enter the second number: "))
print ("\n Fibonacci series is:")
print (first, ",", second, end = " ,")
for i in range (2, n):
next = first + second
print (next, end = " ," )
first = second
second = next

Output:
7. Write a program to check whether a
number is prime or not.
Ans:
num = int (input (" enter any number : "))
if num <= 1:
print (num,"is not a prime number")
else:
for i in range (2, num):
if (num % i) == 0:
print (num, "is not a prime number")
break
else:
print (num," is a prime number ")

OUTPUT:
8. Write a program to check number is
positive, zero or negative.
Ans:
number = int (input ("Enter the number :"))
if number<0:
print (number, "is a negative number")
elif number == 0:
print (number, "is equal to zero")
else:
print (number, "is a positive number ")

OUTPUT:
9. Write a program to reverse a number.
Ans:
#python program to reverse a number using while
loop
n = int (input ("Enter the number you want to
reverse : "))
rev = 0
while n>0:
rem = n%10
rev = (rev*10) + rem
n = n//10
print ("reverse number is ", rev)

Output:
10. Write a program to check whether a person
is eligible for vote or not.
Ans:
age = int (input ("Enter your age"))
name = input ("Enter your name ")
if age>=18:
print (name, "Congratulations, you are eligible
for vote")
else:
print (name, "Sorry, you are not eligible for
vote")

OUTPUT:
11. Write a program to check whether year is
leap year or not.
Ans:
year = int (input ("Enter the year"))
if year% 4==0:
print ("Year", year, "is a leap year ")
else:
print ("Year", year, "is not a leap year ")

OUTPUT:
12. Write a program to calculate the salary.
Ans:
salary = int (input ("Enter your salary :"))
name = input ("Enter your name : ")
if salary>= 40000:
bonus =2000
else:
bonus = 1000
gs= salary + bonus
print (name, " , Your salary is ", gs , "and bonus
is ", bonus)

OUTPUT:
13. Write a program to calculate electericity
bill.
Ans:
name=input ("Enter the name")
units=int (input ("enter the units consumed"))
if units <=200:
costperunit=6
amount = units*6
elif units<=300:
costperunit= 7
amount=(200*6) +(units-200) *7
elif units<=400:
costperunit=8
amount=(200*6)+(200*7)-(units-300) *8
else:
costperunit =9
amount= (200*6) +(200*7)+(200*8) -(units-400)*9
print (name, ", Your electricity bill is Rs. ", amount)
OUTPUT:

14. Write a program to calculate marks.


Ans:
subject1=int (input ("Enter marks of Subject 1"))
subject2=int (input ("Enter marks of Subject 2"))
subject3=int (input ("Enter marks of Subject 3 "))
subject4=int (input ("Enter marks of Subject 4"))
subject5=int (input ("Enter marks of Subject 5"))
total
=subject1+subject2+subject3+subject4+subject5
per=total/5
if per>=90:
grade= 'A'
elif per>=80:
grade= 'B'
elif per>=70:
grade='C'
elif per >=60:
grade='D'
else:
grade='E'
print ("For percentage", per, "the Grade is",
grade)

OUTPUT:
15. Write a program to calculate quadratic
equation: ax2 + bx +c = 0 (a≠0)
Ans:
import math
a=int (input ("Enter the value of a:"))
if a==0:
print ("The value of 'a' cannot be 0. Enter
another value.")
else:
b=int(input("Enter the value of b :"))
c=int(input("Enter the value of c :"))
D=(b**2) -(4*a*c)
if D>0:
X= (-b+math.sqrt(D))/(2*a)
Y= (-b-math.sqrt(D))/(2*a)
print (" The discriminant is ", D," \n so
the real and unequal")
print ("Thus, the roots are: x=", X, "or x
=", Y)
elif D==0 :
X= (-b)/(2*a)
Y= (-b)/(2*a)
print (" The discriminant is ", D," \n so the
real and equal")
print ("Thus, the roots are: x=", X,"or x
=", Y)
else:
print (" The discriminant is ", D," \n so no
real roots exist")

OUTPUT:
16. Write a program to find number of years,
months and days.
Ans:
day=int (input ("enter no. of days"))
y= day//365
r1=day%365
w=r1//30
d=r1%7
print(y,"years",w,"months",d,"days")

OUTPUT:
17. Write a program to find sum of following
series:
i. S = 1 + x + x2 + x3 …+ xn
Ans:
x = int (input ("Enter the value of x:"))
n = int (input ("Enter the value of n (for x**n):"))
s=0
for a in range (n+1):
s+=x**a
print ("sum of first ",n,"terms : ", s)

OUTPUT:
ii. S = 1- x + x2 - x3 +...xn
Ans:

x = int (input ("Enter the value of x:"))

n = int (input ("Enter the value of n (for x**n):"))

s=0

sign = 1

for a in range (n+1):

term= (x**a) *sign

s+=x**a

sign*= -1

print ("sum of first ",n,"terms : ", s)

OUTPUT:
iii. S = 1 + x1 + x2 + x3 …+ xn
Ans:
x = int (input ("Enter the value of x:"))
n = int (input ("Enter the value of n (for x**n):"))
s=0
for a in range (n+1):
s+=x**a
print ("sum of first ",n,"terms : ", s)

OUTPUT:
iv. S = x+ x2 /2! - x3 /3! + x4 /4! -...xn /n!
Ans:
x = int (input ("Enter the value of x:"))
n = int (input ("Enter the value of n (for x**n):"))
s=x
sign=1
for a in range (2, n+1):
f=1
for i in range (1, a+1):
f*=i
term = ((x**a) *sign)/f
s+=term
sign*=-1
print ("sum of first ",n,"terms : ", s)

Output:
BIBLOGRAPHY
 INTERNET
 COMUTER SCIENCE WITH PYTHON BY
SUMITA ARORA

You might also like