0% found this document useful (0 votes)
8 views5 pages

Python Programming Basics Guide

Programming

Uploaded by

addanabbasi000
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)
8 views5 pages

Python Programming Basics Guide

Programming

Uploaded by

addanabbasi000
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

Python Programming Core Concepts

1. Variables
1. Store your name, age, and city in variables and print them.

name = 'Ali'
age = 17
city = 'Islamabad'
print(name)
print(age)
print(city)

2. Store the side of a square in a variable and print its area.

side = 5
area = side * side
print('Area of square =', area)

3. Store marks of 3 subjects and print the total.

math = 80
english = 75
science = 90
total = math + english + science
print('Total marks =', total)

4. Store the names of two friends in variables and print them in one line.
(Practice Question)

5. Store the price of a pen and a notebook in variables and print their combined cost.
(Practice Question)

2. Input/Output Functions
1) Take your name as input and display a greeting message.

name = input('Enter your name: ')


print('Welcome,', name)

2) Ask the user to enter two numbers and print their sum.

a = int(input('Enter first number: '))


b = int(input('Enter second number: '))
print('Sum =', a + b)

3) Take input for a student’s name and marks, then display both.
name = input('Enter student name: ')
marks = input('Enter marks: ')
print('Student:', name, '- Marks:', marks)

4) Ask the user for their city and print a welcome message with the city name.
(Practice Question)

5) Input the length and width of a rectangle and print its area. (Practice Question)

3. Conditional Structure
I. Check whether a number is positive or negative.

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


if num > 0:
print('Positive number')
else:
print('Negative number')

II. Check whether a number is even or odd.

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


if num % 2 == 0:
print('Even number')
else:
print('Odd number')

III. Input marks and print grade according to given conditions.

marks = int(input('Enter marks: '))


if marks >= 80:
print('A Grade')
elif marks >= 60:
print('B Grade')
else:
print('C Grade')

IV. Check whether the entered year is a leap year or not. (Practice Question)

V. Check whether a person is Adult or Minor based on age. (Practice Question)

4. Iterative Structure (Loops)


1. Print numbers from 1 to 10 using a for loop.

for i in range(1, 11):


print(i)

2. Print the table of 5 using a for loop.

for i in range(1, 11):


print('5 x', i, '=', 5* i)
3. Print the sum of numbers from 1 to 5 using a loop.

sum = 0
for i in range(1, 6):
sum += i
print('Sum =', sum)

4. Print even numbers from 2 to 20 using a while loop. (Practice Question)

5. Display countdown from 10 to 1 using a while loop. (Practice Question)

5. Functions
1) Write a function greet() that prints 'Hello Students!'.

def greet():
print('Hello Students!')
greet()

2) Write a function square() that takes a number and prints its square.

def square(num):
print('Square =', num * num)
square(4)

3) Write a function add() that takes two numbers and returns their sum.

def add(a, b):


return a + b
result = add(10, 5)
print('Sum =', result)

4) Write a function average() that takes three marks and prints the average.
(Practice Question)

5) Write a function check_even() that checks if a number is even or odd.


(Practice Question)

6. Lists
I. Create a list of 5 city names and print the list.

cities = ['Islamabad','Lahore','Karachi','Quetta','Peshawar']
print(cities)

II. Create a list of 4 numbers and print the first and last element.

numbers = [10,20,30,40]
print('First element:', numbers[0])
print('Last element:', numbers[-1])

III. Create a list of colors, add a color and remove one.


colors = ['Red','Blue','Green']
[Link]('Yellow')
[Link]('Blue')
print(colors)

IV. Find the largest number in a list of five values. (Practice Question)

V. Count how many times a specific element appears in a list. (Practice Question)

7. Buggy Programs
1. Fix the error:
a = 10
b=5
print('Sum is' a + b)

a = 10
b = 5
print('Sum is', a + b)

2. Fix the bug:


for i in range(1,5)
print(i)

for i in range(1,5):
print(i)

3. Fix the error:


if num = 10:
print('Equal')

if num == 10:
print('Equal')

4. Correct the function definition: (Practice Question)


def add(a,b)
return a + b

5. Correct the print statement: (Practice Question)


print('My age is' + 18)

Excercise
A. Take 3 subject marks as input, calculate total and average, and print.

sub1 = int(input('Enter marks of Subject 1: '))


sub2 = int(input('Enter marks of Subject 2: '))
sub3 = int(input('Enter marks of Subject 3: '))
total = sub1 + sub2 + sub3
average = total / 3
print('Total Marks =', total)
print('Average =', average)

B. Take a number as input and print its multiplication table.

num = int(input('Enter number: '))


for i in range(1, 11):
print(num, 'x', i, '=', num*i)

C. Function that takes a list of 5 numbers, calculates sum, and prints it.

def list_sum(numbers):
total = 0
for n in numbers:
total += n
print('Sum of list =', total)
nums = [5,10,15,20,25]
list_sum(nums)

D. Print if a number is positive, negative, or zero using a function. (Practice Question)

E. Input 3 student names into a list and print each with welcome message.
(Practice Question)

You might also like