Skip to content

Using Python

roeeasher edited this page Jun 18, 2026 · 2 revisions

🐍 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 Environment

Activate your Python environment created earlier (adjust the name as needed):

mamba activate default

Alternatively, you can use a Python IDE, like PyCharm 🧠 — a free educational license is available via Tel Aviv University here.


👋 Hello, Python!

Here’s your first program:

# This program prints "Hello, World!"print("Hello, World!")

🧾 This is how the print() function displays output.


➕ User Input and Addition

# 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.


🔁 Even or Odd?

# 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 in Python

Functions are reusable blocks of code that take inputs and return outputs. They help keep your code clean and modular. 🧼

🧱 Function Structure

Defined using def, and optionally use return to output a result.

Example:

# 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.


🚗 Python Classes

A class is a blueprint for creating objects. 🧱 It bundles together data (attributes) and behaviors (methods).

🏗️ Class Structure

  • Defined with class
  • Constructor: __init__
  • Methods: functions that operate on the object

Example: A Car 🚘

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.


📦 Imports & Modules

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 .py files)

Example: Import a Built-in Module

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}")

🧠 How to Import Your Own Code

Use this format to import from another file:

frompath.to.fileimportfunction

If the files are in the same directory, simplify to:

fromfilenameimportfunction

Modular code = maintainable code 💡


🎉 Congratulations!

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! 🚀

Clone this wiki locally