Fundamental Python




In this session your will be know what you need know about Python Fundamental, Ok check it out.. 🙂

1. Printing OUTPUT

To Display the output on Python we can use print() statement
example:

print('Hello World')

The statement in the above will be displayed output string ‘Hello World

Then.. how about if we store the string ‘Hello World‘ in the above with a variable..?, the way is as follow:

variable1='Hello World'
print(variable1')

The output in the code is same as the previous code, the different lies in the variable which we declared to stored a data, ie the string ‘Hello World

Probably one of you who still dont know what is the variable. Its okay.. 🙂
Variable are used to store information to be referenced and manipulated in a computer programming.

Like this, there is a variable which we declared with name A. and the variable will be use to store a value with ‘city name‘ , which the city name is ‘Payakumbuh‘ , to declare is as follows:

A='Payakumbuh'

Then.. how if we want to store a number with data type integer or float, the way is same, and in Python Programming we no needed  notify that is integer or float type like C,C++,Java and other.

2. if elif else

if elif else is a logic branching which very often be used when make application. Statement if will execute a statement if the condition which is given worth is (true). And if worth is (false) the statement will not be executed. And if first condition with the if worth is false, then the condition will be moved to elif statement, and if the first elif statement still be false, the statement will be moved again to the next elif, and if all condition be false, the condition will be moved to the else statement.

example:

1. age=10
2. if age <=12:
3.   print('Primary School')
4. elif age <=15:
5.   print('Junior High School')
6. elif age <=18:
7.   print('Senior High School')
8. else:
9.   print('college or already working')

Can you guess result in that code..?, yea.. the output is ‘Primary School

The explanation by the code in the above is as follows:
First line we store a worth which age in the age variable, then on the second line, if the age is less than or equal to 12 then result or the ouput is on the third line, ie Primary School. And if the condition still false then the condition will be next to code on the fourth line, and if worth still false, it will be next to the code on the sixth line, and if all the condition worth is false, then the statement else will take the statement.

Code in the above seems still not relevant, eg what if we want to insert the age according to our whishes.. how to doing that..?, Ok.. ok.. leet see it. The way is with using input or raw_input in Python 2.

Example:

age=int(input('How old are you:'))

The explanation is: we make the age variable, then because we insert is age (number with integer type) then we must converted it to integer data type, if we not change it, the input or raw_input function only handle data type string. Then how if data which our insert type float ..? the way is same with which change int type on code in the above.

Then in statement input is to get input from keyboard, and asked ‘How old are you:

The details code in the above can be viewed in the following code:

age=int(input('How old are you:'))
if age <=12:
   print('Primary School')
elif age <=15:
   print('Junior High School')
elif age <=18:
   print('Senior High School')
else:
   print('college or already working')

3. loops

In programming often there are instructions which will be doing on over and over, basically you can doing that with write that on over and over. However that is not effective and not efficient. The right way to doing that is with using statement for and while.

* for

Example: how to printing ‘hello world’ 10x ..?

print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')
print('hello world')

or you can printing it whit this code:

a='hello world'
b=a*10
print(b)

How if we printing it 100 or 1000x ..?, no..no..noo.. That is not efficient way, we can use for statement,  in example look in the follows code:

1. for i in range(0, 10):
2.     print('hello world')

Explanation for the code in the above is for i in range 0-10 then will be execute statement print(‘hello world’), here meaning (0,10) is last number minus initial number, if we writing (1,10) then for loop will be execute 9X, because 10-1=9.

And if we look on the code in the above there is identation on statement print(‘hello world’), because in Python we dont using semicolon (;) or curly brackets ({}), but we using identation as marking that element is there on for statement.

* while

If we used for you will understand how much the looping do exactly, but sometimes the looping is condition which we are not know how much the looping do, we just know the looping will be stop if the condition fullfilled or false, and the statement will keep running if the condition still true, the statement which we are used is while.

example:

1. distance=1
2. while distance<= 10:
3.    print('keep walking')
4.    distance+=1

The explanation to code in the above is, on first line we stored a data with value 1 on variable distance. Then on the second line, while distance still less from 10 then statement print(‘keep walking’) will be executed, and if the value or condition already reach 10 then the code will be stopping. And on the fourth line the statement distance+=1 will be added the value distance with 1 until the value reach 10, if we are not added that code on this line, which the code is +=1, then the code will be running continuously beacuse program will be assume the value is always 1 and never added the value, so the code will be not reach the value 10.

In this way we no need to know how much the looping, we just know if the looping will be stoping if the condition is fullfilled. In this case if distance still less from 10, then the looping will conitinue to do that, and the looping will be stop if already according with the value we set.

This is basic Python syntax, which you can learn over and over, because the further will only doing the looping on this lesson. The Python code will be simple like this, and will not be complicated like C,C++, Java and other language.

The code in the above sufficient to Fundamental Python Syntax, but to be more completed, I will addition with data type advance, ie list and ditctionary.

4. Working with List

I will starting with data type which complex and most often used on Python ie list or array  on the other language like PHP, java, etc.

list marked by square brackets ‘[]‘, so.. data in list can be flanked by the brackets.

example:

kota=['Payakumbuh','Bukit Tinggi','Padang','Pekanbaru',4,3,7]
print(kota)

Code in the above will result this output:

['Payakumbuh', 'Bukit Tinggi', 'Padang', 'Pekanbaru',4,3,7]

You can see the code on the above, in a list we can storing many data types at once, even on Python we can storing data type which the data type is different, not like C/C++, Java and other programming language which just can stored one data type on array. You can see it on code on the above which there string type and integer data type.

Then how to if we want to added data on a list ..?, you can use append() statement.

example:

kota.append('Jakarta')
kota.append('Batam')
kota.append('Samarinda')

output from the code on the above after we add a few city name using append() statement:

['Payakumbuh', 'Bukit Tinggi', 'Padang', 'Pekanbaru', 'Jakarta', 'Batam', 'Samarinda']

not attractive if we seems it 🙂

therefore we can use the for statement on list on the above.

example:

for nm_kota in kota:
    print(nm_kota)

Output from the code on the above will make the output more pleasing to see it.

Payakumbuh
Bukit Tinggi
Padang
Pekanbaru
Jakarta
Batam
Samarinda

The explanation from the code on the above is, to nm_kota in kota, print out the list which in the list there stored the city names which we make on the code before. there nm_kota is a element which declarated in a listie kota list.

You can also added new words to code on the above, e.g:

kota='Payakumbuh'
nama2_kota=['Padang','Pekanbaru','Batam','Samarinda','Lhokseumawe']
for dk in nama2_kota:
    print('Anda punya rumah di %s, dan Anda berasal dari kota %s' % (dk,kota))

Output from code on the above will result this:

Anda punya rumah di Padang, dan Anda berasal dari kota Payakumbuh
Anda punya rumah di Pekanbaru, dan Anda berasal dari kota Payakumbuh
Anda punya rumah di Batam, dan Anda berasal dari kota Payakumbuh
Anda punya rumah di Samarinda, dan Anda berasal dari kota Payakumbuh
Anda punya rumah di Lhokseumawe, dan Anda berasal dari kota Payakumbuh

The explanation from the code on the above is same with before code, there different is lies on %s, what is the %s ..?

%s is to take value a data string, which in this case we make 2X %s, first %s is to take value string from dk iteration, and second %s is to take data string from the kota variable. 

That is how to make list variable along the basically what your need to be know, you will most often using list when make a program, but there is one more variable type which you often to using it on Pyhton, ie Dictionary.

 

5. Using Dictionary

Ok.. about Dictionary indeed data type the most basically which underlies all aspect all modern programming languages, e.g object oriented programming with class, indeed all starting from dictionary.

You will be using dictionary very intensive on Python, even might be said convenience web programming on Python is because has data type dictionary. The dictionary which you can import be JSON and e.g (JSON is data type communication between web functioning from aplication which different)

To think about dictionary we can think about human, the human have name, age, sex, etc. e.g i make human which data type dictionary

human={}
human['name']='Mark Zuckerberg'
human['sex']='Male'
human['status']='marriage'
print(human)
{'status': 'marriage', 'name': 'Mark Zuckerberg', 'sex': 'Male'}

If wee look result from dictonary on the above same with list. but that are different. Dictionary is a set data consisting from the key and value, like we look on the above, name, sex, status is the key, and Mark Zuckerberg, Male, marriage is the values from the keys.

That is Fundamentals Python what you need to know, next time i will introducing about at the very fundamental to develops Python big aplication ie Modularization, The Modularization will starring about Modul, Package and last about Class.

Thanks.