- Introduction to Python: This tutorial covers the basics of Python, including data types, variables, loops, and control structures.
Examples:
- Data types:
# Declare variables of different data types
x = 5 # integer
y = "Hello" # string
z = 3.14 # float
is_active = True # boolean
# Print the data type of a variable
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'str'>
print(type(z)) # Output: <class 'float'>
print(type(is_active)) # Output: <class 'bool'>
- Variables:
# Declare variables and assign values
x = 5
y = 3
# Perform basic math operations with variables
print(x + y) # Output: 8
print(x - y) # Output: 2
print(x * y) # Output: 15
print(x / y) # Output: 1.6666666666666667
- Python Data Types: In this tutorial, you will learn about the different data types in Python, including integers, floats, strings, and booleans.
Examples:
- Strings:
# Declare a string
string = "Hello World"
# Get the length of a string
print(len(string)) # Output: 11
# Access a character in a string using its index
print(string[0]) # Output: 'H'
# Concatenate strings using the + operator
greeting = "Hello"
name = "John"
message = greeting + " " + name + "!"
print(message) # Output: "Hello John!"
# Format strings using the format method
age = 30
formatted_string = "I am {} years old.".format(age)
print(formatted_string) # Output: "I am 30 years old."
- Python Variables: This tutorial covers how to define and use variables in Python, as well as how to perform basic math operations with them.
Examples:
- Variables:
# Declare variables and assign values
x = 5
y = 3
# Perform basic math operations with variables
print(x + y) # Output: 8
print(x - y) # Output: 2
print(x * y) # Output: 15
print(x / y) # Output: 1.6666666666666667
- Python Loops: This tutorial covers the different looping constructs in Python, including for loops and while loops.
Examples:
- For loops:
# Loop through a list and print each element
colors = ["red", "green", "blue"]
for color in colors:
print(color)
# Output:
# red
# green
# blue
# Loop through a range of numbers and print each one
for i in range(5):
print(i)
# Output:
# 0
# 1
# 2
# 3
# 4
- While loops:
# Loop while a condition is true
i = 0
while i < 5:
print(i)
i += 1
# Output:
# 0
# 1
# 2
# 3
# 4
# Loop until a condition is true
i = 0
while True:
print(i)
i += 1
if i == 5:
break
# Output:
# 0
# 1
# 2
# 3
# 4
- Loop control statements:
# Skip an iteration with continue
for i in range(5):
if i == 3:
continue
print(i)
# Output:
# 0
# 1
# 2
# 4
# Exit a loop with break
for i in range(5):
if i == 3:
break
print(i)
# Output:
# 0
# 1
# 2
5. Python Functions:
- Here’s an example of a simple function that takes two numbers as arguments and returns their sum:
def add(a, b):
return a + b
result = add(3, 4)
print(result) # Output: 7
- You can also define functions that don’t take any arguments or return a value. For example:
def greet():
print("Hello, world!")
greet() # Output: "Hello, world!"
- You can also define default values for function arguments. These default values are used when the function is called without the corresponding argument being passed. For example:
def greet(name="John"):
print(f"Hello, {name}!")
greet() # Output: "Hello, John!"
greet("Jane") # Output: "Hello, Jane!"
- Functions can also accept a variable number of arguments. To do this, you can use the
*
operator followed by the name of the argument. This will create a tuple of the arguments passed to the function. For example:
def sum_all(*args):
total = 0
for number in args:
total += number
return total
result = sum_all(1, 2, 3, 4, 5)
print(result) # Output: 15
- You can also use the
**
operator to accept a variable number of keyword arguments, which are stored in a dictionary. For example:
def greet(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
greet(name="John", age=30) # Output: "name: John", "age: 30"
6. Python modules:
- To use a module in your code, you first need to import it. You can do this using the
import
keyword, followed by the name of the module. For example:
import math
x = math.cos(math.pi)
print(x) # Output: -1.0
- You can also import specific definitions from a module using the
from
keyword. For example:
from math import pi, cos
x = cos(pi)
print(x) # Output: -1.0
- You can also give a module an alias using the
as
keyword. This can be useful if you have multiple modules with similar names or if you want to use a shorter name for a module. For example:
import math as m
x = m.cos(m.pi)
print(x) # Output: -1.0
- You can also use the
*
operator to import all definitions from a module. However, this is generally not recommended, as it can make it harder to determine where a specific definition came from.
from math import *
x = cos(pi)
print(x) # Output: -1.0
Comment on “Python Basics”