Koncepcje programowania/Ściąga

Z Wikibooks, biblioteki wolnych podręczników.

Python[edytuj]

# Comments start with a hash symbol

# VARIABLES
x = 5  # Integer
y = 3.14  # Floating-point number
z = "Hello, world!"  # String
is_true = True  # Boolean

# PRINTING
print("Hello, world!")  # Print a string
print(x)  # Print a variable
print(f"The value of x is {x}")  # Print a formatted string

# INPUT
name = input("What is your name? ")  # Get input from the user

# CONDITIONALS
if x > y:
    print("x is greater than y")
elif x < y:
    print("x is less than y")
else:
    print("x is equal to y")

# LOOPS
for i in range(5):  # Loop 5 times
    print(i)

while x > 0:  # Loop until x is 0
    print(x)
    x -= 1

# LISTS
my_list = [1, 2, 3]  # Create a list
my_list.append(4)  # Add an element to the end of the list
print(my_list[0])  # Access an element by index
for item in my_list:  # Loop over the list
    print(item)

# OBJECTS
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def say_hello(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

p = Person("John", 30)
p.say_hello()  # Call a method on the object
print(p.name)  # Access an attribute of the object

JavaScipt[edytuj]

// Comments start with double slashes

// VARIABLES
let x = 5;  // Integer
let y = 3.14;  // Floating-point number
let z = "Hello, world!";  // String
let isTrue = true;  // Boolean

// PRINTING
console.log("Hello, world!");  // Print a string
console.log(x);  // Print a variable
console.log(`The value of x is ${x}`);  // Print a formatted string

// INPUT
let name = prompt("What is your name?");  // Get input from the user

// CONDITIONALS
if (x > y) {
    console.log("x is greater than y");
} else if (x < y) {
    console.log("x is less than y");
} else {
    console.log("x is equal to y");
}

// LOOPS
for (let i = 0; i < 5; i++) {  // Loop 5 times
    console.log(i);
}

while (x > 0) {  // Loop until x is 0
    console.log(x);
    x--;
}

// ARRAYS
let myArray = [1, 2, 3];  // Create an array
myArray.push(4);  // Add an element to the end of the array
console.log(myArray[0]);  // Access an element by index
for (let item of myArray) {
    console.log(item);  // Loop over the array
}

// FUNCTIONS
function sayHello(name) {
    console.log(`Hello, ${name}!`);
}
sayHello("John");  // Call the function

// OBJECTS
let myObject = {
    name: "John",
    age: 30,
    hobbies: ["reading", "playing guitar"]
};
console.log(myObject.name);  // Access a property by name

// DOM MANIPULATION
let myElement = document.getElementById("my-element");  // Get an element by ID
myElement.innerHTML = "Hello, world!";  // Change the element's content
myElement.style.color = "red";  // Change the element's style

Eisp[edytuj]

;; Comments start with double semicolons

;; VARIABLES
(setq x 5)  ; Integer
(setq y 3.14)  ; Floating-point number
(setq z "Hello, world!")  ; String
(setq is_true t)  ; Boolean

;; PRINTING
(message "Hello, world!")  ; Print a string
(message "%d" x)  ; Print a variable

;; INPUT
(setq name (read-string "What is your name? "))  ; Get input from the user

;; CONDITIONALS
(if (> x y)
    (message "x is greater than y")
  (if (< x y)
      (message "x is less than y")
    (message "x is equal to y")))

;; LOOPS
(dotimes (i 5)  ; Loop 5 times
  (message "%d" i))

(while (> x 0)  ; Loop until x is 0
  (message "%d" x)
  (setq x (- x 1)))

;; LISTS
(setq my-list '(1 2 3))  ; Create a list
(add-to-list 'my-list 4)  ; Add an element to the end of the list
(nth 0 my-list)  ; Access an element by index
(dolist (item my-list)  ; Loop over the list
  (message "%d" item))

;; OBJECTS
(defclass person ()
  ((name :initarg :name :type string)
   (age :initarg :age :type integer)))

(defun say-hello (person)
  (message (concat "Hello, my name is " (oref person name)
                    " and I am " (number-to-string (oref person age))
                    " years old.")))

(setq p (make-instance 'person :