0% found this document useful (0 votes)
5 views3 pages

Python Basics Code Examples

The document provides an overview of Python basics, including examples of printing, variables, comments, data types, type conversion, type casting, and operators. It also includes practice questions for users to apply their knowledge. The content is structured to help beginners understand fundamental concepts in Python programming.

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)
5 views3 pages

Python Basics Code Examples

The document provides an overview of Python basics, including examples of printing, variables, comments, data types, type conversion, type casting, and operators. It also includes practice questions for users to apply their knowledge. The content is structured to help beginners understand fundamental concepts in Python programming.

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/ 3

Python Basics with Code Examples

1. First Python Program


print("Hello World")

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

3. Comments
# Single Line Comment

"""
Multi Line
Comment
"""

4. Data Types
# Integer
a = 5

# Float
b = 5.5

# String
c = "Hello"

# Boolean
d = True

# None
e = None

5. Type Conversion
a = 1
b = 2.0
sum1 = a + b
print(sum1)
Python Basics with Code Examples

# Error Example:
# a = 1
# b = "2"
# sum2 = a + b # TypeError

6. Type Casting
a = 1
b = "2"
c = int(b)
sum3 = a + c
print(sum3)

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

8. Operators
# Arithmetic
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a ** b)

# Relational
print(a == b)
print(a != b)
print(a > b)

# Assignment
a += 5
print(a)

# Logical
x = True
Python Basics with Code Examples

y = False
print(not x)
print(x and y)
print(x or y)

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

Practice Q2
side = float(input("Enter side of square: "))
area = side * side
print("Area of square =", area)

Practice Q3
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
avg = (a + b) / 2
print("Average =", avg)

Practice Q4
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print(a >= b)

You might also like