DEV Community

Cover image for Day 1: Getting Started with Python
arjun
arjun

Posted on

Day 1: Getting Started with Python

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

  1. Download Python from the official website.
  2. Install an IDE like VS Code, PyCharm, or simply use IDLE (included with Python).
  3. Verify installation by running python --version in your terminal.

Step 2: Writing Your First Python Program

  1. Open your IDE or terminal.
  2. Create a new file named hello.py.
  3. Add the following code:
   print("Arjun, Kandekar!")
Enter fullscreen mode Exit fullscreen mode
  1. Run the file with the command:
   python hello.py
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
  • Comments: Use # for single-line comments and ''' or """ for multi-line comments:
  # This is a single-line comment
  '''
  This is a
  multi-line comment
  '''
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Practice Exercise

  1. Write a program to display your name, age, and favorite hobby using print().
  2. 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!")
Enter fullscreen mode Exit fullscreen mode

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:

  1. for Loop: Used to iterate over a sequence or range of numbers.
for i in range(1, 6):
    print(f"Step {i}")
Enter fullscreen mode Exit fullscreen mode
  1. while Loop: Executes as long as the condition remains true.
count = 1
while count <= 5:
    print(f"Count: {count}")
    count += 1
Enter fullscreen mode Exit fullscreen mode

Breaking Out of Loops

  1. break: Exits the loop immediately.
  2. 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)
Enter fullscreen mode Exit fullscreen mode

Practice Exercises

  1. 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!")
Enter fullscreen mode Exit fullscreen mode
  1. 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}.")
Enter fullscreen mode Exit fullscreen mode

Conclusion

Today, you learned:

  1. The basics of Python setup and syntax.
  2. Writing your first program and performing arithmetic operations.
  3. 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)