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

Python Basics Detailed Code

The document provides an overview of Python basics, including how to write a simple program, use variables, and implement comments. It covers data types, type conversion, input examples, and various operators. Additionally, it includes practice exercises for summing numbers, calculating the area of a square, finding the average, and making comparisons.

Uploaded by

Hanu Pandey
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)
3 views4 pages

Python Basics Detailed Code

The document provides an overview of Python basics, including how to write a simple program, use variables, and implement comments. It covers data types, type conversion, input examples, and various operators. Additionally, it includes practice exercises for summing numbers, calculating the area of a square, finding the average, and making comparisons.

Uploaded by

Hanu Pandey
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
You are on page 1/ 4

Python Basics with Code Examples

1. First Python Program


print("Hello, World!")
print("Welcome to Python Programming")

2. Variables
name = "Shradha"
age = 23
price = 25.99

print("Name:", name)
print("Age:", age)
print("Price:", price)

3. Comments
# Single line comment explains code line
# This program prints a message

"""
This is a multi-line comment
Useful for documentation
Explains sections of code
"""

print("Comments demonstrated.")

4. Data Types
# Integer
a = 10
print("Integer:", a)

# Float
b = 3.14
print("Float:", b)

# String
c = "Python"
print("String:", c)

# Boolean
Python Basics with Code Examples

d = True
print("Boolean:", d)

# None
e = None
print("NoneType:", e)

5. Type Conversion
a = 5
b = 3.2
result = a + b
print("Sum (implicit type conversion):", result)

# Error example
# a = 5
# b = "10"
# result = a + b # TypeError

6. Type Casting
a = 5
b = "10"
b_int = int(b)
result = a + b_int
print("Sum (after type casting):", result)

# String to float
s = "3.14"
f = float(s)
print("Converted float:", f)

7. Input Examples
num1 = int(input("Enter an integer: "))
num2 = float(input("Enter a float number: "))
name = input("Enter your name: ")

print("Sum:", num1 + num2)


print("Hello,", name)

8. Operators
Python Basics with Code Examples

# Arithmetic Operators
a = 15
b = 4
print("Add:", a + b)
print("Sub:", a - b)
print("Mul:", a * b)
print("Div:", a / b)
print("Mod:", a % b)
print("Power:", a ** b)

# Relational Operators
print("Equal:", a == b)
print("Not Equal:", a != b)
print("Greater:", a > b)
print("Less:", a < b)

# Assignment Operators
x = 10
x += 5
print("x += 5:", x)

# Logical Operators
p = True
q = False
print("not p:", not p)
print("p and q:", p and q)
print("p or q:", p or q)

Practice Q1: Sum of Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)

Practice Q2: Area of Square


side = float(input("Enter the side of square: "))
area = side ** 2
print("Area of square =", area)
Python Basics with Code Examples

Practice Q3: Average of Two Numbers


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
avg = (num1 + num2) / 2
print("Average =", avg)

Practice Q4: Comparison


a = int(input("Enter a: "))
b = int(input("Enter b: "))
if a >= b:
print("True")
else:
print("False")

You might also like