Skip to main content

Python

Python is a popular and versatile programming language that's great for beginners.

Here are the fundamentals.

Installation

First, you need to have Python installed on your computer. You can download Python from the official website: https://www.python.org/downloads/. Choose the latest version (Python 3.x).

A Simple Program

Let's start with a simple "Hello, World!" program. Open a text editor (like Notepad on Windows or TextEdit on Mac) and type the following:

print("Hello, World!")

Save this file with a .py extension, for example, hello.py.

Running Python Code

Open your command-line terminal (Command Prompt on Windows, Terminal on Mac/Linux), navigate to the directory where your hello.py file is saved, and run the program with the following command:

python hello.py

You should see the output "Hello, World!".

Variables and Data Types

Python supports various data types like integers, floats, strings, lists, and more. Here's an example of declaring variables:

# Integer
age = 25

# Float
temperature = 98.6

# String
name = "John Doe"

Basic Operations

Python supports standard arithmetic operations:

a = 10
b = 5

sum_result = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b

Control Flow

Python uses indentation for code blocks. Here's an example of an if statement:

x = 15

if x > 10:
print("x is greater than 10")
else:
print("x is not greater than 10")

Loops

Python has for and while loops. Here's an example of a for loop:

for i in range(5):
print(i)

Functions

You can define your own functions in Python:

def greet(name):
print("Hello, " + name + "!")

greet("Alice")

This is just the beginning of your journey into Python programming. There's so much more to learn, including data structures, libraries, and more advanced topics.

As you progress, you might want to explore topics like lists, dictionaries, functions, modules, and libraries like NumPy, pandas, and more.


Resources