Home > Backend Development > Python Tutorial > Day Getting Started with Python

Day Getting Started with Python

DDD
Release: 2024-12-04 07:51:15
Original
1026 people have browsed it

Day 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!")
Copy after login
Copy after login
  1. Run the file with the command:
   python hello.py
Copy after login
Copy after login

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)
Copy after login
Copy after login
  • Comments: Use # for single-line comments and ''' or """ for multi-line comments:
  # This is a single-line comment
  '''
  This is a
  multi-line comment
  '''
Copy after login

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)
Copy after login

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!")
Copy after login

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}")
Copy after login
  1. while Loop: Executes as long as the condition remains true.
count = 1
while count <= 5:
    print(f"Count: {count}")
    count += 1
Copy after login

Breaking Out of Loops

  1. break: Exits the loop immediately.
  2. continue: Skips the current iteration and moves to the next one.

Example:

   print("Arjun, Kandekar!")
Copy after login
Copy after login

Practice Exercises

  1. Guessing Game: Write a program to guess a random number between 1 and 10.
   python hello.py
Copy after login
Copy after login
  1. Summing Numbers: Sum numbers from 1 to a given input.
  name = "Arjun"  # String
  age = 23        # Integer
  is_student = True  # Boolean
  print(name, age, is_student)
Copy after login
Copy after login

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!

The above is the detailed content of Day Getting Started with Python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template