Maven_super_31_100_Python_1_50 - itnett/FTD02H-N GitHub Wiki

Her er en omfattende liste over de 100 mest grunnleggende kommandoene, funksjonene og mulighetene i Python, sammen med detaljerte lenker, eksempler og grundig kommentering for å hjelpe deg med å lese og tolke koden.

1. Print Statement

print("Hello, World!")

2. Variables

x = 5
y = "John"

3. Data Types

x = 5          # int
y = "Hello"    # str
z = 3.14       # float

4. Type Conversion

x = int(3.14)    # x blir 3
y = str(123)     # y blir "123"

5. Strings

greeting = "Hello"

6. String Concatenation

full_name = "John" + " " + "Doe"

7. String Methods

text = "hello world"
print(text.upper())

8. User Input

name = input("Enter your name: ")

9. Comments

# This is a comment

10. Arithmetic Operations

sum = 5 + 3
diff = 5 - 3
product = 5 * 3
quotient = 5 / 3

11. Boolean Values

is_sunny = True

12. Comparison Operators

x = 5
y = 10
result = x < y

13. Logical Operators

result = True and False

14. If Statement

if x > y:
    print("x is greater than y")

15. Elif and Else

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

16. For Loop

for i in range(5):
    print(i)

17. While Loop

while x < 5:
    print(x)
    x += 1

18. Functions

def greet(name):
    return f"Hello, {name}"
  • Lenker:
    • [W3Schools - Python Functions

Fortsetter med de grunnleggende Python-kommandoene og gir deg omfattende eksempler og lenker for hver kommando:

18. Functions (fortsatt)

def greet(name):
    return f"Hello, {name}"

19. Parameters and Arguments

def add(a, b):
    return a + b

20. Return Statement

def square(x):
    return x * x

21. Lists

my_list = [1, 2, 3, 4, 5]

22. List Methods

my_list.append(6)
my_list.remove(3)

23. Tuples

my_tuple = (1, 2, 3)

24. Dictionaries

my_dict = {"name": "John", "age": 30}

25. Dictionary Methods

my_dict["email"] = "[email protected]"
email = my_dict.get("email")

26. Sets

my_set = {1, 2, 3, 4, 5}

27. Set Methods

my_set.add(6)
my_set.remove(1)

28. List Comprehensions

squares = [x**2 for x in range(10)]

29. Nested Lists

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

30. Conditional Expressions

result = "Pass" if score > 60 else "Fail"

31. Lambda Functions

square = lambda x: x ** 2

32. Map Function

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, nums))

33. Filter Function

nums = [1, 2, 3, 4]
evens = list(filter(lambda x: x % 2 == 0, nums))

34. Reduce Function

from functools import reduce
sum = reduce(lambda x, y: x + y, [1, 2, 3, 4])

35. Zip Function

names = ["

Her er en videreføring av listen over de 100 mest grunnleggende Python-kommandoene, funksjonene og mulighetene, med detaljerte lenker, eksempler, og grundig kommentering:

### 35. **Zip Function**
```python
names = ["John", "Jane", "Doe"]
ages = [25, 30, 35]
combined = list(zip(names, ages))

36. Enumerate Function

for index, value in enumerate(['a', 'b', 'c']):
    print(f"{index}: {value}")

37. Slice Notation

my_list = [1, 2, 3, 4, 5]
sliced = my_list[1:3]

38. List Indexing

first_item = my_list[0]

39. Dictionary Comprehensions

my_dict = {x: x**2 for x in range(5)}

40. Generators

def countdown(num):
    while num > 0:
        yield num
        num -= 1

41. Yield Statement

def generator():
    yield 1
    yield 2
    yield 3

42. Exceptions

try:
    x = 10 / 0
except ZeroDivisionError:
    print("You can't divide by zero!")

43. File Handling

with open('file.txt', 'r') as file:
    content = file.read()

44. Reading Files

with open('file.txt', 'r') as file:
    lines = file.readlines()

45. Writing to Files

with open('file.txt', 'w') as file:
    file.write("Hello, World!")

46. Appending to Files

with open('file.txt', 'a') as file:
    file.write("\nNew line")

47. Closing Files

file = open('file.txt', 'r')
content = file.read()
file.close()

48. Context Managers

with open('file.txt', 'r') as file:
    content = file.read()

49. Decorators

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

50. Class Definition

class MyClass:
    def __init__(self, name):
        self.name = name
⚠️ **GitHub.com Fallback** ⚠️