Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to create a program [ Beginner ]
h
CodeLongAndPros (1632)

Hello, I'm back! (If I ever left?)

I'm also going to use a naming convention now.
[Beginner] assumes only basic knowledge in the target language.
[Intermediate] assumes average level knowledge in the target language.
[Expert] assumes, well, expert level knowledge in the target language.

How to program (for beginners)

Sometimes you'll find yourself with something you want to do, but not know how to actually go about doing it.
(This tutorial is aimed at beginners)

So let's use the example of a program that checks if it's a palindrome.

We can break this down into a couple of steps:

  • Get the user input & clean the string
  • Reverse it and check if it's the same
  • Output the results

Get user input & clean the string.

We can break this down even further:

  • Get user input
  • Remove any non-alphanumeric chars.

So let's start our code:

original = input("> ")
original_clean = ''.join([c for c in original if c.isdigit() or c.isalpha)])

You might be confused about what means.

''.join(list) makes a array of chars ['f', 'o', 'o'] into a string (returns "foo")

We then use a generator to get every char in the input if it is a letter or a number (remove anything not matching)

And we now sanitized our input.

Reverse the string & check if it's a palindrome

So now we have a clean string, so now we need to reverse it.
(To reverse a string we use str[::-1])
So we can store the reversed string into a var:

original = input("> ")
original_clean = ''.join([c for c in original if c.isdigit() or c.isalpha)])
reversed = original_clean[::-1]

After this we compare to see if they are the same:

original = input("> ")
original_clean = ''.join([c for c in original if c.isdigit() or c.isalpha)])
reversed = original_clean[::-1]
pali = reversed == original_clean

Output the results

So now we find ourselves on the last step, outputting it.

I'm just going to use a one-liner since that's the most efficent.

So now we have this:

original = input("> ")
original_clean = ''.join([c for c in original if c.isdigit() or c.isalpha)])
reversed = original_clean[::-1]
pali = reversed == original_clean
print(f"{original} is a palindrome}" if pali else f"{original} is not a palindrome")

And that's how you do a simple program.

But what if we want to do a big program?

So I've made a 'dot file manager', and these are the steps I took:

  • Make a database
    - Create it in the correct format
    - Add a default entry
  • Create an interface to the db
    - Create a class that contains functions to
    - Add an entry
    - Edit one
    - Delete one
  • Create a CLI for it
    - Add commands for all of the database operations above

Ok, that's it, folks.

After reading this, you should have, if even a basic idea, on how to tackle large programming projects.

Comments
hotnewtop
ZenoxHosting (14)

To be honest, this was a pretty good tutorial.

CodeLongAndPros (1632)

@ZenoxHosting I strive for that in all of them

EpicGamer007 (1755)

P P... P ... PYTHON???????????!??!??!!!!??

CodeLongAndPros (1632)

@EpicGamer007 Just as an example. The only reason was the string slicing, actually.

EpicGamer007 (1755)

@CodeLongAndPros , ok... i was a bit sus you converted to #python_supremacist ...

firefish (954)

dot file manager

Hm.... the advantage of following you on GitHub

firefish (954)

@CodeLongAndPros not really it's in the command palate

firefish (954)

@CodeLongAndPros Oh no wrong comment again. what happens when you have a billions different pings at 11pm.

firefish (954)

@CodeLongAndPros Also why do you need a discord bot for a file manager

firefish (954)

@CodeLongAndPros well where are the files that are being managed via discord, on the cloud? On the local file system? On AOZ? Where?!

firefish (954)

@CodeLongAndPros Who's home directory? The user? How will that work through discord?!

Bookie0 (6390)

pretty nice!

Only little tip maybe put those small sections of code in back quotes to make it monotext ;)