Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Using Python
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 usernum1=float(input("Enter the first number: "))
num2=float(input("Enter the second number: "))
# Add the two numbersresult=num1+num2# Display the resultprint("The sum is:", result)🎓 input() captures user input, and float() converts it into a number.
# Ask the user for a numbernumber=int(input("Enter a number: "))
# Check if the number is even or oddifnumber%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 functiondefsquare(number):
"""Returns the square of a given number."""returnnumber**2# Call the functionresult=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
classCar:
"""A simple class to represent a car."""def__init__(self, make, model, year):
self.make=makeself.model=modelself.year=yeardefdescribe(self):
returnf"{self.year}{self.make}{self.model}"defstart(self):
returnf"The {self.make}{self.model} is now starting!"# Create an instancemy_car=Car("Toyota", "Corolla", 2020)
# Use the methodsprint(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)
importmathnumber=16square_root=math.sqrt(number)
print(f"The square root of {number} is: {square_root}")
pi_value=math.piprint(f"The value of pi is: {pi_value}")Use this format to import from another file:
frompath.to.fileimportfunctionIf the files are in the same directory, simplify to:
fromfilenameimportfunctionModular 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 11 for the next step in your programming journey! 🚀