Pythons Basic Programs
Dive into our collection of Python programming snippets. Python's simple syntax and readability make it an excellent choice for beginners, while its powerful libraries make it a top language for data science, web development, and automation. The programs below cover essential Python topics including list comprehensions, file handling, string manipulation, and functional programming concepts. Use these examples to quickly master Python programming techniques.
Code Snippet
#Pythone Single Line comments
""""
Python Multiline Comments
"""
'''
Python Multiline Comments
'''
#Python Input
name=input("Enter name")#this is input funciton
#Python Output Statement
print(name)
#by default all inputs in string format
#type casting
age=int(input("Enter Age"))
print(age)
#it returns the given variable data type
print(type(age))
Code Snippet
#Take a person data and print that info(code,name,age,course,city)
code=input("Enter Code")
name=input("Enter Name")
age=int(input("Enter Age"))
course=input("Enter Course")
city=input("Enter City")
print("Code:"+code)
print("Name:"+name)
print("Age:"+str(age))
print("Age:",age)
print("Course:"+course)
print("City:"+city)
Data Types
Code Snippet
#if you are assign any data to variables it takes appropriate data type
code=100
name="SrinivasaRao"
age=35
course="Python"
fees=3000.50
is_active=True
person_id=9999j
#type function return the data type
print(type(code))
print(type(name))
print(type(age))
print(type(course))
print(type(fees))
print(type(is_active))
print(type(person_id))
print(type([1,2,3,4]))
print(type((1,2,3,4)))
print(type({1,2,3,4}))
print(type({"1":1,"2":2,"3":3,"4":4}))
Code Snippet
#dynamically inputs with type cast
code=int(input("Enter Code"))
name=input("Enter Name")
age=int(input("Enter Age"))
course=input("Enter Course")
fees=float(input("Enter Fees"))
is_active=bool(input("Enter Status (True/False"))
# person_id=9999j
person_id=complex(input("Enter Complex Value"))
#type function return the data type
print(type(code))
print(type(name))
print(type(age))
print(type(course))
print(type(fees))
print(type(is_active))
print(type(person_id))
Variables
Code Snippet
#w.a.p to assing values and print string
print("Welcome to Python First Program") #this is python output statment
code = 100
name = 'SrinivasaRao.K'
place = "Ongole"
salary = 45500.50
# str function can change any format to string format
strtxt = "My Name is " + name + " and Place is " + place + " MY code is " +\
str(code) + " My Salary is " + str(salary)
print(strtxt)
print("My Name is ", name, " and Place is ", place, " MY code is ", code ,
" My Salary is ", salary)
print(type(code))
print(type(name))
print(type(place))
print(type(salary))
Code Snippet
# w.a.p to assign values and print those
code = 100
name = "SrinivasaRao.K"
place = "Ongole"
salary = 45500.50
#Single Line output
print("My Name is " + name, end=" " )
print("Place is " + place, end=" ")
print("MY code is " + str(code), end=" ")
print("My Salary is " + str(salary)+"\n")
#individual Lines Output
print("My Name is " + name)
print("Place is " + place)
print("MY code is " + str(code))
print("My Salary is " + str(salary))
Code Snippet
#dynamical type casting
froot_name=input("Enter Froot Name")
qty=int(input("Enter Qty"))
rate=float(input("Enter Unit Price"))
total_amount=qty*rate
print("Froot Name " + froot_name + " Qty " + str(qty) +
" Unit Price" + str(rate) + " Total Amount is "
+ str(total_amount))
#while calculation only type casting
froot_name=input("Enter Froot Name")
qty=input("Enter Qty")
rate=input("Enter Unit Price")
total_amount=int(qty)*float(rate)
print("Froot Name " + froot_name + " Qty " + qty
+ " Unit Price" + rate + " Total Amount is "
+ str(total_amount))
Operators
Code Snippet
#Arithmatic Operators
# a=45
# b=2
a = int(input("Enter a value"))
b = int(input("Enter b value"))
print("A + B=", a+b) # Addition
print("{}+{}={}".format(a, b, a+b))
print("A - B=", a-b) # Subtraction
print("A X B=", a*b) # Multiplication
print("A/B=", a/b) # Division
print("A//B=", a//b) # Rounded to floor
print("A%B=", a%b) # Moduls
print("2 Power 8=", 2**8) # Exponentiation**
Code Snippet
#Assignment Operators
#exp1=exp1+exp2 => exp1+=exp2
a=5
a+=5 #a=a+5
print(a)
a-=5 # a=a-5
print(a)
a*=5 # a=a*5
print(a)
a/=2 #a=a/2
print(a)
a//=2 #a=a//2
print(a)
a**=2 #a=a**2
print(a)
a%=2 #a=a%2
print(a)
Code Snippet
#Relational operators
a=45
b=35
print(a==b) # false
print(a>b) # True
print(a=b) #True
print(a<=b) #false
print(a!=b) #true
#Logical Operators
c=55
print(a>b and a>c) #false
print(a<b and a<c) #false
print(a>b or ab != a>c) #false
Code Snippet
#Identity Operators
a=45
b=35
print(a is b) #False
print (a is not b) #True
#Membership Operators
x=[1,2,3,4,5]
print (3 in x) #True
print(3 not in x) #False
Code Snippet
print("grape">"apple")
print("apple">"APPLE")
#key asci value
print(ord("a"))
print(ord("b"))
print(ord("A"))
print(ord("B"))
Code Snippet
#ternary Operator
age=17
if age >= 18:
message="Eligible"
else:
message="Not Eligible"
print(message)
#or
message="Eligible" if age>=18 else "Not Eligible"
print(message)
print("Eligible" if age>=16 else "Not Eligible")
Code Snippet
#chaining operators
age=34
if age>= 18 and age<65:
print("Eligible")
#or
if 18<= age <65:
print("Eligible")
Code Snippet
# Python program to show
# bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print("a & b =", a & b)
# Print bitwise OR operation
print("a | b =", a | b)
# Print bitwise NOT operation
print("~a =", ~a)
# print bitwise XOR operation
print("a ^ b =", a ^ b)
Conclusion & Next Steps
Python's elegance is shown through these small but powerful programs. Whether you are using Python for data analysis, web development, or automation, understanding these foundational snippets is key. Keep writing Pythonic code and exploring the massive standard library available to you.