0% found this document useful (0 votes)
22 views13 pages

Cs Class

Uploaded by

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

Cs Class

Uploaded by

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

3/7/2024

Python = High Level Language

Algorithm = Step by step solution of any given problem

Pseudocode = Sentence based set of instructions

Program = Set of instructions written in any programming language.

Flowchart = Diagrammatic based algorithm

Variable = Named memory location whose value is changeable during the


execution of code

Code 1 – Basic Operators:

num1=int(input("Enter the first number"))

num2=int(input("Enter the second number"))

sum=num1+num2

print("sum =",sum)

product=num1*num2

print("product =",product)

power=num1**num2

print("power =",power)

flowdivision=num1//num2

print("flowdivision =",flowdivision)

division=num1/num2

print("division =",division)

multiplication=num1*num2
print("multiplication =",multiplication)
4/7/2024

Control Statement:

1. Conditional statements or selection statements


2. Iterative statements/loop/repetitive statements

8/7/2024

Questions

Q1) Write a program in python to check whether person is eligible to vote


or not.

A1) age= int(input("How old are you?"))

if (age>=18):

print("You can vote")

else:

print("You cannot vote")

Q2) Write a program in python to check given number is positive, negative


or zero.

A2) number=int(input("Enter an integer"))

if number > 0:

print("Positive")

elif number < 0:

print("Negative")

else:

print("Zero")

Q3) Write a program in python to accept two integers and print the
largest.

A3) num1 = int(input("Enter the first integer: "))

num2 = int(input("Enter the second integer: "))

if num1 > num2:

print("The largest number is",num1)


elif num2 > num1:

print("The largest number is",num2)

else:

print("Both numbers are equal")

Q4) Write a program to accept the integers and print the smallest.
(smallest.py)

A4) num1 = int(input("Enter the first integer: "))

num2 = int(input("Enter the second integer: "))

if num1 < num2:

print("The smallest number is:", num1)

elif num2 < num1:

print("The smallest number is:", num2)

else:

print("Both numbers are equal")

Q5) Write a program in python that inputs three numbers and calculate
the sums as per this (twosums.py):
Sum1 as the sum of all input numbers

Sum2 as the sum of non-duplicate numbers (ignore duplicates)

Q6) Program to calculate the amount payable after sales discount, which
is 10% up to the sales amount of 20000 and 17.5% on amounts above
that. (discount.py)

A6) sales_amount = float(input("Enter the sales amount: "))

if sales_amount <= 20000:

discount = 0.10 * sales_amount

else:

discount = 0.175 * sales_amount

amount_payable = sales_amount - discount


print("Sales Amount: Rs.", sales_amount)

print("Discount: Rs.", discount)

print("Amount Payable after discount: Rs.", amount_payable)

Q7) Write a program in python that asks the user to enter a length in cm.
If the user enters a negative length, the program should tell the user that
the entry is invalid, else the program should convert the length to inches
and print out the result. (1 inch = 2.54cm)

A7) length_cm = float(input("Enter a length in centimeters: "))

if length_cm < 0:

print("Invalid entry: length cannot be negative.")

else:

length_inch = length_cm / 2.54

print(length_cm,"cm is equal to",length_inch, "inches.")

Q8) A store charges 120rs per item if you buy less than 10 items, if you
buy between 10 and 99 items, the cost is 100rs more per item. If you buy
100 or more items, the cost is 70 rs. Write a python program that asks the
user how many items they are buying and print the cost.

A8) num_items = int(input("Enter the number of items you are buying: "))

if num_items < 10:

cost_per_item = 120

elif 10 <= num_items < 100:

cost_per_item = 120 + 100

else:

cost_per_item = 70

total_cost = num_items * cost_per_item

print("The total cost for,",num_items,"items is ₹",total_cost,".")

Q9) Write a python program that reads three numbers(integers) and print
them in ascending order (ascending.py)
A9) num1 = int(input("Enter the first integer: "))

num2 = int(input("Enter the second integer: "))

num3 = int(input("Enter the third integer: "))

smallest = min(num1, num2, num3)

largest = max(num1, num2, num3)

