Python Programming: Easy Learning, Notes, and Interactive Tic Tac Toe Code
🕒 2025-04-27 09:29:35.155731What will you learn?
- Introduction to Python programming language and how to set up Jupyter Notebook
- How to print in Python?
- Comment in Python
- How to take input from the user?
- String Operations
- Slicing in Python
- String formatting
- Decision making: if, if-else, if-elif-else
- Looping in Python: for and while
- List
- Tuple
- Dictionary: Key-value pair
- Set
- Function in Python
- Tic Tac Toe game in Python
Introduction to Python programming language
Python programming language is a high-level programming language and it is very easy to use for beginners; it reduces the space as compared to other programming languages. We can use many IDEs: Jupyter Notebook, Sublime Text, Pycharm, Visual Studio Code, Atom, etc.
To set up Jupyter Notebook:
- Download Python in your system from here (when you install Python, you can get Python IDLE and pip; using that pip you can install Jupyter Notebook in your system)
- Open the command prompt and type
pip install jupyter - To open Jupyter Notebook: type
jupyter notebook(You can change the path like d drive, e drive, and particular folder to open Jupiter notebook. ) Note: If you open Jupyter Notebook in e drive, you are not allowed to open any file from other drives like D drive. And if you open Jupyter Notebook in a particular folder, you are allowed to open files that are in that folder only. - Jupyter Notebook allows you to create notes as well as code, making it an excellent IDE for coding.
Python programming language
Python is a high-level programming language that is very easy to use and reduces space as compared to other programming languages like C and C++.
Which IDE to use??
We can use Jupyter Notebook, Pycharm, Visual Studio Code, sublime text, etc.
How to print in Python??
We can use the 'print' keyword to print in Python.
print("My name is Ishwar Gautam")
print(3+4)
3+7
print(3+7)
print(9+6) # it is a good practice to use print statement before
Comment in Python
A comment is not a code, so we can write any human language in a comment. We can use comments so that we can clarify what we are trying to do in the program.
x = 5 # 5 is assigned to x variable (single line comment)
'''Multiline Comment: Hello everyone
Welcome to 30 minutes course
by IG Tech Team'''
How to take input from the user
To take input from the user, we can use input keyword.
input("Enter your name: ")
Note: number can be both integer and string
If you add two strings: '5' + '6' = 56
If you add two integers: 5 + 6 = 11
# program to sum any two numbers input by the user
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print(num1+num2) #string concatenation
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print(num1+num2)
## Considering two inputs at the same time
num1, num2 = input("Enter any two numbers").split()
print(int(num1)+int(num2))
num1, num2 = input("Enter any two numbers").split(',')
print(int(num1)+int(num2))
String operation
We can get the length of the string, get an index of each character in a string, count the repeated number of characters, convert to lowercase or uppercase, and so on.
name = 'Ishwar Gautam'
len(name)
name.index('h')
name.count('a')
name.upper()
name.lower()
Slicing in Python
Slicing is an important concept in Python. It can be used to get part of a string as below.
name = 'IG Tech Team'
name[3:7] #from 3 to 7-1
name[3:] #from 3 to all
name[:7] # from beginning to 7-1
name[3:7:2] # from 3 to 7-1 but step argument 2
# to reverse the string
'''in C or C++, it takes large block of code to reverse the string
but in python, we can do it in just one line of code with the help of slicing'''
name[::-1]
String formatting
name = input("Enter your name: ")
age = input("Enter your age: ")
print("your name is {} and your age is {}".format(name,age))
print(f"Your name is {name} and your age is {age}")
print("your surname is %s and you are %s years old"%(name,age))
Decision making: if, if-else, if-elif-else
if name=='Ishwar':
print("Print True statement")
if name=='FA':
print("Print True statement")
else:
print("Print False statement")
if name=='FA':
print("Print True statement")
elif name == 'Ishwar':
print("Print Else statement")
else:
print("Print False statement")
Looping in Python: for, while
for i in range(5): # i=0,1,2,3,4
print("IG Tech Team")
for i in range(1,5): # i=1,2,3,4
print("IG Tech Team")
i = 0
while(i<5):
print("Welcome to 30 minute course")
i+=1 #i=i+1
List
elements inside a square bracket
a = [3,6,8,5]
type(a)
b = ['Ishwar','IG tech team']
type(b)
c = [3, 8,"Ishwar"]
type(c)
print(c[2])
# add two list
b+c
# append element in list --> always append in last
c.append("Amit")
print(c)
# insert element in list in any place
c.insert(1,"Tech")
print(c)
# delete element from list
c.pop()
print(c)
c.pop(0)
print(c)
Tuple
elements inside parenthesis
a = (3,7,9)
type(a)
b = ("Ishwar","IG")
type(b)
c = ("Ishwar", 3)
type(c)
# add two tuple
b + c
c.append(7) # gives error
# we can't do append, insert, pop in tuple
Dictionary: key-value pair
elements inside curly bracket
dic = {"name":"Ishwar", "age":24}
type(dic)
d = dict(name = "Ishwar", age=24)
type(d)
print(d['name'])
# add element in dictionary
d['address']='Butwal
print(d)
Set
element inside a curly bracket but doesn't have a key-value pair
s = {3,6,8}
type(s)
s = {6.3, "Ishwar"}
type(s)
# list vs set
l = [3,6,7,9,3,4,8,3,6,1,2,0,7]
s = {3,6,7,9,3,4,8,3,6,1,2,0,7}
print(l)
print(s)
# set contains unique elements, so set is very useful when we have to remove redundant element
# remove redundant element from list
l = [3,6,7,9,3,4,8,3,6,1,2,0,7]
l = set(l)
l = list(l)
print(l)
Function in Python
A function is a block of code that can be reusable throughout the program.
syntax: def function_name(parameter){
//code
}
def area_of_square(l):
return l*l
print(area_of_square(5))
Tic Tac Toe game using python
from IPython.display import clear_output
board = {'7': ' ' , '8': ' ' , '9': ' ' ,
'4': ' ' , '5': ' ' , '6': ' ' ,
'1': ' ' , '2': ' ' , '3': ' ' }
def PrintBoard(board):
print(board['7'] + '|' + board['8'] + '|' + board['9'])
print('-+-+-')
print(board['4'] + '|' + board['5'] + '|' + board['6'])
print('-+-+-')
print(board['1'] + '|' + board['2'] + '|' + board['3'])
def game():
turn = 'O'
count= 0
while True:
PrintBoard(board)
move= input("It's your turn "+turn+". Which place do you want to move?")
clear_output()
if board[move] ==' ':
board[move] =turn
count+=1
else:
print("The place is already occupied. Try next one: ")
continue
#Now we will check if player X or Y won or not
if board['7']==board['8']==board['9'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['4']==board['5']==board['6'] != ' ':
print("\n Game over \n")
print("***** " +turn+" won. ****")
break
elif board['1']==board['2']==board['3'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['1']==board['4']==board['7'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['2']==board['5']==board['8'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['3']==board['6']==board['9'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['7']==board['5']==board['3'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
elif board['1']==board['5']==board['9'] != ' ':
print("\n Game over \n")
print("***** "+turn+" won. ****")
break
#incase neither X or O get won
if count==9:
print("Game over!! \n")
print("The game is Draw!!")
break
#changing the player
if turn == 'O':
turn='X'
else:
turn='O'
game()
Conclusion
This is a very basic tutorial on Python programming language. If you are looking for more similar types of course. Here is the list:
- Advanced Python Programming Language
- Python Programming Language Interview Preparations
- Python Programming Language full course in Nepali Language
- Python Programming Language full playlist in the English Language
I hope this post is very helpful to you. If you have any questions, ask me in the comment section. I will reply as soon as possible. Keep learning.
Comments
Loading comments...
Leave a Comment