0% found this document useful (0 votes)
29 views9 pages

Python Unit 1 Programs 1

Uploaded by

japanserverbyvpn
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)
29 views9 pages

Python Unit 1 Programs 1

Uploaded by

japanserverbyvpn
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/ 9

Python

1. Aim:-Introduction to Python Programming.

Theory:- Python is a general-purpose interpreted, interactive, object-oriented, and


high-level programming language. It was created by Guido van Rossum during
1985- 1990. Like Perl, Python source code is also available under the GNU
General Public License (GPL). This tutorial gives enough understanding on
Python programming language.

Prerequisites
You should have a basic understanding of Computer Programming terminologies.
A basic understanding of any of the programming languages is a plus.
Python is a high-level,interpreted,interactive and object oriented scripting
language. Python is designed to be highly readable. It uses English keywords
frequently where as other languages use punctuation, and it has fewer syntactical
constructions than other languages.
• Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is similar
to PERL and PHP.
• Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
• Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
• Python is a Beginner's Language − Python is a great language for the
beginner-level programmers and supports the development to a wide range
of applications from simple text processing to WWW browsers to games.
History of Python
Python was developed by Guidovan Rossum in the late eighties and early nineties
at the National Research Institute for Mathematics and Computer Science in the
Netherlands.
Python is derived from many other languages, including ABC,Modula-
3,C,C++,Algol-68, Small Talk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
Pythonisnowmaintainedbyacoredevelopmentteamattheinstitute,although Guidovan
Rossum still holds a vital role in directing its progress.

3
Python Features
Python's features include−
• Easy-to-learn –Python has few keywords, simple
structure, and a clearly defined syntax. This allows
the student to pick up the language quickly.
• Easy-to-read−Python code is more clearly defined and visible
to the eyes.
• Easy-to-maintain−Python's source code is fairly easy-to-
maintain.
• Abroad standard library−Python's bulk of the
library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an
interactive mode which allows interactive testing
and debugging of snippets of code.
• Portable − Python can run on a wide variety of
hardware platforms and has the same interface on
all platforms.
• Extendable − You can add low-level modules to
the Python interpreter. These modules enable
programmers to add to or customize their tools to
be more efficient.
• Databases−Python provides interfaces to all major commercial
data bases.

• GUI Programming – Python supports GUI


applications that can be created and ported to many
system calls, libraries and windows systems, such
as Windows MFC, Macintosh, and the X Window
system of Unix.
• Scalable − Python provides a better structure and
support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a
big list of good features, few are listed below −
• It supports functional and structured programming methods as
well as OOP.
• It can be used as a scripting language or can be
compiled to byte-code for building large
applications.
• It provides very high-level dynamic data types and supports
dynamic type checking.
• It supports automatic garbage collection.
• It can be easily integrated with C,C++,COM, ActiveX, CORBA,
and Java.

2. AIM: To write a python program to find the largest element among


three Numbers.
# Input from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
# Logic to find the largest
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
# Output the result
print("The largest number is:", largest)
3. Write a Program to display all prime numbers within an interval
S=int(input("enter first number: ”))
E=int(input("enter last number"))
for num in range(S, E + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
break

4. Write a program to swap two numbers without using a temporary


variable.
# Taking input from the user
a = int(input("Enter the first number (a): "))
b = int(input("Enter the second number (b): "))
print(f"Before swapping: a = {a}, b = {b}")
# Swapping logic without using a temporary variable
temp = b
b=a
a = temp
print(f"After swapping: a = {a}, b = {b}")
5. Demonstrate the following Operators in Python with suitable examples.
  Arithmetic Operators
  Relational Operators
  Assignment Operators
  Logical Operators
  Bit wise Operators Ternary Operator
  Membership Operators Identity Operators

 Arithmetic Operators
print("----- Arithmetic Operators -----")
a = int(input(“enter the number:”))
b = int(input(“enter the number:”))
print("a + b =", a + b) # Addition
print("a - b =", a - b) # Subtraction
print("a * b =", a * b) # Multiplication
print("a / b =", a / b) # Division
print("a // b =", a // b) # Floor Division
print("a % b =", a % b) # Modulus
print("a ** b =", a ** b) # Exponentiation

 Relational Operators
print("\n----- Relational (Comparison) Operators -----")
a = int(input(“enter the number:”))
b = int(input(“enter the number:”))
print("a > b:", a > b)
print("a < b:", a < b)
print("a == b:", a == b)
print("a != b:", a != b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)

 Assignment Operators
print("\n----- Assignment Operators -----")
x = int(input(“enter x value for Addition: ”))
x += 12
print("x += 12 ->", x)
x = int(input(“enter x value for Subtraction Assignment: ”))
x -= 1
print("x -= 1 ->", x)
x = int(input(“enter x value for Multiplication Assignment: ”))
x *= 5
print("x *= 5 ->", x)
x = int(input(“enter x value for Division Assignment: ”))
x /= 2
print("x /= 2 ->", x)
x = int(input(“enter x value for Modulus Assignment: ”))
x %= 3
print("x %= 3 ->", x)
x = int(input(“enter x value for Exponentiation Assignment: ”))
x **= 3
print("x **= 3 ->", x)
x = int(input(“enter x value for Floor Division Assignmen: ”))
x //= 3
print("x //= 3 ->", x)
 Logical Operators
print("\n----- Logical Operators -----")
p = True
q = False
print("p and q:", p and q)
print("p or q:", p or q)
print("not p:", not p)

 Bitwise Operators
print("\n----- Bitwise Operators -----")
#a = 5 # binary: 0101
#b = 3 # binary: 0011
a = int(input(“enter the number:”))
b = int(input(“enter the number:”))
print("a & b =", a & b) # Bitwise AND
print("a | b =", a | b) # Bitwise OR
print("a ^ b =", a ^ b) # Bitwise XOR
print("~a =", ~a) # Bitwise NOT
print("a << 1 =", a << 1) # Left shift
print("a >> 1 =", a >> 1) # Right shift

 Ternary Operator
print("\n----- Ternary Operator -----")
x = int(input(“enter the number:”))
y= int(input(“enter the number:”))
# Syntax: <value_if_true> if <condition> else <value_if_false>
max_val = x if x > y else y
print("The greater number is:", max_val)

 Membership Operators
print("\n----- Membership Operators -----")
list1 = [1, 2, 3, 4, 5]
print("3 in list1:", 3 in list1)
print("7 not in list1:", 7 not in list1)

 Identity Operators
print("\n----- Identity Operators -----")
a = [1, 2, 3]
b=a
c = [1, 2, 3]
print("a is b:", a is b) # True, same object
print("a is c:", a is c) # False, different objects with same content
print("a == c:", a == c) # True, values are equal

6. Write a program to print multiplication table of a given number.

# Input from the user


num = int(input("Enter a number to print its multiplication table: "))
# Print the multiplication table from 1 to 10
print(f"\nMultiplication Table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

You might also like