middle = num1 + num2 + num3 - smallest - largest

print("The integers in ascending order are:",smallest,middle,largest)

Q10) Write a python program to input length of three sides of a


triangle .then check if these sides will form a triangle or not (Rule : a+b>
c ;b+c>a ;c+a >b).

A10) side1 = float(input("Enter the length of side 1: "))

side2 = float(input("Enter the length of side 2: "))

side3 = float(input("Enter the length of side 3: "))

if side1 + side2 > side3 and side2 + side3 > side1 and side3 + side1 >
side2:

print("These sides can form a valid triangle.")

else:

print("These sides cannot form a valid triangle.")


11/7/2024

Loops

A loop is an instruction that repeats multiple times as long as some


condition is met.

While Loops – While loop is used to repeat a section of an unknown


number of times until a specific condition is met. – Syntax

while text_expression:

statement

text_expression is the criteria, it it’s true, the statement will be executed.


As soon as the criteria is false, the code will exit the look.

Q1) Write a program for the sum of integers up to ‘n’ by using while loop.

Q2) Write a program in python to find the sum of the first 20 even
numbers.

For Loops – When the iterations are known, we use a loop.

Syntax:

For variable in range(start, end):

Statements

Q1) Write a python program to find sum of the first 10 natural numbers
using for loop and the average.

A1) sum=0

for x in range(1,11):

sum +=x

a=sum/10

print("sum",sum,"average:",a)
HW (12/7/24)
Q1) Print your name 10 times.

A1) name = "Kyros"

for i in range(10):

print(name)

Q2) Print the first 10 natural numbers.

A2) for i in range(11):

print(i)

Q3) Print the sum of first n numbers.

Q4) Print the even numbers between 50 and 100.

A4) for i in range(50, 101):

if i % 2 == 0:

print(i, end=", ")

or

for i in range (50,101,2):

print(i)

Q5) Print the odd numbers from 1 to 100.

A5) for i in range(1, 101):

if i % 2 != 0:

print(i)

Q6) Print multiplication table of number n.

A6) n = int(input("Enter a number you wish to multiply: "))

for i in range(1, 11):

print(i * n, end="\t")

print()

Q7) Program to check if a number is prime or not.

Q8) Print reverse value of any given number.

Q9) Write a python program to count the number of digits in a number.

Q10) Write a python program to calculate the factorial of a number.


Q11) Write a python program to print the Fibonacci series up to n terms.

A11) n = int(input("Enter the number of terms: "))

a=0

b=1

print(a)

print(b)

for x in range(1,n-1):

c=a+b

a=b

b=c

print(c)
16/7/2024

SUBSTRINGS:

Q1) Write a Python program to calculate the length of a string.

A1) my_string = input("Enter a String")

string_length = len(my_string)

print(string_length)

Q2) Python Program to count the number of vowels in a string.

A2)

Q3) Write a Python program to get a string made of the first 2 and the last
2 chars from a given a string.

A3) string = input("Enter a string: ")

new_string = string[:2] + string[-2:]

print("Original String:", string)

print("New String:", new_string)


18/7/2024

SubStrings

- Extracting part of a string for example string “science” can be


extracted from “computer science”
- Python offers many ways to substring a string. This is often called
“slicing”.

Syntax:

string[start:end]

string[:end]

string[start:]

string[start:end:step]
Q1) Write a python program to accept a string, the program will check if a
letter is lowercase, it’ll convert it to uppercase and vice versa.

A1) user_input=input("Enter a String: ")

swapped = ""

for letter in user_input:

if letter.islower():

swapped += letter.upper()

elif letter.isupper():

swapped += letter.lower()

else:

swapped += letter

print(swapped)
19/7/2024

Round – Rounds a decimal number to the nearest positive integer.

Syntax:

round()

Random

Syntax:

import random

print(random.randint(0,9))

List/Array -
23/7/2024

Q) Write a python program that takes 10 integer values from a user,


stores them in a list and then calculates sum and average of those values.

25/7/2024

Q1) Write a python program that accepts 10 positive integers and counts
all even numbers.

Q2) Write a python program that accepts 10 positive integers and counts
all even numbers.

You might also like