Python Basics

Learn some basic Python mechanics that will help you later on!

Getting Started with Python

Python is a high-level programming language that emphasizes code readability. Let's start with some basic concepts:

1. Variables and Data Types

Variables are containers for storing data values. Python has several basic data types:

# Numbers
age = 25          # integer
price = 19.99     # float

# Strings
name = "John"
message = 'Hello, World!'

# Boolean
is_student = True
is_working = False

# Lists
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]

2. Basic Operations

Python supports various mathematical and string operations:

# Mathematical operations
sum = 10 + 5      # Addition
diff = 10 - 5     # Subtraction
product = 10 * 5  # Multiplication
quotient = 10 / 5 # Division

# String operations
greeting = "Hello"
name = "World"
message = greeting + " " + name  # String concatenation
repeated = "Ha" * 3              # String repetition

3. Print Statements

The print() function is used to output text to the console:

print("Hello, World!")
name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.")
print(f"My name is {name} and I am {age} years old.")  # f-string

4. Input from Users

The input() function allows you to get input from users:

name = input("What is your name? ")
age = int(input("How old are you? "))  # Convert string to integer
print(f"Hello {name}, you are {age} years old!")

5. Comments

Comments are used to explain code and are ignored by Python:

# This is a single-line comment

'''
This is a
multi-line comment
'''

# Variables
x = 5  # This is an integer
y = "Hello"  # This is a string

Conclusion

Python is a high-level programming language known for its readability. It uses variables to store data, supports numbers, strings, boolean values, and lists. Basic operations like math and string manipulation are possible. The print() function outputs text and input() function allows user input. Comments can be used for explanations