Saturday, December 10, 2016

Python String


Today is my third tutorial. Today I am discussing String only

string. So, Let’s begin our string journey. The string is a big 

Topic. This is our first String Class.

STRING



What is String?

A – Z all are characters. We make words and writes sentence using

those characters. A string is a sequence of characters. Example :

joy, Mother, Father and so on. String are like the sentence. String

are very important when you communicating to the user. Keep remember

when using string your program always use quote I.e: “This is a

string.” You can use single quote also. Like ‘I am joy’.




Ok,ok Now we are focusing on Python string. Python string are very

easy but you have to remember some rules and methods. If you

remember those methods and rules, your life will be easy.


Always remember that python strings are immutable.




What the hack? What is immutable? I never heard that?

Once python string created , you can not change the character.
That is immutable. Nothing fancy. Now let’s get coding.



# declare a variable.
# assign a string in this variable.
# use the type() to check the variable type.



>>> country = “ Bangladesh”
>>> type(country)
<type ‘str’>



See that country variable type is the string. You can also print this
variable.
>>> print country
We can also single quotation.
>>> city=’Dhaka’
>>> city



















If you are genius or brilliant like me , then use this example and
see what happened?
>>> country = ‘usa”













see that, I start single quotation and finish double quotation. Very
good. Now you encounter an error.
Error name is SyntaxError: EOL while scanning string literal.

EOL means End of line. This means program found end of line but
doesn’t found string.
Now, you tell me that I want to use single quotation on my string.

>>> name = “Adam’s “
>>> name
Adam’s”
You can also write this.
>>> name = “Adam\’s”
>>> name


















I use backward slash (\) before the single quote.



Accessing Values in Strings

I give you a string. Now you ask me that how I can access string
character? Can I access string character?

My answer is YES. You access string character. But how?. If you want
to access string character, you have to use this standard [] syntax.
But I want to tell you something important very important which is
Indexing.

What is Indexing?
The index is such kind of method where you put numeric values and it
gives you a value. When you use indexing you will have to use []
notation. Let’s coding. Write all code.

Tips: If you don’t write code, you don’t learn anything.


Index number always start with zero.
>>> country = ‘Bangladesh’
Here, Bangladesh is a 10 character. You want to access B.
B a n g l a d e s h
0 1 2 3 4 5 6 7 8 9


>>> country [0]
>>> ‘B’
>>> country [1]
a’












You can access one character like this.
You can also measure string length. There is a built-in function
len(). len() is a built-in function which tells your string length.



>>> country = 'United States Of America’
>>> len(country)












Here space is also character. len() counts space as a character.
length shows 10. Country last index is = len(Country)-1=9


>>> Country[10]
IndexError: string index out of range.




Country[10] give me an IndexError. That means my index range is 0 t0
9. But I give 10. That’s why index error happened.


RED-ALERT String immutable

Immutable means you don’t change your string character. Example:


>>> name = “ Albert”
>>> name[0] = “a”
TypeError: ‘str’ object does not support item assignment



















That means if you already created a string, you can’t change the
string. That means immutable or Non-mutable.

That’s for today. The string is a big topic. We will discuss next Topic.

Be Safe, make other people happy and Be happy.


Important link:

https://developers.google.com/edu/python/strings
tutorials point
Basic String
learn python the hard way
http://www.pythonforbeginners.com/basics/string-manipulation-in-python 

https://www.programiz.com/python-programming/string



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.




Friday, December 2, 2016

Python: Python beginner first class

Python: Python beginner first class: Python is an Interpreted language. Wait a minute! what is an interpreted language? Interpreted language: --------------------------- ...

Python beginner first class

Python is an Interpreted language. Wait a minute! what is an interpreted language?

Interpreted language:
---------------------------

# Interpreted is not the property of Programming language rather it's a property of implementation.

# Interpreted language fully depends on language implementation.

# An Interpreted language like python, a ruby that's language implementation execute instruction directly.

# In Interpreted language code run line by line.So that's why python runs the code line by line.

#Python will fall under bytecode interpreted.

# .py source code at first complied at bytecode .pyc.  This bytecode can be interpreted by official CPython.




That's it.Now open your Interactive shell. If you are in windows go to start and write python and open IDLE(python GUI).


I am using Python 2.7.6 shell. 



Now Let's get start coding.

So our first program:

"Hello world"


press enter and it will show 'Hello world'

2nd program:

print "Hello Python"

output:

Hello Python

Carefully note that first program output give us single quotation and second program only Hello Python because we write a print function in the second program.

Later we talk about function

so, In python, if you want your output without quotation you can use print .

3rd program:
------------------

>>> 1+1
2

>>> 3 * 2
6

>>> 8 - 2
6

>>> 9 / 3
3

>>> 10/3
3

oh, wait. I think you are wrong.where ? 10/3 = 3.33. Hm, write but python

interpreter shows 3. why?

Because 10 and 3 are an integer. That's why interpreter gives the answer integer 3.

But if we define Real number like this:

>>> 10.0 / 3
3.3333333333333335

see output came real number.


Here is the screenshot.
























That's it for today. 



Thanks.



Tips:
---------

practice interactive mode.As much as you practice you quickly learn, if any mistake please comment.




Thursday, December 1, 2016

How to install Python windows?

If you want to learn python, then at first you will have to install python software.First, go to this link https://www.python.org/downloads/release/python-2712/



There is two option.I prefer Windows x86 MSI installer because I install that version. You download this.

Installing Python:
------------------------------

After finishing python x86 MSI installer, then open it.






Click the run button.








If only you use your computer then leave it for install for all users.

If you are multiple accounts on your PC, then choose the second option.





If you want to change to installation folder feel free to change. But I suggest that leave it as it is and click next.






scroll down and select the red  and click next.





we see the windows command prompt and it means installation process start and when you saw that cleaning up... it means python install in your computer.

pip  is package management. pip allow you install all additional python packages from PyPI.




Then click finish.



If you chose to use the 2.7.3 version of Python, you will need to follow these steps. Once you have successfully installed Python, it is time to add it to the System Path Variable. Doing this will allow Python to run scripts on your computer without any conflicts of problems.
Begin by opening the start menu and typing in “environment” and select the option called “Edit the system environment variables.”


Then System Properties open and click the environment variable.


click the new and a box pops-up.
In variable name you can write anything. variable path "C:\Python27\;C:\Python27\Scripts;"
If you change folder destination and write this folder path.


Press “OK,” then “OK,” then “OK,” then the red “X” to accept all changes and exit the “System Properties” window.
Now we  successfully install and add an environment variable, we are ready to open python GUI.

Go to Start menu and type python saw that :

click this IDLE and see that 2.7.6 that's a version number 64 bit version.
now just write this print "Hello, Python!"
it will eventually show this output!
Hello, Python!

That's it for today!