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

python

The document contains Python code snippets demonstrating various programming concepts such as variables, conditional statements, loops, functions, classes, and a TicTacToe game implementation. It includes examples of defining functions, using recursion for factorial calculation, and implementing a class with methods. The code also showcases user input handling and game logic for a TicTacToe game.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python

The document contains Python code snippets demonstrating various programming concepts such as variables, conditional statements, loops, functions, classes, and a TicTacToe game implementation. It includes examples of defining functions, using recursion for factorial calculation, and implementing a class with methods. The code also showcases user input handling and game logic for a TicTacToe game.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

name='Alice'

age=25
pi=3.14
print(name)
print(age)
print(3.14

output:
Alice
25
3.14

USEING IF FUNCTION:

if age>18:
print('Adult')
else:
print('Minor')

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

output:
Adult
0
1
2
3
4

IF DEFINE FUNCTION:
def greet(name):
return 'hello,' +name
output = greet('alice')
print (greet('Alice'))
print (greet('UBI'))

output:hello,Alice
hello,UBI

import math
print(math.sqrt(16))

output: 4.0

#print ('hello,world')
a=int(input('enter first number:'))
b=int(input('enter second number:'))
print ('sum:',a+b)

output:
enter first number: 5
enter second number: 5
sum: 10

def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
print(factorial(5))

output: 120
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
fact = int(input('enter factorial number:'))
print (factorial(fact))
output:

class person:
def __init__(self, name, age):
self.name=name
self.age=age

p1 = person('john', 36)

print(p1.name)
print(p1.age)

output
class person:
def __init__(self, name, age):
self.name=name
self.age=age

def __str__(self):
return f"{self.name}({self.age})"

p1 = person('john', 36)
print(p1)

Outut

class person:
def __init__(self, name, age):
self.name=name
self.age=age

def __str__(self):
return f"{self.name}({self.age})"

p1 = person('prasanna', 45)
p2 = person('john', 36)
p3 = person('ullas', 29)
print(p1, p2, p3)

output
class TicTacToe:
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.player = 'X'

def print_board(self):
for row in self.board:
print('|'.join(row))
print('-' * 5)

def is_winner(self, player):


for row in self.board:
if all(cell == player for cell in row):
return True

for col in range(3):


if all(self.board[row][col] == player for row in range(3)):
return True

if all(self.board[i][i] == player for i in range(3)) or all(self.board[i][2


- i] == player for i in range(3)):
return True

return False

def is_full(self):
return all(self.board[row][col] != ' ' for row in range(3) for col in
range(3))

def get_empty_positions(self):
return [(r, c) for r in range(3) for c in range(3) if self.board[r][c] == '
']

def dfs(self, player):


if self.is_winner('X'):
return 1
if self.is_winner('O'):
return -1
if self.is_full():
return 0

if player == 'X':
best = -float('inf')
for r, c in self.get_empty_positions():
self.board[r][c] = 'X'
best = max(best, self.dfs('O'))
self.board[r][c] = ' '
return best
else:
best = float('inf')
for r, c in self.get_empty_positions():
self.board[r][c] = 'O'
best = min(best, self.dfs('X'))
self.board[r][c] = ' '
return best

def best_move(self):
best_score = -float('inf')
move = None
for r, c in self.get_empty_positions():
self.board[r][c] = 'X'
score = self.dfs('O')
self.board[r][c] = ' '
if score > best_score:
best_score = score
move = (r, c)
return move

def play(self):
while not self.is_full():
if self.is_winner('O'):
print("O wins!")
return
if self.is_winner('X'):
print("X wins!")
return

if self.player == 'X':
move = self.best_move()
if move:
r, c = move
self.board[r][c] = 'X'
self.player = 'O'
else:
self.print_board()
r, c = map(int, input("Enter row and column (0-2) for O:
").split())
if self.board[r][c] == ' ':
self.board[r][c] = 'O'
self.player = 'X'

self.print_board()
print("Draw!")

if __name__ == "__main__":
game = TicTacToe()
game.play()

You might also like