0% found this document useful (0 votes)
4 views4 pages

Python Snippets

The document includes various Python programs demonstrating data types, arithmetic operations, swapping numbers, reversing a number, and displaying digits. It also illustrates the use of 'pass' and 'continue' statements in loops. Each section contains code snippets along with their outputs.

Uploaded by

bhavya.ranaa
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)
4 views4 pages

Python Snippets

The document includes various Python programs demonstrating data types, arithmetic operations, swapping numbers, reversing a number, and displaying digits. It also illustrates the use of 'pass' and 'continue' statements in loops. Each section contains code snippets along with their outputs.

Uploaded by

bhavya.ranaa
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

#Program to print different data types

a=10
b="Hi! Welcome to Python Programming."
c=3.14
d=[12,'S',"Some",1.33]
e=(12,"Jack")
f={"Eye":1,"Nose":2}
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

<class 'int'>
<class 'str'>
<class 'float'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

#Program to add two numbers


a=10
b=20
print(a+b)
#concatenates two numbers
a=input("Enter first number: ")
b=input("Enter second number: ")
print(a+b)
#sum of 2 numbers
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print(a+b)

30
Enter first number: 11
Enter second number: 22
1122
Enter first number: 11
Enter second number: 22
33
#Swap
print ('Enter a choice 1 or 2')
ch=int(input())
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
if ch==1:
t=a
a=b
b=t
print("a and b: ",a,b)
elif ch==2:
a=a+b
b=a-b
a=a-b
print("a and b: ",a,b)

Enter a choice 1 or 2
2
Enter first number: 1
Enter second number: 2
a and b: 2 1

#Program to reverse a number and display the sum of the digits


rev=0
sum=0
a=1984
while a>0:
r=a%10

rev=rev*10+r
sum=sum+r
a//=10
print(sum,rev)

22 4891

#Program to display 1st 10 digits


print("Printing... 1 to 10")
for i in range(1,11):
print(i)
print("Odd numbers from 1 to 10")
for i in range(1,11,2):
print(i,end=" ")
print("\nEven numbers from 1 to 10")
for i in range(2,11,2):
print(i,end=" ")

Printing... 1 to 10
1
2
3
4
5
6
7
8
9
10
Odd numbers from 1 to 10
13579
Even numbers from 1 to 10
2 4 6 8 10

# Example of pass
for char in "aeiou":
if char == "i":
print("Pass executed")
pass # Does nothing, execution continues
print(char)

print()

# Example of continue
for char in "aeiou":
if char == "i":
print("Continue executed")
continue # Skips the rest of the loop for this iteration
print(char)

a
e
Pass executed
i
o
u

a
e
Continue executed
o
u

Python Loops - Sanfoundry

You might also like