Python_Notes
Python_Notes
rint:
P
print("write here___")
nput:
I
name=input("What is your name?")
print("What a great name "+ name + "!")
ariables:
V
x=5fr4
y=z-3
z=5*x
print(x+y+z)
omment:
C
#This is a comment (Write here)
Other code
f/Else Statements:
I
"""
If/Else Statements
Equals: a == b
Not Equals: a != b
Less than: a<b
Less than or equal to: a <= b
Greater than: a>b
Greater than or equal to: a >= b
"""
x = 5
y = 6
if x < 6:
print("True")
else:
print("False")
ists:
L
fruits = ["apple", "banana", "cherry"]
print(fruits)
or loops:
F
for x in range(1,5):
print("Hello World")
hile Loops:
W
counter = 0
nfinite Loops:
I
while True:
print("This loop will run forever!")
ounting:
C
for x in range(50, 2, -2)
print(x)
while True:
item = input("Enter the name of the item to order (or
'done' to finish): ")
if item.lower() == 'done':
break
elif item in menu:
quantity = int(input(f"How many {item}s would you
like to order? "))
order.append((item, quantity))
total_cost += menu[item] * quantity
else:
print("Sorry, we don't have that item. Please
choose an item from the menu.")
rint("\nYour Order:")
p
for item, quantity in order:
print(f"{quantity} x {item}(s)")
if __name__ == "__main__":
main()
unctions:
F
def greeting():
print("Hello, world!")
greeting()
unctions (add):
F
def add(a,b):
return a + b
unctions (subtract):
F
def subtract(a,b):
return a - b
unctions (multiplication):
F
def multiply(a,b):
return a * b
unctions (division):
F
def divide(a,b):
return a // b
xponentiation:
E
def exponent(a,b):
return a ** b
nth Root:
def root(a,b):
return a ** (1/b)
og:
L
import math
andom:
R
import random
def guess_the_number():
print("🎉 Welcome to the Random Number Guessing Game!
Guess a number between 1 and 100! 🎉\n", "No decimals 🙂\n",
"IF you put a decimal I'll go like this 😈\n")
number_to_guess = random.randint(1, 100)
attempts = 0
max_attempts = 7 # Maximum number of attempts
if guess.isdigit():
guess = int(guess)
if 1 <= guess <= 100:
attempts += 1
rint("😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈
p
😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈
😈
😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈
😈
😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈
😈
😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈
😈
😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈"
😈
)
if attempts == max_attempts:
print(f"😢 Sorry, you've reached the maximum
attempts. The number was {number_to_guess}. 😢")
if __name__ == "__main__":
guess_the_number()
ile:
F
L = ["This is Jeff \n", "This is Joe \n"]
with open("myfile.txt", 'w') as file1:
#Writing data to a file
file1.write("Hello \n")
file1.writelines(L)
ic-tac-toe:
T
USE ONLINE-PYTHON.COM
import random
def print_board(board):
print("\n")
print(" 0
1 2")
for row_index, row in enumerate(board):
print(f"{row_index} " + " | ".join(row))
if row_index < 2:
print(" ---|---|---")
def is_board_full(board):
return all([spot != " " for row in board for spot in row])
def ai_move(board):
# Simple AI strategy: choose a random empty spot
empty_spots = [(r, c) for r in range(3) for c in range(3)
if board[r][c] == " "]
if empty_spots:
row, col = random.choice(empty_spots)
board[row][col] = "O"
def tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"
while True:
print_board(board)
if current_player == "X":
try:
row = int(input(f"Player {current_player},
enter the row (0, 1, or 2): "))
col = int(input(f"Player {current_player},
enter the column (0, 1, or 2): "))
if row < 0 or row > 2 or col < 0 or col > 2:
print("Invalid input. Please enter a row
and column between 0 and 2.")
continue
except ValueError:
print("Invalid input. Please enter numerical
values for row and column.")
continue
board[row][col] = current_player
else:
ai_move(board)
if check_winner(board, current_player):
print_board(board)
print(f"Player {current_player} wins!")
break
if is_board_full(board):
print_board(board)
print("It's a tie!")
break
if __name__ == "__main__":
tic_tac_toe()