0% found this document useful (0 votes)
1 views

Python_Notes

This document contains Python programming notes covering various topics such as printing, input handling, variables, control structures (if/else statements, loops), functions, file handling, and basic game implementations like tic-tac-toe and a number guessing game. It includes code snippets demonstrating the usage of these concepts along with comments explaining their functionality. The notes serve as a comprehensive guide for beginners to understand and practice Python programming.

Uploaded by

robinr130609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python_Notes

This document contains Python programming notes covering various topics such as printing, input handling, variables, control structures (if/else statements, loops), functions, file handling, and basic game implementations like tic-tac-toe and a number guessing game. It includes code snippets demonstrating the usage of these concepts along with comments explaining their functionality. The notes serve as a comprehensive guide for beginners to understand and practice Python programming.

Uploaded by

robinr130609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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‬

while counter < 5:‬



print("Counter is at", counter)‬

counter = counter + 1‬

‭nfinite Loops:‬
I
while True:‬

print("This loop will run forever!")‬

‭ounting:‬
C
for x in range(50, 2, -2)‬

print(x)‬

‭enu Ordering Machine:‬


M
# Menu items with prices‬

menu = {‬

"Tape": 5.99,‬

"Thermometer": 8.99,‬

"Paper": 6.49,‬

"Drink": 1.99,‬

"Computer": 269.00,‬

"Battery": 7.99,‬

"Toy": 28.99‬

}‬

‭ Function to display the menu‬
#
def display_menu():‬

print("Menu:" and "Market")‬

for item, price in menu.items():‬

print(f"{item}: ${price:.2f}")‬

‭ Function to take an order‬


#
def take_order():‬

order = []‬

total_cost = 0.0‬

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.")‬

return order, total_cost‬


‭ Main function to run the ordering system‬


#
def main():‬

display_menu()‬

order, total_cost = take_order()‬

‭rint("\nYour Order:")‬
p
for item, quantity in order:‬

print(f"{quantity} x {item}(s)")‬

‭rint(f"\nTotal cost: ${total_cost:.2f}")‬


p
print("Thank you for your order!")‬

if __name__ == "__main__":‬

main()‬

‭unctions:‬
F
def greeting():‬

print("Hello, world!")‬

greeting()‬

‭unctions (add):‬
F
def add(a,b):‬

return a + b‬

‭ = int(input('Enter 1st number: '))‬


a
b = int(input('Enter 2nd number: '))‬

‭esult = add (a,b)‬


r
print("The sum is " + str(result) + ".")‬

‭unctions (subtract):‬
F
def subtract(a,b):‬

return a - b‬

‭ = int(input('Enter 1st number: '))‬


a
b = int(input('Enter 2nd number: '))‬

‭esult = subtract (a,b)‬


r
print("The difference is " + str(result) + ".")‬

‭unctions (multiplication):‬
F
def multiply(a,b):‬

return a * b‬

‭ = int(input('Enter 1st number: '))‬


a
b = int(input('Enter 2nd number: '))‬

‭esult = multiply (a,b)‬


r
print("The product is " + str(result) + ".")‬

‭unctions (division):‬
F
def divide(a,b):‬

return a // b‬

‭ = int(input('Enter 1st number: '))‬


a
b = int(input('Enter 2nd number: '))‬

‭esult = divide (a,b)‬


r
print("The quotient is " + str(result) + ".")‬

‭xponentiation:‬
E
def exponent(a,b):‬

return a ** b‬

‭ = int(input('Enter base: '))‬


a
b = int(input('Enter exponent/power: '))‬

‭esult = exponent (a,b)‬


r
print("The power is " + str(result) + ".")‬

n‭‬‭th Root:‬
def root(a,b):‬

return a ** (1/b)‬

a = int(input('Enter radicand: '))‬



b = int(input('Enter degree: '))‬

‭ositive = root (a,b)‬


p
negative = - root (a,b)‬

if root (a,b) == 0:‬



print("The square root is " + str(positive) + ".")‬

else:‬

print("The square root is " + str(positive) + " and "‬

+ str(negative) + ".")‬

‭og:‬
L
import math‬

def logarithm(a, b):‬



return math.log(a, b)‬

‭ = int(input('Enter anti-log: '))‬


a
b = int(input('Enter base: '))‬

‭esult = logarithm(a, b)‬


r
print("The logarithm is " + str(result) + ".")‬

‭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‬

while attempts < max_attempts:‬



guess = input("Guess a number between 1 and 100: ")‬

if guess.isdigit():‬

guess = int(guess)‬

if 1 <= guess <= 100:‬

attempts += 1‬

if guess < number_to_guess:‬



print("Too low! 📉")‬

elif guess > number_to_guess:‬

print("Too high! 📈")‬

else:‬

print(f"🎉 Congratulations! You guessed‬

the number {number_to_guess} in {attempts} attempts. 🎉")‬

break‬

else:‬

print("Please enter a number between 1 and‬

100.")‬

else:‬

for x in range(20):‬

‭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)‬

with open("myfile.txt", 'a') as file1:‬



# Appending‬

file1.write("Today is December, 12, 2024")‬

with open("myfile.txt", "rt") as file1:‬



# Reading form a file‬

print(file1.read())‬

‭ython Sandbox | Turtle Mode‬


P
Drawing (Firework):‬

import turtle‬

import random‬

‭ Set up the screen‬


#
screen = turtle.Screen()‬

screen.bgcolor("black")‬

‭ Create a turtle object‬


#
firework = turtle.Turtle()‬

firework.shape("circle")‬

firework.speed(0)‬

firework.hideturtle()‬

‭ Function to draw a firework‬


#
def draw_firework(color, x, y):‬

firework.color(color)‬

‭irework.penup()‬
f
firework.goto(x, y)‬

firework.pendown()‬

firework.showturtle()‬

for _ in range(36):‬

firework.forward(100)‬

firework.right(170)‬

firework.hideturtle()‬

‭ Draw multiple fireworks‬


#
colors = ["red", "yellow", "blue", "green", "purple",‬

"orange"]‬

for _ in range(5):‬

color = random.choice(colors)‬

x = random.randint(-200, 200)‬

y = random.randint(-200, 200)‬

draw_firework(color, x, y)‬

‭ Keep the window open‬


#
screen.mainloop()‬

‭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 check_winner(board, player):‬



# Check rows, columns, and diagonals‬

for row in board:‬

if all([spot == player for spot in row]):‬

return True‬

for col in range(3):‬

if all([board[row][col] == player for row in‬

range(3)]):‬

return True‬

if all([board[i][i] == player for i in range(3)]) or‬

all([board[i][2 - i] == player for i in range(3)]):‬

return True‬

return False‬

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‬

if board[row][col] != " ":‬



print("Spot already taken. Try again.")‬

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‬

current_player = "O" if current_player == "X" else "X"‬


if __name__ == "__main__":‬

tic_tac_toe()‬

You might also like