Python programming a Gentle Introduction | Sole Concept Lab
Python For data science

Python programming a Gentle Introduction A noobs guide to python basics

Introduction

Python as a programming language is very easy to learn and it is similar to a spoken language almost. I have been using Python extensively for the past couple of years for two main reasons.

  • It’s a general-purpose scripting language
  • It also supports a lot of statistical tools

Let me tell you a combination of both of them is what makes Python a data scientist’s tool. Well, I forgot to mention that lots of other tools have integrations with Python. To name a few, Spark, Hadoop etc. Not only that lot of other API’s have extensions in Python. Twitter is a very famous example. 

Just a minute more before we get started !!! I forgot to mention that Python is easily scalable and is open source.

I personally use the anaconda distribution of Python. To follow along and learn Python coding basics, I suggest you have Python 3.6 installed. I will keep this post short and crisp so that you don’t spend much time on a single post.

We will cover the following topics in the article today.

  1. As of with every language we will start the Python code with a Hello World print statement
  2. We will proceed to see how to make this print statements more verbose (I mean interactive)
  3. Dive in straight to the variables and various types of variables that can be used in Python.

Let’s dive straight in.

The print statement

The way the print statement operates in Python is super simple. You just say print and in a parenthesis enter the string or the number you want to print. Not tough right? Let’s welcome ourselves into the Python world with the famous “Hello” world statement code as shown below.

1
print("Hello World")

And Voilà there you have it printed on your screen. But let’s customize it now.

Making the print verbose

How would it be if you could ask a users name and welcome him by his name? wonderful isn’t it? That’s what we will achieve using .format() method. So to get a users input we just type in input and assign it to a variable.

I agree the concept of a variable is not touched upon but it’s just something that stores a value for now. Let’s dive into the code right away.

1
2
3
name = input("Whats your name?:  ")

print("Hi {}".format(name))

Let’s break it a bit here. Do you see that I have included a {} in the print statement that is exactly the place where I would like to put in the user’s name!! So what does it do? the input lets the user input his name. Let’s say the user input his name as Bob. Now the print function would display  “Hi Bob”

Pretty simple isn’t it. What if you ask the user to input more than one thing how does that work? Hmmmmmmmmm. We will get back to that after we finish talking about variables in some detail.

Variables:

So what are variables? You know them !!! It’s simple its something that changes. Now, how is this definition going to help me write a Python code? Let’s create a variable and call it x and store a number 2 in it. If you then print x Python will display the number 2. Simple?

Now if you redefine x and store the number 4 in it and print x again what do you think will happen? if you guessed the display would be 4 you are right. The number stored in x has changed that’s exactly why you call it a variable.

How do you actually store a number in a variable the code is below. It exactly depicts the situation above.

1
2
3
4
5
6
7
x= 2 #store the number 2 in a variable x

print(x)

x=4 # now store the number 4 in x

print(x)

Copy paste the code into your Python interpreter see what you get. Easy Peasy !!!

I almost forgot you could also add two numbers or perform mathematical operations by storing 2 or more numbers into variables. I leave it to you to try it out.

So far so good right? But what exactly can you store in a variable? That’s simple too. The following is a list of things you can store in a variable.

  • int number:  In layman terms, it’s just a number without a decimal point for instance 3
  • float:  having numbers after the decimal point in layman terms. \(\pi = \frac{22}{7}\) is a float.
  • A string: A string is just a character, an alphabet or a word a sentence or a special characters for instance.

Its time to code again lets store the words Hi there!  in a variable and also How are you? in another variable and see what you can do with it !! run the code below to understand

1
2
3
4
5
6
7
a = "Hi There!"

b = "How are you?"

c= a+b

print(a+b)

By the way, I just forgot to mention that strings are always written within quotes.

I had raised a question earlier as to what would happen if you input two or more things from the user? How would you display the information? Its simple you can call it a variable!!!! Like the one shown below

1
2
3
4
5
a = input("What is your name?  ")

b = input(" How is your day going?   ")

print( "Hi {x}, I am glad your day is going {y}".format(x=a,y=b)

Could you figure out what is happening? I am sure you can !!!

Conclusion

Congrats on getting started with Python. Now that you have understood how to start coding in Python things will get easy as we move on.

By now you will be familiar with print statements, making them verbose. Apart from this, I walked through variables and the type of data you can store in them. Also, we saw how we can combine print statements with variables. i.e use variables with print statements.

In the next post, I will introduce you to data structures in Python and also control statements.

Happy learning!

Sharing is caring!

Leave a Comment