Friday, December 9, 2016

Python variable, type and comment (second class)


Today we discuss variable, comment and type.

Variable:

what is variable?

Do you ever see Containers?. If so, then you know what they do?

They store so many things. That’s a variable in programming. They

save information or hold information. Variable purpose is to label

and store data in memory.

In real world everything object. In programming world object and 

variable exist. But most important variable. Open your interactive 

shell.

Example:

print “My name is joy”

my name is the joy

now we can write differently use a variable.

name=”joy”

print “my name is“,name

my name is a joy

x=1
x=x+1
x
guess the output?
2

x=1
y=2
z=x+y
z

guess the output?

3

age=14

print “my age is”,age

guess the output?

My age is 14





okay. Now I am coming to describe you why I write this?


First of all, I can use variable many times. One variable I can use million of times. Look at first example.

Name=”joy”

joy” is a string. We talk about string later. We save this string into Name variable. Here Name saves joy. Every time you write Name it shows “joy”. You can save anything into the variable, then reuse it.

Second example:

1 x=1
2 x=x+1
3 x

Here x equal 1 means 1 saves into x.

Line 2

x = x + 1

Here left x new variable. It will completely new. It has stored the new value. Right-hand side x+1. Here x value 1 and plus 1 means 2.

Line 3

x=2

x = 1

x = x + 1
x = 1 + 1
x = 2

I hope you understand now. If you don’t understand then use another variable.


x = 1

y = x + 1

y

guess the output?

2

Here y is completely new variable and it saves value 2. You can use anything.

The variable name also very important. If you declare variable, you follow some rules. If you don’t follow these rules python gives you an error. So you have to follow these rules.

1. Basically, when you declare variable, you must declare meaning

full variable name. Why? When someone reading your code or after two

months when you read the code and you don’t understand what your

code doing and what happened?. That’s really bad for other

programmer because they don’t understand your code and they don’t

like you.


Tip: Always declare a meaning full variable name.

Example:

firstName = “Iftekhar”

lastName = “joy”

Here I am using camel case variable. You can use anything

firstname = “Iftekhar”

last name = “joy”

first_name = “Jack”

last_name = “Reacher”

we can use an underscore(_). But don’t use space. It will give you an error.

first name = ‘jack’

this time you encounter an error. The error name is a syntax error.

SO, declaring variable don’t use space between two words.

Number_1 = 1

Number_2 = 2

total = Number_1 + Number_2

total


























see that you can use numeric with the variable.

But you don’t use number first. Like

12name=”joy”

it will give a syntax error.




























So, keep remember that when you use variable you give meaning full

variable name.

Comment out

So what is a comment out?
Give you example:

x = 1
y = 2

c = x + y
print c

Now imagine that you are a programmer. But when a nonprogrammer
read this code, is he understand anything?

The answer is No. Then how can I teach them?

You can write the comment in your code. You have to tell other what
is happen your code?. That’s a better programmer sign.

In python, if you want to comment your code then you have to use hash(#). In the single-line comment, you can use #.

# x is a variable. x value is 1
1 x = 1
# y is another variable and value is 2
2 y = 2
# add x and y and save the value in c.
3 c = x + y
# c is a sum of x and y.
4 print c

if you want to write long explanation then use ‘’’ ‘’’ this.

‘’’ ‘’’
personally, I like single line comment.

Variable Type

That’s really important for all programmer. You have to know for
what type of variable you declare.

X = 1
Now I ask What type of variable X is? Is it an integer or float or string.
Then at first you use type() method .

type(X)
it will show you <type ‘int’>

that means it is an integer.

Number_1 = 1.00
type(Number_1)
<type ‘float’>
it’s a float number.

Name = “joy”
type(Name)
<type ‘str’>
it’s a string

type() is a module or function which returns value type.




No comments:

Post a Comment