[Go to site: main page, start]

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

Python Programming Basics and Examples

The document provides a comprehensive overview of various programming concepts in Python, including conditional statements (if, else, elif), loops (for, while), and functions. It includes examples for checking conditions, calculating factorials, generating Fibonacci series, and determining even or odd numbers. Additionally, it covers list operations, string manipulations, and file handling, along with user input scenarios and basic mathematical operations.

Uploaded by

24022170
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 views16 pages

Python Programming Basics and Examples

The document provides a comprehensive overview of various programming concepts in Python, including conditional statements (if, else, elif), loops (for, while), and functions. It includes examples for checking conditions, calculating factorials, generating Fibonacci series, and determining even or odd numbers. Additionally, it covers list operations, string manipulations, and file handling, along with user input scenarios and basic mathematical operations.

Uploaded by

24022170
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

Some code related questions:

If statement
age = 20 // age =10 ( if 20 is given it will print first sop, if 10 then second sop )

# Checking if age is greater than 15

if (age > 15):

print("age", age, "is greater than 15")

print("Program ended as age is lesser than 15")

(if I will write age = 10, then only second line will print)

If else statement
value = -10

if (value>0):

print("positive integer")

else:
print("negative integer")

by taking user input:

a = input("enter value of a")

b= input("enter value of a")

if (a==b):

print(True)

else:

print(False)

using logical & operator

Using Logical Operators with If..Else: We can combine multiple conditions using logical
operators such as and, or, and not.

age = 4

age = 4 // by changing value of age result can change

experience = 10

# Using > & 'and' with IF..ELSE

if age > 23 and experience > 8:

print("Eligible.")

else:

print("Not eligible.")

if…elif…else Statement

i = 25

# Checking if i is equal to 10
if (i == 10):

print("i is 10")

# Checking if i is equal to 15

elif (i == 15):

print("i is 15")

# Checking if i is equal to 20

elif (i == 20):

print("i is 20")

# If none of the above conditions are true

else:

print("i is not present")

Looping statements

For loop:

for i in range(3):

print(i)

for i in range(3):

print("welcome")

range()
To understand for loop we need to first understand about range() pre defined function

range(5) range(1,5) range(1,5,2)


Start=0 Start=1 Start=1
Condition<5 Condition<5 Condition<5
Increment=1 Increment=1 Increment=2
for i in range(5): for i in range(1,5): for i in range(1,5,2):
print(i) print(i) print(i)
Result: 0,1,2,3,4 Result: 1,2,3,4 Result: 1,3

How to do reverse using loop


Range(10,0,-1)

10 is where from to start

0=condition where to stop

-1 means to decrement

Table for 2 using for Loop


Using for loop
for a in range(1,11):

print("2*",a,"=",a*2)

using while loop

while loop

i=1

while i<10:

print(i)

i=i+1 # if we dnt write inc/dec loop will never exist


Factorial
Finding factorial of any number
Here num+1 is consider in range, as we also take 1 less value in range that why
we have added 1
eg: range(1,7): it means value will be from 1,2,3,4,5,6 only 7 will not be
included so for this reason i am taking num+1

Now suppose you want to take user input and ask for conditions also
Fibonacci Series
Program to find Fibonacci Series of given number

Suppose you want conditions to be included:


Program to determine whether a person is eligible to vote or not
age=int(input("enter age "))

print(age)

if(age>18):

print("age is greater")

else:

print("age is lesser than 18")

Program to find whether the given number is even or odd


a=30

b=5

c=a%b

print(c)

d=a/b

print(d)

number=20

if(number%2==0):

print("even")

else:

print("odd")
Program to determine whether the character entered is a vowel or
no
value=input(“enter the character you want to check : “)

print(“the 8ntered chatecter is : “, value)

if(value==’a’or value==”e” or value==”I” or value==”o” or value==”u”):

print(“value is your vovel”)

else:

print(“value is not vovel”)

[Link]
List Programs
Program to calculate the sum and average of first 10
numbers

• Using sum() pre defined method

• Using for loop


print("enter values")

sum=0

for a in range(1,11):

print(a)

sum = sum + a

print("total sum is: ", sum)

average=sum/10

print(average)
Program to enter a number and then calculate the sum of its
digits.
a = input("Enter Number: ")

sum = 0
for i in a:
sum = sum + int(i)
print(sum)
Program to print the multiplication table of n, where n value
is entered by user

value=int(input("enter value"))

for a in range(1,11):

print(value,"*",a,"=",a*value)

Write a program to print if number is positive negative or zero


Write a program using lambda function to print a message ‘even’ if
number is even and ‘odd’ if number is odd.

Program to display power of a number using formatting characters and


without formatting characters

Program that uses split() to split a multiline string


Program that counts the occurrences of a character in a string. Do not use built
in function.

Reverse a string using pre defined thing in python


Program to reverse of string by user defined function (make a function for this
program)

Check palindrome: Using slicing

Program to access a file after it is closed


Program to split the line into a series of words and use space to
perform the split operation.

1. One way without using file handling

2. Second is with file handling


Program that reads data from a file and calculates the percentage
of vowels and consonants in the file

Rest you can do urself:


Program to calculate area of square and rectangle using a class.
Write a program to create a class ‘Person’ with two attributes. Display the details using
object.

Seek() and tell()

operations of list-code (atleast 8)


operation on string-code (atleast 8)

rest u can check the questions on sppu question bank given to u people

You might also like