Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

12/3/22

Advent of Code - Day 1


--- Day 1: Calorie Counting ---

Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.

11/24/22

Where To Learn Python Programming For Free


Hi, guys. Welcome back to answering random questions on the Internet relating to Python programming. And yes, I know I have given this series more to name, but you know, we all know what we're doing here. 
So today we have "where to learn python programming for free" , so you can learn Python programming for free everywhere. It just depends on what level of programming you are at first and you know what you want to use. 
For example, if you prefer using blogs I'd advise programiz.
If you are on YouTube there is freecodecamp there is coding with mosh and there is me
So yeah, you can also go on TikTok or do programming on TikTok, it's usually snippets. It's not going to be detailed. 
You can go for courses like CS50, it's free. That's Harvard Computer Science, about introduction to programming using Python? It's free. The lecturer, I think. What's his name again? David. And he's a very good dude. He explains things very well. He was doing it was his course. I used to learn C or C++, I can't remember.

11/22/22

In Python Program Range(4) Returns...


Hey, guys. Welcome to my series Answering Random Questions in Python programming.
So here we have 'in Python program return for returns.' 
Okay, so it varies. Oh, sorry. No, sorry, wrong answer. Otherwise arbitrary range, is it? 
I mean, we are all familiar with range in statistics and know its current or the data within a starting point and an ending point in python. If we don't, you can't. You can't. 
There are two, actually multiple ways.
Three probably. 
But the first way is to just declare range and declare the ending point as when you pass only one variable into it. And what happens is it starts from zero. For example, here we have range four. So it's going to create a list kind of numbers from zero and stopping at 'n minus one' actually to meet mathematical. 
So it's not going to stop at first. It's going to be zero one, two, three, four isn't going to be included in that range, but you can set your starting point where you have maybe wanted to range from 'two comma four'. 
So the starting point is going to be two and then three because it's engine four. So range just picks a starting point to the number before the ending point. Even starting point isn't giving, then automatically starts at zero. So if you have if you print out range four is going to just print range zero, comma four. But if you now loop it because we have created a data structure. So if you loop treats you now print this. If for example for I in range for print, I then will have zero 1 to 3. All right. Yeah.

11/20/22

In Python Program, a Control Structure Mcq


Welcome to my series "Answering random questions relating to Python programming". So this is an MCQ, we have "in  a Python program, a control structure, (a) direct order of execution, of the statements in the program. (b) detects what happens before the program starts and after it terminates. (c) defines program specific data structures and finally manages the input and the output of control characters. 

I'm going to go through each of the wrong ones first and tell you why. It's obvious this one's for you anyway. Do we know what a control structure is? It's kind of like the sequence of the program. Every single program row, every single programming language is into language, which is the only one I know that doesn't fall into this category of applying am secure. Sorry. Oh, shit. Of applying control structures.

Because you can put 10,000 guys just not style it and using scissors and all. It's going to look exactly how you want it to. But there are certain times when, for example, if I'm declaring a function, I can't code function for function is created and all those little rules because Python isn't exactly very strict with your rules, but all those things get out kind of forms the control structure. 

So it's not just used for managing the input output or the control structure of the control characters. It doesn't define program specific data structures and it doesn't dictate what happens before the program starts and after 10 minutes because it's running through the program. So it's whilst it's ongoing, so directs the order of execution of the statement in the program. Yes. So it's showing how the program should run, what you want to happen first and what you want to happen after that. Yeah, it's like a sequence which your program follows.

11/19/22

When Is Python Programming Used


Hey, guys. Welcome to my series, Answering Questions related to Python. 
So today we're going to answer the question, "when is Python programming used?"  
Now there's like a long list, but whenever you hear Python, the first thing that usually comes to mind is web development, data analysis and visualization, machine learning, A.I, Automation. 
And people also use Python to develop what you can use in programming language to develop another programming language. Not advisable, but some people use Python to develop games. You can use Python to develop low functioning softwares, basic desktop apps and yeah.

11/18/22

Does python require prior coding experience ​


