-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial 11
Letโs test that your Python installation was successful โ and walk through some basic exercises to get comfortable with Python syntax.
Activate your Python environment created earlier (adjust the name as needed):
mamba activate defaultAlternatively, you can use a Python IDE, like PyCharm ๐ง โ a free educational license is available via Tel Aviv University here.
Hereโs your first program:
# This program prints "Hello, World!"
print("Hello, World!")๐งพ This is how the print() function displays output.
# Get two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Add the two numbers
result = num1 + num2
# Display the result
print("The sum is:", result)๐ input() captures user input, and float() converts it into a number.
# Ask the user for a number
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")๐ง Learn about % (modulo operator) and if/else for decision-making.
Functions are reusable blocks of code that take inputs and return outputs. They help keep your code clean and modular. ๐งผ
Defined using def, and optionally use return to output a result.
# Define the function
def square(number):
"""Returns the square of a given number."""
return number ** 2
# Call the function
result = square(5)
print("The square of 5 is:", result)โ Functions help you avoid repetition.
A class is a blueprint for creating objects. ๐งฑ It bundles together data (attributes) and behaviors (methods).
- Defined with
class - Constructor:
__init__ - Methods: functions that operate on the object
class Car:
"""A simple class to represent a car."""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def describe(self):
return f"{self.year} {self.make} {self.model}"
def start(self):
return f"The {self.make} {self.model} is now starting!"
# Create an instance
my_car = Car("Toyota", "Corolla", 2020)
# Use the methods
print(my_car.describe())
print(my_car.start())๐ Use classes to model real-world entities and build more scalable code.
Modules allow you to reuse code across projects. Python supports:
- ๐ง Built-in modules (
math,os,datetime) - ๐ External modules (
numpy,pymatgen) - ๐ User-defined modules (your own
.pyfiles)
import math
number = 16
square_root = math.sqrt(number)
print(f"The square root of {number} is: {square_root}")
pi_value = math.pi
print(f"The value of pi is: {pi_value}")Use this format to import from another file:
from path.to.file import functionIf the files are in the same directory, simplify to:
from filename import functionModular code = maintainable code ๐ก
You now understand the basics of:
- โ Running Python
- ๐งฎ Variables & operations
- ๐ Loops & conditionals
- ๐งฉ Functions
- ๐ Classes
- ๐ฆ Modules
You're ready to move on to
๐ Tutorial 12 for the next step in your programming journey! ๐