Skip to content

Tutorial 11

Lee Burton edited this page Jul 15, 2025 · 27 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 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.


๐Ÿ” Even or Odd?

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


๐Ÿš— 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 ๐Ÿš˜

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.


๐Ÿ“ฆ 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

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

๐Ÿง  How to Import Your Own Code

Use this format to import from another file:

from path.to.file import function

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

from filename import function

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 12 for the next step in your programming journey! ๐Ÿš€

Clone this wiki locally