All right. Welcome to my "answering popular questions relating to Python" series. And today, we're going to be answering whether or not Python needs prior coding experience. 
Even though doubt benefits you more. You honestly don't need to have any coding experience to jump into Python. And python can be your very first language for you to enter into the programming field. It's very straightforward. It doesn't have a strict syntax like C++ and all that for you to like follow. Making errors at that beginner stage is very scarce.
 It is user friendly even once you start advancing, but as an introductory language, python is a very good option.

11/9/22

check data type for all elements in a list

For more codes click here 

#check data type for all elements in a list


a = [1,"pease", 4, "dog"]


for i in a:

    if isinstance(i, int):

        print(f"{i} is an integer")

    elif isinstance(i, str):

        print(f"{i} is a string")

    else:

        pass


10/16/22

BMI Calculator in python

A GUI application that takes user inputs to output their BMI

If you are not confident with python click here

For full source code Click here

10/15/22

Acronyms.py

A project to help create acronyms to any sentence passed into the software.

10/1/22

Simple calculator using python and tkinter

 from re import sub

from tkinter import *


#creating a window
root =  Tk()

#decale the default variable
calc = ""

#definig the function
def addi():
    global calc
    calc += "+"
    pop['text'] = calc

def subi():
    global calc
    calc += "-"
    pop['text'] = calc

def divi():
    global calc
    calc += "/"
    pop['text'] = calc

def multipl():
    global calc
    calc += "*"
    pop['text'] = calc

def ze():
    global calc
    calc += "0"
    pop['text'] = calc

def on():
    global calc
    calc += "1"
    pop['text'] = calc

def tw():
    global calc
    calc += "2"
    pop['text'] = calc

def th():
    global calc
    calc += "3"
    pop['text'] = calc

def fo():
    global calc
    calc += "4"
    pop['text'] = calc

def fi():
    global calc
    calc += "5"
    pop['text'] = calc

def si():
    global calc
    calc += "6"
    pop['text'] = calc

def se():
    global calc
    calc += "7"
    pop['text'] = calc

def ei():
    global calc
    calc += "8"
    pop['text'] = calc

def ni():
    global calc
    calc += "9"
    pop['text'] = calc

def decii():
    global calc
    calc += "."
    pop["text"] = calc

def evali():
    global calc
    try:
        calc = eval(calc)
        pop["text"] = calc
        calc = ""
    except:
        pop['text'] = "AN ERROR OCCURRED"
        calc = ""

def cleari():
    global calc
    calc = ""
    pop["text"] = calc



#naming the window
root.title("A SIMPLE CALCULATOR")

#label and entry field
m = Label(root, text="CALCULATOR", font=(25))
pop = Label(root, text='', font=(50))

#operators box
adding = Button(root, text="+",height=2, width=5, command=addi)
subtracting = Button(root, text="-",height=2, width=5, command=subi)
dividing = Button(root, text="/",height=2, width=5, command=divi)
multiplying = Button(root, text="*",height=2, width=5, command=multipl)
equating = Button(root, text="=", height=10, width=13, command=evali)

#numbers
nine = Button(root, text="9", height=2, width=5, command=ni)
eight = Button(root, text="8", height=2, width=5, command=ei)
seven = Button(root, text="7", height=2, width=5, command=se)
six = Button(root, text="6", height=2, width=5, command=si)
five = Button(root, text="5", height=2, width=5, command=fi)
four = Button(root, text="4", height=2, width=5, command=fo)
three = Button(root, text="3", height=2, width=5, command=th)
two = Button(root, text="2", height=2, width=5, command=tw)
one = Button(root, text="1", height=2, width=5, command=on)
zero = Button(root, text="0", height=2, width=5, command=ze)
ccc = Button(root, text="C", height=2, width=5, command=cleari)
deci = Button(root, text=".", height=2, width=5, command=decii)

#placing on screen
m.place(x=190, y=0)
pop.place(x=100, y=30)

#placiing operators
adding.place(x=100, y=60)
subtracting.place(x=150, y=60)
dividing.place(x=200, y=60)
multiplying.place(x=250, y=60)
equating.place(x=300, y=60)

