How To Use Variables, Input , Control Structures In Python3 Examples

1. Simple Variable Example.

a=10
b=20.6
c="hello"


print(a)
print(b)
print(c)

2. Simple Example To Show How To Assign Same Value To Multiple Variable.

a=b=c=12
print(c)
print(b)
print(a)

3. Simple Example To Show How To Assign Multiple Values To Multiple Variable At A Time.

a,b,c = 7,7.5,"test"
print(a)
print(b)
print(c)

4. Simple Example To Show How To Take Input From Console.

var1 = int(input("enter a number"))
print (var1)

5. Simple Example To Show How To Take Input From Console And Perform Addition.

a=int(input("enter a value"))
b=int(input("enter b value"))
c=a+b
print(c)

Note:-int() for converting string input to integer, you can float() for converting string data to float

6. Simple Example To Show The Use Of If.

a=10
b=20
if(a>b):
	print("A is greater")
print("End")

7. Simple Example To Show The Use Of If Else.

a=100
b=20
if(a>b):
	print("A is greater")
else:
	print("B is greater")
print("End")	

8. Simple Example To Show The Use Of If Elif.

a=10
b=20
c=30

if((a>b)&(a>c)):
	print("A is greater")
elif(b>c):
	print("B is greater")
else:
	print("C is greater")
print("End")	

9. Simple Example To Show The Use Of range().

print(range(10))

print(range(5,10))

10. Simple Example To Show The Use Of for loop.

#For Loop Example
for i in range(10):
	print(i)

Note:-# is meant for commenting the code in python

11. Simple Example To Show The Use Of while loop.

a=1
while(a<10):
	print("\t",a)
	a=a+1

12. Simple Example To Display Odd and Even Numbers Separately.

a=1
while(a<10):
	if(a%2==0):
		print("\t\t",a)
	else:
		print(a)
	a=a+1

13. Simple Example To Show The Use Of Nested For Loop.

#For Loop Example
for i in range(10):
	for j in range(10):
		print("*")
	print("\n")	
	

14.How To Use Ternary Operator?.

#Ternary Operator Example
a,b=25,3
min=a if a