Introduction to Python
Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with its notable use of significant indentation. It is dynamically typed and garbage-collected. Python's versatility makes it suitable for web development, data science, artificial intelligence, automation, and much more.
This tutorial will cover the fundamental concepts you need to start programming in Python.
Variables and Data Types
Variables are used to store data values. Python has several built-in data types:
Common Data Types:
int
: Whole numbers (e.g.,10
,-5
).float
: Decimal numbers (e.g.,3.14
,-0.5
).str
: Text (e.g.,"Hello"
,'Python'
).bool
: Boolean values (True
orFalse
).list
: Ordered, mutable collections (e.g.,[1, 2, 3]
).tuple
: Ordered, immutable collections (e.g.,(1, 2, 3)
).dict
: Unordered collections of key-value pairs (e.g.,{'name': 'Alice', 'age': 30}
).
Example:
# Assigning values to variables
name = "Alice"
age = 30
height = 5.5
is_student = False
# Printing variables
print(name)
print(age)
Operators
Operators are used to perform operations on variables and values.
Types of Operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
(modulo),**
(exponentiation),//
(floor division). - Comparison Operators:
==
,!=
,>
,<
,>=
,<=
. - Logical Operators:
and
,or
,not
. - Assignment Operators:
=
,+=
,-=
, etc.
Example:
x = 10
y = 5
# Arithmetic
sum_result = x + y # 15
# Comparison
is_equal = x == 10 # True
# Logical
is_valid = is_equal and y > 0 # True
Control Flow: Conditional Statements
Conditional statements allow you to execute different blocks of code based on whether a condition is true or false.
if
: Executes a block of code if a condition is true.elif
: (else if) Checks another condition if the previousif
orelif
was false.else
: Executes a block of code if all preceding conditions are false.
Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or lower")
Control Flow: Loops
Loops are used to execute a block of code repeatedly.
for
loop: Iterates over a sequence (like a list, tuple, string, or range).while
loop: Executes a block of code as long as a condition is true.
Example (for loop):
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Example (while loop):
count = 0
while count < 5:
print(count)
count += 1
Functions
Functions are blocks of reusable code that perform a specific task. They help in organizing code and avoiding repetition.
Defining and Calling a Function:
def greet(name):
"This function greets the person passed in as a parameter."
print("Hello, " + name + "!")
# Calling the function
greet("World")
Next Steps
Congratulations on completing the Python basics tutorial! You've learned about variables, data types, operators, control flow, and functions.
To continue your learning:
- Practice writing small Python programs.
- Explore more advanced topics like Object-Oriented Programming (OOP), file handling, and modules.
- Check out the official Python documentation: python.org.
- Try building a simple project, like a calculator or a to-do list application.