Learn to Code via Tutorials on Repl.it!

← Back to all posts

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")``````

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
• Create an interface to the db
- Create a class that contains functions to
- 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.

hotnewtop
ZenoxHosting (12)

To be honest, this was a pretty good tutorial.

CodeLongAndPros (1589)

@ZenoxHosting I strive for that in all of them

EpicGamer007 (1642)

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

CodeLongAndPros (1589)

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

EpicGamer007 (1642)

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

firefish (915)

dot file manager

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

firefish (915)

@CodeLongAndPros not really it's in the command palate

firefish (915)

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

firefish (915)

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

firefish (915)

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

firefish (915)

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

Bookie0 (6028)

pretty nice!

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