IT-info > Python

Python

Python language info

Example code

command = ""
started = False
while True:
    command = input(">" ).lower()
    if command == "start":
        if started:
            print("Car is already started")
        else:
            started = True
            print("Car started...")
    elif command == "stop":
        if not started:
            print("Car is already stopped")
        else:
            stopped = True
            print("Car stopped")
    elif command == "help":
        print("""
start - top start the car
stop - to stop the car
quit - to quit
        """)
    elif command == "quit":
        break
    else:
        print("Sorry, I don't understand that")

Example code

#for item in 'Python':
#for item in ["Banana", "Orange", "Apple", "Kiwi"]:
#for item in [1, 2, 3, 4]:
#for item in range(10): # 0 1 2 3 4 5 6 7 8 9
#for item in range(5, 10) # 5 6 7 8 9
for item in range(5, 10, 2) # 5 7 9
    print(item)

Example code

prices = [10, 20, 30]
total = 0

for price in prices:
    total += price
print(f"Total: {total}")

Example code

for x in range(4):
    for y in range(3):
        print(f'({x}, {y})')

Example code

Example 1

numbers = [5, 2, 5, 2, 2]
for number in numbers:
    print('x' * number)

Result:

xxxxx
xx
xxxxx
xx
xx

Example 2

numbers = [5, 2, 5, 2, 2]
for number in numbers:
    output = ''
    for count in range(number):
        output += 'x'
    print(output)

Result:

xxxxx
xx
xxxxx
xx
xx

Example code

numbers = [3, 6, 2, 8, 10, 4]
max = numbers[0]
for number in numbers:
    if number > max:
        max = number
print(max)