Expanding the First Step of Python, Hello Python!!

The first program developers are often introduced to is the “Hello World!”. Irrespective of the language you’re using, you have probably seen one or experienced. This article will let you expand the world of “Hello World!”…..

Checking with Codes

Apart from the talk, Let’s check out the way you would write Hello World in Python. Now here, we will compare it with other languages to have a wider vision of the topic.

Python3

Need to write below line to execute….

print(‘Hello World!’);

Fascinating right? Those of you who are used to JavaScript might not be very impressed. The JS Hello World example wouldn’t be much different.

JavaScript

Yes, again a single line code below :

console.log(‘Hello World!’);

Ruby

Ruby is not so alone….

puts “Hello World!”

Now, To put the simplicity of these into context let’s look at another two examples.

C

#include <stdio.h>

int main(int argc, char* argv[])
{
printf(“Hello World!\n”);
return 0;
}

Java

public class HelloWorld {
public static void main(String[] args) {
System.out.prinln(“Hello World!”);
}
}

There has been a shift in the last few years where the programming community has started to lean towards the prior three languages as introductory languages over the latter two. Perhaps these Hello World’s give you a small taste of why. What do you think?

Okay, back to Python. Get Python Coding Training in delhi at Codec Networks.

Where is Pythonic thing?

This last section to skim the surface of what the word Pythonic is and we will look at a Pythonic Hello World.

What is so “Pythonic” ?

When people think about this question, they may think of Python’s famous

import this

Example. Which when ran will give you this:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

Take from that what you will. Let’s focus on one line from the text.

There should be one– and preferably only one –obvious way to do it.

This line may describes the mentality behind the word Pythonic and idiomatic Python. If you want to start learning python,Python Training in Delhi is available at Codec  Networks, Delhi.

Back to the code

Include a very basic function (oh my!) so it’s not so confusing when we look at the lines.

# is how you start a Python comment

# section one
def main():
print(“Hello World!”)

# section two

if __name__ == “__main__”:
main()

Okay? Tear it down

 

Section 1

def main():
print(“Hello World!”)

Define a function that takes no arguments and doesn’t return any value named main. Print – Hello World! to the console when main is called….

Section 2

if __name__ == “__main__”:
main()

__name__ is assigned to the calling module…

In short:

  • if the module is imported __name__ will be the set to the importing module
  • if the file is directly ran then execute the if statement

Let’s look at one more modified example before we wrap this up

# fcc-greet.py

def greet(name):
print(“Hello {}, welcome to Codec Networks!”.format(name))

if __name__ == “__main__”:
from sys import argv
greet(argv[1]) # first command argument

The print statement and the last line may be a little much for some newer users. Instead of explaining them I’m going to show you two different ways to use our new Python program.

The first is through the terminal/command prompt:

$ python fcc-greet.py t3h2mas

which prints this to the console. Using `fcc-greet.py` as a module:

# my-program.py

import fcc-greet

users = [“Monday”, “Tuesday”, “Wednesday”]
map(fcc-greet.greet, users)

which would output :

Hello Monday, welcome to Codec Networks!

Hello Tuesday, welcome to Codec Networks!

Hello Wednesday, welcome to Codec Networks!

That last example might have a little much going on…Just focusing on the output! Codec Networks provides Python Scripting Training in Delhi and also the online classes globally.

That completes our example program using Pythonic idioms. We finished with a program that can be called from the prompt with a supplied argument as well being used as a module easily from different programs. Python Scripting Certification in Delhi | NCR at Codec Networks, where ideas are the ways to explore, try some more……All the best !!

About

Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *