Now that we can create and run Python scripts, let's press on.
Let's break down this program into its components. This should make it easier to learn.
Don't worry about memorising this stuff. You are going to see these components again and again, so after a while it will become second nature to you.
It's worth noting here that Python cares about indentation. If I use 4 spaces at the start of a line, so should you!
The highlighted code below is a function. A function does something.
This particular function prints, or outputs to the screen, the text, or string, 'hello, world'
We've decided to call it main. The name main is just a convention. We could have called it anything.
So, to recap, we've defined a function called main, that prints the string 'hello, world'.
That was pretty easy, wasn't it?
The highlighted code below is a conditional statement. It does something if a condition is met.
This particular conditional statement kicks off the main function if somebody's __name__ is __main__.
Who's __name__ are we talking about? The current python source-code file or module, that's who.
Whenever we run a Python module from the cmd prompt, its __name__ is set to __main__.
So, lets see how this fits together.
Python reads a lot like English!
You won't learn Python, or programming in general, by just reading tutorials. You actually need to get your hands dirty.
So...using your current vocabulary and skillset, "Write a Python script that prints the days of the week"
Have a coffee first though. What's the rush?
If you found this useful, please share with friends.comments powered by Disqus