Python Basic Programs & Code Snippets

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.

1. Comments and Inputs

Concept: Introduces single and multi-line comments in Python, along with how to read user input using the built-in input() function.

program_1.py


#Python Single Line comments
""""
Python Multiline Comments
"""
'''
Python Multiline Comments
'''
#Python Input
name=input("Enter name: ") # input function
#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))
Analysis
  • Time Complexity: O(1) - Constant time.
  • Space Complexity: O(1) - Constant space.
Expected Output:
Enter name: John Enter Age: 25 25

2. Read Multiple Strings

Concept: Demonstrates taking multiple inputs from a user sequentially and printing them out.

program_2.py


#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)
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Enter Code: 101 Enter Name: Alice Enter Age: 22 ...

3. Variable Assignment

Concept: Shows how Python automatically infers the data type based on the assigned value (dynamic typing).

program_3.py


#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}))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Code: 100 Name: SrinivasaRao

4. Dynamic Input Casting

Concept: Reads strings from the user and explicitly casts them to integers (int) and floating point numbers (float).

program_4.py


#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))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Enter Code: 102 Enter Name: Bob Enter Age: 30 ...

5. Simple Print Statement

Concept: The classic first program, demonstrating how to use the print() function to output text to the console.

program_5.py


#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))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Welcome to Python First Program

6. Multiple Variable Assignments

Concept: Assigns values to variables of various primitive data types and uses print to display them.

program_6.py


# 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))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
100 SrinivasaRao.K Ongole 8500.0

7. Billing Example

Concept: Reads product details, converts numeric inputs to int/float, calculates the total amount, and prints a formatted bill.

program_7.py


#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))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Enter Froot Name: Apple Enter Qty: 5 Enter Rate: 2.5 Amount: 12.5

8. Arithmetic Operators

Concept: Demonstrates standard arithmetic operations in Python including addition, subtraction, multiplication, and power/exponentiation.

program_8.py


#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**
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Enter a value: 45 Enter b value: 2 Addition : 47 Sub : 43 Mul : 90 Power : 2025

9. Assignment Operators

Concept: Illustrates compound assignment operators (+=, -=, *=, /=) to concisely update a variable's value.

program_9.py


#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)
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
10 5 25 5.0

10. Relational Operators

Concept: Shows how to compare two values, yielding boolean results (True or False) using equality and inequality checks.

program_10.py


#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
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
False True False True False True

11. Identity and Membership Operators

Concept: Uses 'is' and 'is not' to compare memory locations, and 'in' and 'not in' to check for element presence inside collections or strings.

program_11.py


#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
Analysis
  • Time Complexity: O(N) for membership tests on lists.
  • Space Complexity: O(1)
Expected Output:
False True True False

12. String Comparison & ASCII

Concept: Compares strings lexicographically and uses ord() and chr() to convert between characters and their ASCII code representations.

program_12.py


print("grape">"apple")
print("apple">"APPLE")

#key asci value
print(ord("a"))
print(ord("b"))
print(ord("A"))
print(ord("B"))
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
True True 97 98 a

13. If-Else Statements

Concept: Evaluates a condition using an if-else block to control program flow based on boolean logic.

program_13.py


#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")
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Not Eligible

14. Chained Comparison Operators

Concept: Demonstrates Python's unique ability to chain relational operators directly, e.g., 18 <= age < 65.

program_14.py


#chaining operators
age=34
if age>= 18  and age<65:
    print("Eligible")

#or

if 18<= age <65:
    print("Eligible")
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
Eligible Eligible

15. Bitwise Operators

Concept: Performs bit-level operations (AND, OR, NOT, XOR, Shifts) on integer operands.

program_15.py


# 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)
Analysis
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Expected Output:
a & b = 0 a | b = 14 ~a = -11 a ^ b = 14 a >> 1 = 5 a << 1 = 20

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.