L'objectiu d'aquest material és configurar l'entorn de desenvolupament
en ordinadors amb sistema operatiu Windows o distribucions de Linux basades en Debian,
com Ubuntu o Linux Mint.
En aquest manual s'instal·larà i es configuraran les següent eines:
En Python, el codi està estructurat en mòduls, que són directoris amb un fitxer __init__.py.
Podeu crear un mòdul amb Botó dret > New > Python Package.
#!/usr/bin/env python# Python Basics Example Script# 1. Variables and Data Types# ----------------------------# Integerage=25# Floatheight=5.9# Stringname="Alice"# Booleanis_student=Trueprint(f"My name is {name}, I am {age} years old, {height} feet tall, and it is {is_student} that I am a student.")# 2. Conditional Statements (if-elif-else)# ----------------------------------------ifage<18:print("You are a minor.")elifage<65:print("You are an adult.")else:print("You are a senior.")# 3. Loops (for, while)# ----------------------# for loop exampleprint("\nFor loop example:")foriinrange(5):# Will loop from 0 to 4print(f"Iteration {i}")# while loop exampleprint("\nWhile loop example:")count=0whilecount<3:print(f"Count is {count}")count+=1# 4. Functions# -------------defgreet(name,is_morning=True):"""Function to greet a person based on the time of day."""ifis_morning:returnf"Good morning, {name}!"else:returnf"Hello, {name}!"print("\nFunction example:")print(greet("Alice"))print(greet("Bob",False))# 5. Lists and Dictionaries# ---------------------------# List of numbersnumbers=[10,20,30,40,50]# Dictionary representing a personperson={"name":"John","age":30,"is_student":False}# Accessing list and dictionary elementsprint("\nList and Dictionary example:")print(f"The first number in the list is {numbers[0]}")print(f"The person's name is {person['name']} and they are {person['age']} years old.")# 6. Classes and Objects# -----------------------classDog:"""A simple class representing a dog."""def__init__(self,name,breed):self.name=nameself.breed=breeddefbark(self):returnf"{self.name} says Woof!"# Creating an instance (object) of the Dog classmy_dog=Dog("Buddy","Golden Retriever")print("\nClass and Object example:")print(f"My dog's name is {my_dog.name} and he is a {my_dog.breed}.")print(my_dog.bark())
Tracta d'executar el codi amb Run. Comprova la consola
per veure si s'ha executat correctament.