#placing digits
nine.place(x=100, y=100)
eight.place(x=150, y=100)
seven.place(x=200, y=100)
six.place(x=250, y=100)
five.place(x=100, y=140)
four.place(x=150, y=140)
three.place(x=200, y=140)
two.place(x=250, y=140)
ccc.place(x=100, y=180)
one.place(x=150, y=180)
zero.place(x=200, y=180)
deci.place(x=250, y=180)

root.resizable(False, False)
root.geometry("500x500")



root.mainloop()


9/11/22

Problem 2 - The game of Nims / Stones

 In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins.


Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another).


In the outermost loop, we want to keep playing until we are out of stones.


Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able!


Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question.


So, the basic outline of the program should be something like this:


TOTAL = 100


MAX = 5


pile = TOTAL # all stones are in the pile to start


while [pile is not empty]:


while [player 1's answer is not valid]:


[ask player 1]


[check player 1's input... is it valid?]


[same as above for player 2]


Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones.


Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially.


When you're finished, test each other's programs by playing them!


Registration System

 You are making a registration form for a website.

The form has a name field, which should be more than 3 characters long.

Any name that has less than 4 characters is invalid.

Complete the code to take the name as input, and raise an exception if the name is invalid, outputting "Invalid Name". Output "Account Created" if the name is valid.

Sample Input

abc

Sample Output

Invalid Name

Shooting Game

 You are creating a shooting game!

The game has two types of enemies, aliens and monsters. You shoot the aliens using your laser, and monsters using your gun.

Each hit decreases the lives of the enemies by 1.

The given code declares a generic Enemy class, as well as the Alien and Monster classes, with their corresponding lives count.

It also defines the hit() method for the Enemy class.

You need to do the following to complete the program:

1. Inherit the Alien and Monster classes from the Enemy class.

2. Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's hit() method.

Sample Input

laser

laser

gun

exit

Sample Output

Alien has 4 lives

Alien has 3 lives

Monster has 2 lives

9/9/22

Spelling Backwards

 Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.

Sample Input

HELLO

Sample Output

O

L

L

E

H

9/8/22

Letter Counter

 Given a string as input, you need to output how many times each letter appears in the string.

You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values.

Create a program to take a string as input and output a dictionary, which represents the letter count.

Sample Input

hello

Sample Output

{'h': 1, 'e': 1, 'l': 2, 'o': 1}

Fruit Bowl

 You have a bowl on your counter with an even number of pieces of fruit in it. Half of them are bananas, and the other half are apples. You need 3 apples to make a pie.

Task

Your task is to evaluate the total number of pies that you can make with the apples that are in your bowl given to total amount of fruit in the bowl.

Input Format

An integer that represents the total amount of fruit in the bowl.

Output Format

An integer representing the total number of whole apple pies that you can make.

Sample Input

26

Sample Output

4

Popsicles

 You have a box of popsicles and you want to give them all away to a group of brothers and sisters. If you have enough left in the box to give them each an even amount you should go for it! If not, they will fight over them, and you should eat them yourself later.

Task

Given the number of siblings that you are giving popsicles to, determine if you can give them each an even amount or if you shouldn't mention the popsicles at all.

Input Format

Two integer values, the first one represents the number of siblings, and the second one represents the number of popsicles that you have left in the box.

Output Format

A string that says 'give away' if you are giving them away, or 'eat them yourself' if you will be eating them yourself.

Sample Input

3 9

Sample Output

give away


Contact Search

 You are given a list of contacts, where each contact is represented by a tuple, with the name and age of the contact.

Complete the program to get a string as input, search for the name in the list of contacts and output the age of the contact in the format presented below:

Sample Input

John

Sample Output

John is 31

National Economic Freedom

 You are working on data that represents the economic freedom rank by country.

Each country name and rank are stored in a dictionary, with the key being the country name.

Complete the program to take the country name as input and output its corresponding economic freedom rank.

In case the provided country name is not present in the data, output "Not found".

9/7/22

Car data

 You are working at a car dealership and store the car data in a dictionary:

car = {

    'brand': 'BMW',

    'year': 2018,

    'color': 'red'

}

Your program needs to take the key as input and output the corresponding value.

Sample Input

year

Sample Output

2018

Blog Archive