Python
Welcome to the chapter on Python for Class 10. In this chapter, you will learn about the basics of Python programming, how to write simple programs, and understand important concepts like variables, data types, operators, and control structures. By the end of this chapter, you will be able to write and run basic Python programs!
Introduction to Python
Python is a popular, easy-to-learn programming language. It is used for web development, data science, automation, and more. Python code is simple and easy to read.
Why Learn Python?
- Simple and easy syntax
- Widely used in the industry
- Great for beginners and professionals
- Large community and lots of resources
Writing Your First Python Program
You can write Python code in an editor or an IDE. Save your file with a .py extension.
Example:
print("Hello, World!")
Basic Concepts
- Variables: Used to store data. Example:
x = 5 - Data Types: Common types are
int(numbers),float(decimal numbers),str(text),bool(True/False). - Operators: Used to perform operations. Example:
+,-,*,/
Input and Output
Use input() to take input from the user and print() to display output.
name = input("Enter your name: ")
print("Hello,", name)
Control Structures
- If Statements: Used for decision making.
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
- Loops: Used to repeat actions.
forandwhileare common loops.
for i in range(5):
print(i)
Functions
Functions are blocks of code that perform a task. Define a function using def.
def greet():
print("Hello from a function!")
greet()
Practice Questions
- Write a Python program to print your name.
- Write a program to add two numbers entered by the user.
- Write a program to check if a number is even or odd.
- Write a program to print numbers from 1 to 10 using a loop.
- Write a function that takes a number and prints its square.
Challenge Yourself
- Write a program to find the largest of three numbers.
- Write a program to print the multiplication table of a number entered by the user.
Did You Know?
- Python was named after the comedy group "Monty Python"!
- Python is used by companies like Google, Instagram, and NASA.
Glossary
- Variable: A name that stores a value.
- Function: A block of code that does a specific job.
- Loop: Repeats a set of instructions.
- Syntax: The rules for writing code in a language.
Answers to Practice Questions
print("Your Name")a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print("Sum is", a + b)num = int(input("Enter a number: ")) if num % 2 == 0: print("Even") else: print("Odd")for i in range(1, 11): print(i)def square(n): print(n * n) square(5)
Practice Python every day to become a great programmer!