Search This Blog

Learning Python with Reeborg's World : Day 2

It's day 02. And, I am moving ahead with more interesting concepts in programming. I have decided today I am going to cover the most so, I could get a grasp on all the concepts at the earliest.

Day 02:

Well, how I planned & finalized today's study. It didn't go that way. I went out to solve reeborg's maze challenge that covers all that we learned yesterday. Seems like I need to work more hard on my problem-solving skills.

In the maze challenge, reeborg comes at random positions. I am able to solve most of all parts in a maze. Only, locations near to the corner of the maze I am facing a problem. As of now leading to a never-ending loop.

One more thing, 1002 is pretty dangerous in this world! Yes, true if you see 1002 without having a second thought; just go back to code review. 

In the attached screenshot you can see 1002 execution steps. Sadly, I am stuck at it and will be re-doing this challenge all over again. 

-> 1002 indicates a never-ending loop.



You can check out this challenge right here.

After this, I had moved to recursion and I understood it very well. Yet again for the second time today!!! Reeborg's recursion challenge made me realize: Honey, you're a novice :'( You can't learn python just by cramming concepts. Do more practice! HELL LOT OF PRACTICE.

In short, if we call out the definition of recursion then, it is:

Function calls itself repeatedly. If we're familiar with loops we can learn them easily. 

Syntax:

 def go_home():

    if not at_goal():

        move()

        go_home()


# now do it!

go_home()

You can try this code here under home1 challenge. I am on its counting on challenge. Have few doubts for them sent a mail to Andre Sir. Hopefully, I get a reply from him so, I can re-continue my python study.

For a brief introduction to recursion please go here.

Also, I am thinking to take an udemy course alongside. I am finding difficulty in tracking my progress since I am learning under no guidance. I will do my research for a good course in python and will share if I get myself enrolled in any.

In total, 3-4 hours went by just in these two tasks. However, I am happy I am being consistent on my journey.

Thank You.

Learning Python with Reeborg's World : Day 1

Today, I am on my journey with Reeborg's world as I really like the way how the combination of exercises & tutorials are structured so, one can really sign off after at least something in hand/mind.

So, Day 1 it is & is starting from Quick Python introduction, which has covered basics including definition, function syntax, function naming, etc.

Day 1:

1. Unlike, few programming languages Python Programs follow the flow of execution in the way they are written.

First, I have been introduced to 3 basic function callings:

move()

take()

put()

One makes the reeborg to move, the second instructs to take the object, and the third one orders reeborg to put the taken object.


Reeborg move take put function

2. Now, comes the main question what is a function?

In simple words, a function is a set of instructions for performing actions and is stored for later use.

Syntax of the function:

def non_keyword_function_name:

......useful statements


Note 2.1: Valid function names can start with a letter or underscore but, not with a number.

Note 2.2: Def is a keyword used to define a function/to show the starting of the function precede by a colon. Colon starts the body of the block which constitutes executable instructions. Make sure to indent the statements after the colon.

Note 2.3: Indentation is there to make the program more human-friendly or say to increase the program readability.

3. Now, wrote the first useful python program. By default, reeborg doesn't have a function to switch to turn right. So, here we go with our first square program. Additionally, I have created turn around function so, I can end the square at the position it first started. P.S. Just added to explore.

#created turn around & turn right function


def turn_around():

    turn_left()

    turn_left()


def turn_right():

    turn_left()

    turn_left()

    turn_left()


turn_left()

move()

turn_right()

move()

turn_right()

move()

turn_right()

move()

turn_around()


You can also try to create your own square at the link or can test the code here(click me).

In short, a function is a code that is written so, we can use & re-use when in need. It shortens human efforts. And, can be invoked/called just this way: function_name()


Note 3.1: Parenthesis [one is opening ( and another one is closing ) ] at the end while calling the function is important else function won't get executed. 

Note 3.2: In Python, the technically correct term for “thing” is object; for Python, everything that it knows is an object. e.g: s= 2+6

4. Third is Most used & helpful when it comes to writing repetitive code. That is For loop that allows executing specific instructions which are continually repeating & following the same pattern. For loop is used when you're known to no. of instructions to run. It's the safest loop if you wish to avoid infinite loops.

For instance, while writing the instructions for the turn_right() function. We had to re-write turn_left() as we were in need of 3 repetitive left turns to make it a right turn.

Herein, for loop can save the re-writing efforts.

Syntax:

def turn_right():
......for save_energy in range(0,3): #it will run from 0 to 2. Yeah, not including 3.
...............turn_left()

In this simple case, the code block to be repeated is a single statement.

Note 4.1: Inside range the loop doesn't run fully. As if you've written 0, 3. That means, will run from 0 to 2, not including 3. 

5. When we deal with data then, a need for conditions comes. For this purpose, in python, we have, if-elif-else

Syntax:

if condition:
.....pass
elif condition:
.....pass
else
.....pass

When you're to indicate a condition in negation, not keyword is used. It reverses the value. For example not True -> It will make the true statement False.

Note 5.1: It is not compulsory to write if-elif-else in a pair. You can write just if as well. But when there are two statements then, you can choose whether if-elif or if-else pair.

Note 5.2: Else comes only once for one/particular if block but under if, elif can be numerous.

6. We have one more looping option to deal with repetition. That is while loop. This loop runs until the particular condition becomes true. It's the wildest loop that can sometimes lead to an infinite loop/never-ending loop.

Syntax:

while condition_which_runs _until_it_is_true:
...........pass

Loop will be running over & over by the time you do not reach the true condition.


For a brief introduction to reboorg's python section 1 please refer here.

python hello.py File "", line 1 python hello.py ^ SyntaxError: invalid syntax

 Error: python hello.py File "<stdin>", line 1 python hello.py ^ SyntaxError: invalid syntax while running a python script in command prompt ( windows )


Possible Reasons: 

  1.  The problem is that you are trying to run python hello.py from within the Python interpreter, which is why you're seeing the traceback.
  2. You are not using the command prompt in administrator access.
  3. You are not in the right directory where your python files exist.

Solution: ( OS: Windows 10 )

Step 1: If you are using Windows, press Win+R and type cmd. As shown in the image below. And run it as administrator.


Step 2: Suppose in your computer, hello.py file is at this address: c:\users\yourname\desktop\pythonfiles

So, use the below-mentioned commands to go at the above-mentioned path.

C:\WINDOWS\system> cd..

C:\Windows>cd..

C:\Users>cd users\yourname\Desktop\

C:\Users\engineerbae\Desktop> cd python files

If you're already on the correct path directly jump to step 3.

Step 3: Now, run the hello.py this way: python hello.py in the command prompt.

You shall receive your output :)


Lately, when I ran my first python script using the command prompt. I had been using an interpreter directly and had no clue what's going wrong. 


Hope it helped.

Thank You!