0% found this document useful (0 votes)
12 views7 pages

Python Programming Basics and Examples

The document contains various Python code snippets demonstrating different programming concepts such as printing calendars, string manipulation, loops, and basic arithmetic operations. It includes examples of functions, list methods, and control flow statements like if-else and loops. Additionally, it showcases a simple calculator, a guessing game using random numbers, and string formatting techniques.

Uploaded by

pullurigowtham
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)
12 views7 pages

Python Programming Basics and Examples

The document contains various Python code snippets demonstrating different programming concepts such as printing calendars, string manipulation, loops, and basic arithmetic operations. It includes examples of functions, list methods, and control flow statements like if-else and loops. Additionally, it showcases a simple calculator, a guessing game using random numbers, and string formatting techniques.

Uploaded by

pullurigowtham
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

Could not connect to the reCAPTCHA service.

Please check your internet connection and reload to get a reCAPTCHA challenge.

#printing any month in a calendar


import calendar
y=int(input("Enter a Year: "))
m=int(input("Enter a month: "))
cal=[Link](y,m)
print(cal)

Enter a Year: 2026


Enter a month: 6
June 2026
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

#slicing
s1="Hello Python"
print(s1[::-1])

nohtyP olleH

#join and reversing


def rev_str(s):
return ''.join(reversed(s))
print(rev_str("Gowtham kumar"))

ramuk mahtwoG

#for loop
def rev_str(n):
rev=""
for char in n:
rev=char+rev
return rev
print(rev_str("Gowtham"))

mahtwoG

#for loop
l1=[1,2,3]
l2=["red","blue"]
for i in l1:
for j in l2:
print(l1,l2,end=' ')
#end=' ' is used to print the output in 1 line

[1, 2, 3] ['red', 'blue'] [1, 2, 3] ['red', 'blue'] [1, 2, 3] ['red', 'blue'] [1, 2, 3] ['red', 'blue'] [1, 2, 3]
 

#for loop
n1 = int(input("Enter the number: "))
for i in range(1, 11):
print(f"{n1} * {i} = {n1 * i}")

Enter the number: 1


1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10

# Simple Calculator

num1 = float(input("Enter first number: "))


operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
print("Division by zero")
else:
print("Invalid operator!")

print("Result:", result)

Enter first number: 2


Enter operator (+, -, *, /): +
Enter second number: 3
Result: 5.0

#while loop
i=1
while i<10:
print(i)
i+=1
if i==5:
break
print(i)

1
2
3
4
5

#using break
for j in range(1,10):
print(j)
j+=1
if j==5:
break
print(j)

1
2
3
4
5

#printing triangle using '*'


n=5
for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print()
*
* *
* * *
* * * *
* * * * *

#printing triangle using '*' in reverse order


n=5
for i in range(n,0,-1):
for j in range(1,i+1):
print("*",end=" ")
print()

* * * * *
* * * *
* * *
* *
*

#printing triangle using 'numbers'


n=5
for i in range(1,n+1):
for j in range(1,i+1):
print(i,end=" ")
print()

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

n=5
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print()

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#finding vowels in a word


word="Gowtham"
vowels=["a","e","i","o","u"]
v_count=0
for char in word:
if char in vowels:
v_count+=1
print("vowels=", char)
print("No. of Vowels = ", v_count)

print(word)

vowels= o
vowels= a
No. of Vowels = 2
Gowtham

#factorial using for loop


n=5
fact=1
for i in range(1,n+1):
fact=fact*i
print(fact)

120

#factorial using while loop


n=5
fact=1
i=1
while i<n+1:
fact=fact*i
i+=1
print(fact)

120

#finding Max discount(codechef problem)


n=int(input('Enter no. of Bils: '))
for i in range(n):
b=float(input("Enter the bill amount: "))
f=b*0.1
dis1=100
dis2=f
max_discount=max(dis1,dis2)
print(max_discount)

Enter no. of Bils: 3


Enter the bill amount: 100
100
Enter the bill amount: 1300
130.0
Enter the bill amount: 1000
100

#codechef problem
t=int(input())
for i in range(t):
x, y = map(int, input().split())
n=x*y
r=0
if n <100:
r=0
elif n<200:
r=1
elif n<300:
r=2
elif n<400:
r=3
elif n<500:
r=4
elif n<600:
r=5
elif n<700:
r=6
elif n<800:
r=7
elif n<900:
r=8
elif n<1000:
r=9
print(r)

2
10 10
1
20 4
0
#fabinocci
n=10
a=0
b=1
c=0
d=""
for i in range(n):
c=a+b
a,b=b,c
print(c, end=",")

1,2,3,5,8,13,21,34,55,89,

#Random number
import random
print([Link](1,1000))

71

#Game using Random Number


import random
i=0
while i<5:
x=int(input("Enter any number b/w 1-10: "))
y=[Link](1,10)
if x<y:
print("your number is less than random number")
elif x>y:
print("your number is greater than random number")
else:
print("You have found the secret number")
print("Secret Number is: "+str(y))
break
i+=1
c=5-i
print("You have "+str(c)+" chances left")
if c==0:
print("your chances are done")

Enter any number b/w 1-10: 3


your number is greater than random number
You have 4 chances left
Enter any number b/w 1-10: 2
your number is less than random number
You have 3 chances left
Enter any number b/w 1-10: 3
your number is less than random number
You have 2 chances left
Enter any number b/w 1-10: 2
your number is less than random number
You have 1 chances left
Enter any number b/w 1-10: 3
your number is greater than random number
You have 0 chances left
your chances are done

#string concat
s1="vishnu"
s2="Gowtham"
print(s1+s2)

vishnuGowtham

s1="Gowtham Kumar"
for i in enumerate(s1):
print(i)
(0, 'G')
(1, 'o')
(2, 'w')
(3, 't')
(4, 'h')
(5, 'a')
(6, 'm')
(7, ' ')
(8, 'K')
(9, 'u')
(10, 'm')
(11, 'a')
(12, 'r')

s1="Hi hello how are you man"


print([Link]("are"))

('Hi hello how ', 'are', ' you man')

s1="Hi hello how are you man"


print([Link]("are"))

('Hi hello how ', 'are', ' you man')

s1=" Hello python "


print([Link]())

Hello python

s1=" Hello python "


print([Link]())

['Hello', 'python']

s1=" Hello python "


print([Link]("Hello","Gowtham"))

Gowtham python

s1=" Hello python "


print([Link]())

hello python

s1=" Hello python "


print([Link]())

HELLO PYTHON

#List Methods
my_list=[1,2,3]
my_list.append(4)
print(my_list)

[1, 2, 3, 4]

my_list=[1,2,3]
my_list.extend([4,5])
print(my_list)

[1, 2, 3, 4, 5]

my_list=[1,2,3]
my_list.remove(1)
print(my_list)
[2, 3]

my_list=[1,2,3]
my_list.insert(1,10)
print(my_list)

[1, 10, 2, 3]

my_list=[1,2,3]
my_list.pop()
print(my_list)
my_list1=[1,2,3]
my_list1.pop(1)
print(my_list1)

[1, 2]
[1, 3]

my_list=[1,2,3]
my_list.reverse()
print(my_list)

[3, 2, 1]

Could not connect to the reCAPTCHA service. Please check your internet connection and reload to get a reCAPTCHA challenge.

You might also like