0% found this document useful (0 votes)
2 views6 pages

Python Basics

This document provides an introduction to Python programming, covering fundamental concepts such as indentation, basic functions (print() and input()), variables, arithmetic operations, for loops, and conditional statements (if, else). It emphasizes the importance of indentation for code structure and includes examples for clarity. The document serves as a beginner's guide to understanding and using Python effectively.

Uploaded by

Leon Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Basics

This document provides an introduction to Python programming, covering fundamental concepts such as indentation, basic functions (print() and input()), variables, arithmetic operations, for loops, and conditional statements (if, else). It emphasizes the importance of indentation for code structure and includes examples for clarity. The document serves as a beginner's guide to understanding and using Python effectively.

Uploaded by

Leon Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Basics

Python is a powerful and easy-to-learn language. Let's get started with some
fundamental concepts.

1. Indentation
Indentation refers to the spaces at the beginning of a code line. In many programming
languages, indentation is used for readability, but in Python, it's very important.
Python uses indentation to indicate a block of code.
●​ You should use 4 spaces for indentation.
●​ You'll see this when we use if statements and for loops.

Example:

if 5 > 2:​
print("Five is greater than two!") # This line is indented

If you don't indent correctly, Python will give you an error!

2. print() and input()


These are two of the most basic and useful functions in Python.

print()
The print() function is used to display output to the screen. You can print text
(strings), numbers, or the values of variables.
●​ Text (strings) needs to be enclosed in single quotes (') or double quotes (").

Examples:

print("Hello, World!")​
print('Python is fun!')​
print(123)​
print(4 + 5)
input()
The input() function allows you to get input from the user. It pauses your program and
waits for the user to type something and press Enter.
●​ The input() function always returns the user's input as a string. If you need it to be
a number, you'll have to convert it.
Example:

name = input("What is your name? ")​


print("Hello, " + name + "!")​

age_string = input("How old are you? ")​
# If you want to use age as a number, you need to convert it:​
# age_number = int(age_string)​
# print("Next year, you will be", age_number + 1)

3. Variables
A variable is like a container that stores a value. You can give a variable a name and
then use that name to refer to the value it holds.
●​ Naming Rules:
○​ Variable names can contain letters (a-z, A-Z), numbers (0-9), and the
underscore character (_).
○​ They cannot start with a number.
○​ Variable names are case-sensitive (myVariable is different from myvariable).
○​ Choose descriptive names for your variables (e.g., user_age instead of x).
Assigning Values:
You use the equals sign (=) to assign a value to a variable.
Examples:

message = "Welcome to Python!" # A string variable​


count = 10 # An integer variable​
price = 19.99 # A floating-point (decimal) variable​

print(message)​
print("Count:", count)​
print("Price:", price)​

# You can change the value of a variable​
count = count + 5​
print("New count:", count)

4. Simple Arithmetic Calculations


Python can perform all basic arithmetic operations.
●​ Addition: +
●​ Subtraction: -
●​ Multiplication: *
●​ Division: / (results in a float, e.g., 10 / 4 = 2.5)
●​ Floor Division: // (discards the decimal part, e.g., 10 // 4 = 2)
●​ Modulus (Remainder): % (gives the remainder of a division, e.g., 10 % 3 = 1)
●​ Exponentiation (Power): ** (e.g., 2 ** 3 = 8, which is 2*2*2)
Examples:

num1 = 15​
num2 = 4​

sum_result = num1 + num2​
difference = num1 - num2​
product = num1 * num2​
quotient = num1 / num2​
floor_quotient = num1 // num2​
remainder = num1 % num2​
power = num2 ** 3 # 4 to the power of 3​

print("Sum:", sum_result)​
print("Difference:", difference)​
print("Product:", product)​
print("Quotient:", quotient)​
print("Floor Quotient:", floor_quotient)​
print("Remainder:", remainder)​
print("Power (num2^3):", power)

You can also use parentheses () to control the order of operations, just like in math.

5. for i in range(num1, num2)


A for loop is used for iterating over a sequence (like a list of numbers). The range()
function is very helpful for creating a sequence of numbers.
●​ range(stop): Generates numbers from 0 up to (but not including) stop.
○​ range(5) will give 0, 1, 2, 3, 4
●​ range(start, stop): Generates numbers from start up to (but not including) stop.
○​ range(2, 6) will give 2, 3, 4, 5
●​ range(start, stop, step): Generates numbers from start up to (but not including)
stop, with an increment of step.
○​ range(1, 10, 2) will give 1, 3, 5, 7, 9

The i in for i in range(...) is a variable that takes on the value of each number in the
sequence, one by one. You can name this variable anything you like (e.g., number,
count), but i is a common convention.

Remember the indentation for the code inside the loop!

Examples:

print("Counting from 0 to 4:")​


for i in range(5): # Equivalent to range(0, 5)​
print(i)​

print("\nCounting from 3 to 7:")​
for number in range(3, 8): # Goes up to 7 (8-1)​
print(number)​

print("\nCounting even numbers from 2 to 10:")​
for x in range(2, 11, 2): # Starts at 2, up to 10, step by 2​
print(x)

6. if, else, and Comparison Symbols


if statements allow your program to make decisions. The code inside an if statement
only runs if a certain condition is true.

You can also add an else block, which will run if the if condition is false.

Remember the indentation for the code inside if and else blocks!

Comparison Symbols
These are used to create conditions:
●​ == : Equal to (e.g., a == b)
●​ != : Not equal to (e.g., a != b)
●​ > : Greater than (e.g., a > b)
●​ < : Less than (e.g., a < b)
●​ >= : Greater than or equal to (e.g., a >= b)
●​ <= : Less than or equal to (e.g., a <= b)
Examples:

age = 15​

if age >= 18:​
print("You are an adult.")​
else:​
print("You are a minor.")​

temperature = 25​

if temperature > 30:​
print("It's hot outside!")​
elif temperature > 20: # 'elif' means 'else if' - checks another condition​
print("It's a pleasant day.")​
else:​
print("It might be cold.")​

# Example with numbers​
num_a = 10​
num_b = 20​

if num_a == num_b:​
print(f"{num_a} is equal to {num_b}")​
elif num_a < num_b:​
print(f"{num_a} is less than {num_b}")​
else:​
print(f"{num_a} is greater than {num_b}")​

password_attempt = "12345"​
correct_password = "password123"​

if password_attempt == correct_password:​
print("Access granted!")​
else:​
print("Access denied!")

You might also like