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

1. python codes and infos

The document contains various Python code snippets demonstrating different functionalities such as counting digits in a string, reversing a list, getting the current date and time, performing basic arithmetic operations, implementing a snake game, and creating a quiz game. Each section includes code examples and explanations for their respective tasks. The document serves as a practical guide for beginners to learn Python programming through hands-on examples.

Uploaded by

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

1. python codes and infos

The document contains various Python code snippets demonstrating different functionalities such as counting digits in a string, reversing a list, getting the current date and time, performing basic arithmetic operations, implementing a snake game, and creating a quiz game. Each section includes code examples and explanations for their respective tasks. The document serves as a practical guide for beginners to learn Python programming through hands-on examples.

Uploaded by

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

1.

# find how many digits in a string


sentence="My457students87"

count=0

for letter in sentence:


if letter.isdigit():
count=count+1

print(count)

2. #find reverse of a list


numbers=[1,2,3,4,5]

reversed_numbers=numbers[::-1]
print(reversed_numbers)

3. # how to find out about the current date and time


current_datetime= datetime.now()

print(current_datetime)

4. # current dayname
current_dayname= current_datetime.strftime("%A")
print(current_dayname)
WARNING DO NOT ADD THE WRITING BELOW:
%a for short days examples:mon
%A for Long days Examples:Monday
%b for Short Months Examples:Mar
%B for Long Months Examples:March
%y for Short Years Examples:23
%Y for Long Years Examples:2023
%H for Hours (00-24) Examples:15
%I/L for Hours (00-12) Examples:3
%M for Minutes (00-59) Examples:55
%S for Seconds (00-59) Examples:40
%H for Hours (00-24) Examples:15
%m for Month Numbers (00-12) Examples:10
%d for Day Numbers (00-31) Examples:7

5. # simple calculator

def add(a, b):


print(a + b)

def sub(a, b):


print(a - b)

def multi(a, b):


print(a * b)

def div(a, b):


print(a / b)

num1 = float(input("Enter First Number=> "))


num2 = float(input("Enter Second Number=> "))
sign = input("Enter what you want to do add, sub, multi or div=> ")
if sign == "add":
add(num1, num2)
elif sign == "sub":
sub(num1, num2)
elif sign == "multi":
multi(num1, num2)
elif sign == "div":
div(num1, num2)
else:
print("You Cannot Do That...")

6. # sanke game
import turtle
import random

w = 500
h = 500
food_size = 10
delay = 100

offsets = {
"up": (0, 20),
"down": (0, -20),
"left": (-20, 0),
"right": (20, 0)
}

def reset():
global snake, snake_dir, food_position, pen
snake = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]]
snake_dir = "up"
food_position = get_random_food_position()
food.goto(food_position)
move_snake()

def move_snake():
global snake_dir

new_head = snake[-1].copy()
new_head[0] = snake[-1][0] + offsets[snake_dir][0]
new_head[1] = snake[-1][1] + offsets[snake_dir][1]

if new_head in snake[:-1]:
reset()
else:
snake.append(new_head)

if not food_collision():
snake.pop(0)

if snake[-1][0] > w / 2:
snake[-1][0] -= w
elif snake[-1][0] < - w / 2:
snake[-1][0] += w
elif snake[-1][1] > h / 2:
snake[-1][1] -= h
elif snake[-1][1] < -h / 2:
snake[-1][1] += h

pen.clearstamps()

for segment in snake:


pen.goto(segment[0], segment[1])
pen.stamp()

screen.update()

turtle.ontimer(move_snake, delay)

def food_collision():
global food_position
if get_distance(snake[-1], food_position) < 20:
food_position = get_random_food_position()
food.goto(food_position)
return True
return False

def get_random_food_position():
x = random.randint(- w / 2 + food_size, w / 2 - food_size)
y = random.randint(- h / 2 + food_size, h / 2 - food_size)
return (x, y)

def get_distance(pos1, pos2):


x1, y1 = pos1
x2, y2 = pos2
distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
return distance
def go_up():
global snake_dir
if snake_dir != "down":
snake_dir = "up"

def go_right():
global snake_dir
if snake_dir != "left":
snake_dir = "right"

def go_down():
global snake_dir
if snake_dir!= "up":
snake_dir = "down"

def go_left():
global snake_dir
if snake_dir != "right":
snake_dir = "left"

screen = turtle.Screen()
screen.setup(w, h)
screen.title("Snake")
screen.bgcolor("blue")
screen.setup(500, 500)
screen.tracer(0)

pen = turtle.Turtle("square")
pen.penup()

food = turtle.Turtle()
food.shape("square")
food.color("yellow")
food.shapesize(food_size / 20)
food.penup()

screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_right, "Right")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")

reset()
turtle.done()

7. # quiz game
print('Welcome to AskPython Quiz')
answer=input('Are you ready to play the Quiz ? (yes/no) :')
score=0
total_questions=3

if answer.lower()=='yes':
answer=input('Question 1: What is your Favourite programming language?')
if answer.lower()=='python':
score += 1
print('correct')
else:
print('Wrong Answer :(')

answer=input('Question 2: Do you follow any author on AskPython? ')


if answer.lower()=='yes':
score += 1
print('correct')
else:
print('Wrong Answer :(')

answer=input('Question 3: What is the name of your favourite website for


learning Python?')
if answer.lower()=='askpython':
score += 1
print('correct')
else:
print('Wrong Answer :(')

print('Thankyou for Playing this small quiz game, you attempted',score,"questions


correctly!")
mark=(score/total_questions)*100
print('Marks obtained:',mark)
print('BYE!')

You might also like