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! 🎉
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.
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.
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:
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 in Python are used to store data. Here are some basic data types:
5 or -10.3.14 or 0.0001.') or double quotes ("). For example: "Hello, World!".Here's an example of declaring and using variables:
name = "John Doe"
age = 25
print(f"Hello, {name}! You are {age} years old.")Operators are symbols that perform specific mathematical or comparison operations. Here are some basic operators:
+, -, *, /, ** (exponentiation), and % (modulus).==, !=, <, <=, >, and >=.=.Let's see an example using operators:
a = 5
b = 10
print(f"The sum of a and b is {a + b}.")Control structures in Python help manage the flow of execution in your code. Here are some essential control structures:
Let's write a simple for loop that prints the numbers 1 to 10:
for i in range(1, 11):
print(i)Functions are reusable blocks of code that perform a specific task. Here's an example of a simple function that prints a greeting:
def greet(name):
print(f"Hello, {name}!")
greet("John Doe")Python has a vast ecosystem of libraries that provide additional functionality. To use a library, you need to import it into your script:
import math
# Use the library
print(math.sqrt(16)) # Output: 4.0What is the output of the following code?