Python Tutorial: From Import 🎯

beginner
19 min

Python Tutorial: From Import 🎯

Welcome to CodeYourCraft's Python Tutorial! In this lesson, we'll dive into the wonderful world of Python, a versatile and easy-to-learn programming language. Let's get started! 🎉

What is Python? 📝

Python is a high-level, interpreted programming language that is widely used for various purposes, including web development, data analysis, machine learning, and AI. It's known for its readability and simplicity, making it a great choice for beginners.

Installing Python 📝

Before we start coding, you'll need to install Python on your computer. You can download it from the official website. Follow the instructions for your operating system to complete the installation.

Understanding Python Syntax 💡

Python code consists of statements, which are usually ended with a colon (:) and indented to show the structure of the code. Let's look at a simple Python statement:

python
print("Hello, World!")

Here, print is a built-in function that displays the text within the parentheses. The indentation indicates that this statement is part of the Python script.

Variables and Data Types 📝

Variables in Python are used to store data. Here are some basic data types:

  • Integers: Whole numbers, e.g., 5 or -10.
  • Floats: Decimal numbers, e.g., 3.14 or 0.0001.
  • Strings: Sequences of characters, enclosed in single quotes (') or double quotes ("). For example: "Hello, World!".

Here's an example of declaring and using variables:

python
name = "John Doe" age = 25 print(f"Hello, {name}! You are {age} years old.")

Operators 📝

Operators are symbols that perform specific mathematical or comparison operations. Here are some basic operators:

  • Arithmetic: +, -, *, /, ** (exponentiation), and % (modulus).
  • Comparison: ==, !=, <, <=, >, and >=.
  • Assignment: =.

Let's see an example using operators:

python
a = 5 b = 10 print(f"The sum of a and b is {a + b}.")

Control Structures 💡

Control structures in Python help manage the flow of execution in your code. Here are some essential control structures:

  • If statements: For conditional execution.
  • For loops: For iterating over a sequence.
  • While loops: For repeating a block of code until a condition is met.

Let's write a simple for loop that prints the numbers 1 to 10:

python
for i in range(1, 11): print(i)

Functions 💡

Functions are reusable blocks of code that perform a specific task. Here's an example of a simple function that prints a greeting:

python
def greet(name): print(f"Hello, {name}!") greet("John Doe")

Importing Modules 📝

Python has a vast ecosystem of libraries that provide additional functionality. To use a library, you need to import it into your script:

python
import math # Use the library print(math.sqrt(16)) # Output: 4.0

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the output of the following code?