[PyTUT 4] VARIABLES AND COMMENTS

Hi everybody, this is a small tutorial I want to show you before introducing arrays in Python because we will use it in the next tutorial.

Related topics

[PyTUT 2] NUMBERS AND OPERATORS

[PyTUT 3] STRINGS

Variables

To put it simply, we can think of a variable as a name of a value which can be a number, string, list, tuple, or even dictionary. In fact, every time when we declare a variable, the interpreter will allocate memory for its value.

A value can be assigned to a variable by using equal sign = and it can be any type of data such as number, string, or array data type in Python (list, tuple, and dictionary).

>>> a = 8
>>> b = 9.0
>>> c = float(a)
>>> print c
>>> 8.0
>>> d = 'Hello '
>>> e = 'Adele'

To access a value from a string or number type variable, we simply call and perform operations with its name.

>>> s = a + b
>>> print s
>>> 17.0
>>> st = d + e
>>> print st
>>> Hello Adele

See that, whatever you can do with 2 numbers or 2 strings, you can also do with 2 variables which stores 2 numbers or 2 strings.

However, it is different when accessing value from array data types in Python. In the next tutorial, I will introduce to you how to deal with it.

Comments

Sometimes, you may make a long code in your project and it will be tough to work with it again after a long time if you don’t note the meaning of parts in your program. That’s why we need to use comments in our program. A comment is made by putting a hash mark # before a message.

>>> theta = 5.0 # initializing theta
>>> bias = 1.3 # initializing bias
>>> x = 2 # initializing variable
>>> y = theta*x + bias # estimating the model

Although all comments are ignored by Python interpreter, it is an crucial part in a Python program. The more you make good comments in your code, the more you save your time.

That’s all. Is it easy?

In the next tutorial I will introduce array data types used in Python. Well, it will be a long tutorial and you will have a lot of things to do.

 

Hope you enjoy it,

Curious Chick

Advertisements

4 thoughts on “[PyTUT 4] VARIABLES AND COMMENTS

  1. I started python from weird books
    Nice effort you are doing here.

    Like

    1. Hi outlierskrp, thanks for your attention.

      Honestly, my purpose of making this tutorial series is not to introduce all aspects of Python, but try to make a shortened version to support my next series of tutorials about Machine Learning using Python.

      Hope to get some advice from you to make it better!
      Curious Chick

      Liked by 1 person

      1. I’m all ears
        I am reading from python cookbook
        core python programming
        and al sweigart book
        bits here and bits there

        Liked by 1 person

  2. Wow, really hard work.

    I learned Python with two first books you’re reading. I cannot recommend any books that are better than it. On the other hand, I practiced more from exercises in Learn Python the Hard way and the Python Workbook.

    I hope to get some advice from you to make my tutorial better!
    Curious Chick

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close