Thursday, December 15, 2016
Python list
Python List
1st part
Today we discuss list. This topic is very important. When we
go to the real programming world, the list is our daily friend. So,
please ask anything if you don’t understand and write all the code
idle. Always write down all the code. Let’s get the start.
If you know c programming language or c++, then you know the name of
array. List and array are the same things. List is a basic data structure
of python. In my table, I have a book, pen, glass, water, laptop, phone.
Now I want to make a list all of these items and print these
items. The first question you asked how I store all the data in a
list. Give you an example.
>>> li = [‘lamp’,’laptop’,’cell phone’,’pen’,’stapler’,’water’]
>>> li
[‘lamp’,’laptop’,cell phone’,’pen’,’stapler’,’water’]
These all items are the string. We use square brackets [] to store all
these items. Do you want to ensure that variable li are a list?
Use type() method. If you want to see the type of li, then you saw the list.
>>> type(li)
<type ‘list’>
>>> len(li)
6
li length is 6. Because here are 6 items.
Python list index
python list index are start from zero(0). Array index also start
from zero(0).
>>> li[0]
‘lamp’
>>> li[1]
‘laptop’
>>> li[2]
‘cell phone’
>>> li [3]
‘pen’
>>> li [4]
‘stapler’
>>> li [5]
‘water’
>>>li[6]
it will give you indexError. List index out of range.
Because in my list, I have 6 item. But you give index number 6 to
access seven number item but seven number item does not exist in
your list.
Now, you ask me can I use negative number?. Yes, you can. Using
the negative number, you can access list item. See that:
>>> li[-1]
‘water’
Type every code. If you don’t code, just read. Then just get lost.
Get the fuck out of here. I don’t want to. Get lost, fucker!!!
when you give -1 it starts with last index. [-1] that always give
you last index value. let’s coding:
>>>li[-2]
‘stapler’
>>>li[-3]
‘pen’
>>>li[-4]
‘cell phone’
>>> li[-5]
‘laptop’
>>> li[-6]
‘lamp’
we can store anything in list not only string. Another Example:
>>> li2 = [‘Bangladesh’, 100, 3.5, ‘Laptop’]
>>> li2
[‘Bangladesh’, 100, 3.5, ‘Laptop’]
what types of items in the list?
>>> type (li2[0])
<type ‘str’>
>>>type(li2[1])
<type ‘int’>
>>> type (li2[2])
<type ‘float’>
so, you can store different types of item in the list.
Now, if we want sublist from the list, you can do that. Example:-
>>> list3 = [“joy”, 23, ”bangladesh”, “Harry Potter”]
>>> list3
[“joy”, 23, ”Bangladesh”, “Harry Potter”]
if we want first three item from list3 then we use slice method.
>>> list3[0:3]
[‘joy’,23,’Bangladesh’]
if I give list3[1:3], what we found?
>>> list3[1:3]
[23,’Bangladesh’]
Now if I give list3[-1:] then -1 to end means ‘Harry Potter’ to ‘Harry Potter’.
>>> list3[-1:]
>>> list3[-1:1]
>>>list3[:-1]
>>>list3[0:4]
>>> list3[0:4:2]
Here two indicate that it increment by 2.
>>> list3[0:4:1]
>>> list3
>>> list3[::1]
>>>list3[::2]
>>>list[::-1]
That’s enough for today. See you soon. Eat, sleep and code.
Tuesday, December 13, 2016
Python beginner string
PYTHON STRING
3rd Part
Today our string chapter last part. Let’s get coding:-
Suppose, you write a string. Let’s write:
>>>str='bcd '
>>> str
'bcd '
str we use extra space. Now you want to remove extra space. How you
remove extra space? We have to use the strip() method. strip() method
remove extra space.
>>> str.strip()
'bcd'
But there is an important point. When we use ‘.strip()’ method it
actually, makes a new string. It does not change the original str.
Now if you want to use new string, then if you have to assign your
new string new variable.
>>> str
>>> str2 = str.strip()
>>> str2
'bcd'
now str2 have no space. Complete new variable.
>>> str = ' a b c d '
>>> str
'a b c d '
Now at first remove first and last space.
>>> str2=str.strip()
>>> str2
‘a b c d’
we don’t change inside a string. Now, Let’s see another example.
>>> str='AbcDeF'
>>> str.upper()
'ABCDEF'
>>> str.lower()
'abcdef'
At first, we use ‘.upper()’ method. This method turn all character
capital. If you want to show all small letter then use lower()
method. But most important str doesn’t change. why every time we use
variable?. Because the string is non-mutable or immutable object.
TIP: You can’t change the Python string.
How to write a reverse string?
>>> school='Newyork'
>>> "".join(reversed(school))
'kroyweN'
That’s it. So, We finish our string class. Now, go to back to your
room and just relax and watch movie and song.Do whatever you like to
do. But when start string goes back to the first part and read
again.Write lot’s code and you became a ninja coder.
Best of Luck.
Monday, December 12, 2016
Python beginner string
PYTHON STRING
2nd Part
This is our python string second part. So, Let’s get coding.
You want to search a word from your string. I give you a 1000 text
file. You want to search your favorite word. How you search this.?
Python has a find() method. Let’s give you a couple of examples.
>>> country = “Bangladesh”
>>> country.find(‘Bangla’)
0
find() method return which position you find ‘Bangla’ string.
you find method found ‘Bangla’ in 0 positions. Again, I give
you the scenario.
B a n l a d e s h
0 1 2 3 4 5 6 7 8
so, when you put ‘Bangla’ in find method, it starts position 0.
That’s why it returns zero. Now, if I give ‘abc’, then find method
returns -1.That means find method does not found your ‘abc’ in
‘Bangladesh’.
>>> country.find(‘abc’)
-1
>>> full_name=”iftekharul islam”
>>> full_name
>>> full_name.find(‘tekharul’)
2
why two? I said that it always start the first position. What is
‘t’ index number in full_name. Think again. I know you know the
answer. you are the best. Yeah, ‘t’ index number is 2.
i f t e k h a r u l
0 1 2 3 4 5 6 7 8 9
Now, you can ask me why ‘.find()’. Why you use dot(.)?. Good
question.Here the country is an object. The string is an object.
String object has a many methods. We discuss object later.
replace() method
Now we want to change string inside. Really? How?>>> Boyfriend = “messi”
>>> Boyfriend.replace(‘messi’,’ronaldo’)
'ronaldo'
you can use variable
>>> ex_girl = “shakira”
>>> current_girl=”Laila”
>>> ex_girl.replace(ex_girl,current_girl)
‘Laila’
>>> ex_girl
‘shakira’
Here, ex_girl still same. When I use replace method ex_girl or
Boyfriend example, replace() method make new string and then return
it. But main string ex_girl does not change.
>>> country = ‘Bangladesh’
>>> new_country = country.replace(‘B’,’b’)
>>> new_country
‘bangladesh’
>>> country
‘Bangladesh’
I am finishing here. I will write string last part. So, stay tune
with me. God Bless you.
Who am I ?
I am Iftekharul Islam. You can call me joy. Basically, I am a self
taught and emotional person. I try to teach myself. I am a full time
computer science student,part-time programmer, and blogger. I like
read and watch MOOC. I like non-fiction books and programming books.
Though I read all Harry Potter books. I am a great fan of Harry
Potter. I am also going Gym not every time. I like to run. I like
morning sun rising. I love a human. In my life, I only care two
things:- Family and people. I love my mom so much. She is my life.
GOAL: Make people happy and Be happy.
Major: Software Engineering.
Email: iftekharjoy1@gmail.com
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 stringcharacter? 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.
>>> 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.
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”
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.
Subscribe to:
Posts (Atom)