Introduction to Python
Python is a versatile and beginner-friendly programming language known for its simplicity and readability. It's widely used in web development, data science, automation, artificial intelligence, and more. In this tutorial, we’ll dive into Python basics to kickstart your journey.
Step 1: Installing Python
- Download Python from the official website.
- Install an IDE like VS Code, PyCharm, or simply use IDLE (included with Python).
- Verify installation by running
python --version
in your terminal.
Step 2: Writing Your First Python Program
- Open your IDE or terminal.
- Create a new file named
hello.py
. - Add the following code:
print("Arjun, Kandekar!")
- Run the file with the command:
python hello.py
Output: Arjun, Kandekar!
Step 3: Understanding Basic Syntax
- Variables and Data Types: Assign values to variables:
name = "Arjun" # String
age = 23 # Integer
is_student = True # Boolean
print(name, age, is_student)
-
Comments:
Use
#
for single-line comments and'''
or"""
for multi-line comments:
# This is a single-line comment
'''
This is a
multi-line comment
'''
Step 4: Simple Arithmetic Operations
Python can handle basic math operations:
a = 10
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
Practice Exercise
- Write a program to display your name, age, and favorite hobby using
print()
. - Perform basic math operations on two numbers and print the results.
Python Control Structures
Conditional Statements
Python uses if
, elif
, and else
statements to execute code based on conditions.
Example:
temperature = 30
if temperature > 35:
print("It's too hot outside!")
elif 20 <= temperature <= 35:
print("The weather is pleasant.")
else:
print("It's quite cold outside!")
Explanation:
-
if
: Executes the block if the condition is true. -
elif
: Provides additional checks if the previous conditions are false. -
else
: Executes when none of the conditions match.
Loops in Python
Loops let you repeat code efficiently. Python offers two main types:
-
for
Loop: Used to iterate over a sequence or range of numbers.
for i in range(1, 6):
print(f"Step {i}")
-
while
Loop: Executes as long as the condition remains true.
count = 1
while count <= 5:
print(f"Count: {count}")
count += 1
Breaking Out of Loops
-
break
: Exits the loop immediately. -
continue
: Skips the current iteration and moves to the next one.
Example:
for i in range(1, 6):
if i == 3:
break # Stops the loop when i is 3
print(i)
for i in range(1, 6):
if i == 3:
continue # Skips printing 3
print(i)
Practice Exercises
- Guessing Game: Write a program to guess a random number between 1 and 10.
import random
number = random.randint(1, 10)
while True:
guess = int(input("Guess the number (1-10): "))
if guess == number:
print("Correct!")
break
elif guess < number:
print("Too low!")
else:
print("Too high!")
- Summing Numbers: Sum numbers from 1 to a given input.
n = int(input("Enter a number: "))
total = sum(range(1, n + 1))
print(f"The sum of numbers from 1 to {n} is {total}.")
Conclusion
Today, you learned:
- The basics of Python setup and syntax.
- Writing your first program and performing arithmetic operations.
- Using control structures (
if
,for
,while
) to make decisions and repeat tasks.
Practice the exercises and experiment with different scenarios. Tomorrow, we’ll dive into functions and modules to make your code more organized and reusable!
Top comments (0)