Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Lecture 10 Python Programs

The document is a Python programming tutorial authored by Dr. Aijaz Ahmed Arain, covering various basic programming concepts. It includes examples such as adding two numbers, swapping variables, finding square roots, generating random numbers, and converting units. Additionally, it demonstrates functions for checking odd/even numbers, calculating areas, and implementing a simple calculator.

Uploaded by

fawad shaikh
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)
3 views

Lecture 10 Python Programs

The document is a Python programming tutorial authored by Dr. Aijaz Ahmed Arain, covering various basic programming concepts. It includes examples such as adding two numbers, swapping variables, finding square roots, generating random numbers, and converting units. Additionally, it demonstrates functions for checking odd/even numbers, calculating areas, and implementing a simple calculator.

Uploaded by

fawad shaikh
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/ 16

Quaid-e-Awam University of Engineering,

Science and Technology

Introduction to Computing
(Python Programming)

Dr. Aijaz Ahmed Arain


Assistant Professor
Ph.D. Information Technology
M.S. Information Technology
M.Sc. Computer Technology
B.Sc. Computer Technology

Department of Computer Science


Add Two Numbers
num1 = 1.5
num2 = 6.3

sum = float(num1) + float(num2)

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))


Swap Two Variables
x=5
y = 10
temp = x
x=y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
Find the Square Root
num = 8

#num = float(input('Enter a number: '))

num_sqrt = num ** 0.5


print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
Generate a Random Number
# importing the random module

import random

print(random.randint(0,9))
Convert Kilometers to Miles
kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac

print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))


Check if a Number is Odd or Even
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

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


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Find the Sum of Natural Numbers
num = 5
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Convert Decimal to Binary, Octal and Hexadecimal
dec = 15
print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
Find ASCII Value of Character
ord()
c = ‘a'
print("The ASCII value of '" + c + "' is", ord(c))
_________________________________________________
chr()
>>> chr(65)
'A'
>>> chr(ord('S') + 1)
'T'
Calculate the Area of a Triangle
a=5
b=6
c=7
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Calculate the Area of a Triangle

s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
Find the Factors of a Number

# This function computes the factor of the argument passed


def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0: Do your self
print(i)
num = 10
print_factors(num
def add(x, y): Simple Calculator
return x + y
choice = input("Enter choice(1/2/3/4): ")
def subtract(x, y): num1 = float(input("Enter first number: "))
return x - y num2 = float(input("Enter second number: "))

def multiply(x, y): if choice == '1':


return x * y print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
def divide(x, y): print(num1,"-",num2,"=", subtract(num1,num2))
return x / y elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
print("Select operation.") elif choice == '4':
print("1.Add") print(num1,"/",num2,"=", divide(num1,num2))
print("2.Subtract") else:
print("3.Multiply") print("Invalid input")
print("4.Divide")
Sort words alphabetically
my_str = "Hello this Is an Example With cased letters"

words = my_str.split()

words.sort()

print("The sorted words are:")

for word in words:


print(word)
16

You might also like