Basics of Python.

Starting Python

Using Interactive Mode

You can work with Python interactively with the Python interpreter. In interactive mode we can work as when we are working in the operating system prompt. Python interpreter you can activate by giving commands to the operating system prompt:

$ Python
Python interpreter will display

Python 1.5.2 (# 1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66 19990314
+ / Linux (egcs-on linux-i386
Mathematisch Copyright 1991-1995 Stichting Centrum, Amsterdam
Three larger signs (>>>) are the main Python prompt. For orders that continues Python will respond with signs three dots (…).

>>> If 1:

To exit the Python prompt into the operating system prompt you can press that produces the final character file, Ctrl-Z (in DOS / Windows) or Ctrl-D (on Unix).

Interactive mode is very useful to try Python commands before making the script. In this paper we will be more work using scripts.

Using Scripts

If you are using interactive mode when creating the program, you can not save it to the storage medium. To overcome this you can create a script with a text editor that produces pure text, such as Notepad or vi. Turn your text editor and type this command lines:

#! / Usr / bin / python
print “Learning Python …”
# End of file
Give the name of this script This script will only print the string Learning Python … to the screen. To run this script we just need to type:

$ Python
the operating system prompt.

The first line is typical for Unix / Linux, which is given if you want to run these scripts without typing interpreter who will translate the script is concerned, of course, after changing the permissions of these scripts into executable. This line will have no effect on Windows operating systems. In the next script examples first line will be eliminated to save space.

The second line serves to print a string enclosed in quotation marks.

The third row is a testimony, or comments, that will not affect the course of the program. Description is always preceded by the # sign. If interpreters find this sign marks the start until the end of the line will be considered as evidence.

Variables and Operators

Creating variables in Python is very simple. You just need to fill in a value to a variable with the type of data you want. Operators are charging equals sign (=).

Variables in Python are very dynamic. Meaning:

variable does not need to be declared to have certain types of data;
variable data types can be changed while the program is running.

Python knows some kind of numbers are whole numbers (integers), fractions (floating point), and complex numbers. Complex numbers can be written in the format (real + imajinerj) or use the complex (real, imaginary). For other types of data without the numbers already described can be understood from the following example:

variabel_integer = 10
variabel_floating = 24.5
variabel_kompleks = 3 +4 j

print variabel_integer
print variabel_floating
print variabel_kompleks
Operators mathematical function normally in Python as in other programming languages. There are some records that must be considered.

Operation charging multiple variables with the same value can be done in one go.
Sign () is used to classify the operations that must be done first.
Integer division with integers will be rounded down.
Integer will be converted to floating point operations involving integers and floating point numbers.
We can not convert complex numbers into real numbers (floating point or integer), only the absolute numbers we can get.
The following examples will illustrate the above statement.

a = b = c = d = 0
print ‘a =>’ a, ‘b =>’ b, ‘c =>’ c, ‘d =>’, d

x = 30 – 10/2
print ‘x =>’, x

y = (30-10) / 2
print ‘y =>’, y

print ’10 / 3 = ‘, 10/3
print ’10 / 3.5 = ‘, 10 / 3.5
print * 12.4 = ‘8 ‘, 8 * 12.4
print ‘abs (1 +9 j) =’, abs (1 +9 j)

The string can be written in three ways:

enclosed in single quotes;
enclosed in double quotation marks;
flanked by three single quotes or three double quotes.
Methods used should be tailored to the needs. Consider the following examples:

print ‘limited apostrophe’
print “bounded double quotes”
print ‘”double quotes” in single quotes’
print “‘apostrophe’ in double quotes”
print ‘\’ single quote \ ‘in single quotes must use character
+ Escape ‘
print “as well as \” double quote \ “inside double quotes”
From this example we can see that we are not able to print a single quote in a string enclosed in single quotes as well. Similarly, for the double quotation marks. But we can use what is called the escape character. This sign tells the interpreter that the character that follows it is not interpreted as usual. In this example, the quotation marks are not interpreted as a boundary string if preceded by the escape character.

Consider also the following example:

print ‘line sentence would seem too long if \
written lengthwise in full. ‘
print ‘escape character is required if you want to print on line \ n New’
print “” “String formatted printable flanked
“Three” apostrophe and ‘three’ double quotes.
With this sign string will be printed as when it was written. “” ”
In connection with a string data type, there are special operators for the string concatenation operator (concatenation operator), ie +, and iteration operators, namely *.

stringA = “A String”
stringB = “String B”
print ‘+ stringA stringB =’ + stringA stringB
print “‘-‘ * 20 = ‘,’ – ‘* 20
Other properties owned by the string is to be drawn in part from its constituent character, because this is a string array, or a row, character. We can take some of the characters from this string with the slice notation. But the characters that make up these strings can not be changed.

stringA = “A String”
print ‘stringA [4] =’ stringA [4]
print ‘stringA [2:6] =’, stringA [2:6]

List called arrays in other programming languages. List is a mix of data types that can have a composition of different. A list can be created by using square brackets, []. Members list are listed in square brackets and each separated by commas. List properties can be listed like this:

Constituent components can be replaced.
Penyusunya component can be read and manipulated directly.
Constituent components can be added.
Constituent components can be taken to indicate the index or slice notation.
Components making up a list can also be a list of the other.
For more details can try the following script:

x = [1.2, ‘three’, ‘four’]
print x

x [1] = x [1] + 2
print x

x [1] = ‘two’
print x

x = x + [‘extra’, 1]
print x

print ‘x [2] =’, x [2]
print ‘x [1:4] =’, x [1:4]

y = [‘Salman’, ‘AS’]
x [0] = y
print x

Unlike the list using index numbers to refer to the contents of a variable, use the dictionary to refer to the contents of key variables. The nature of these two data types differ only in a few things.

To declare a dictionary, Python wear mark {}.

d = {‘name’: ‘Salman U.S.’, ‘value’: 3}
print d
print d [‘name’]
To access the members of a dictionary we use the symbol [] as well as lists.

Dictionary can have a member of a list, or dictionary again.

d = {‘name’: ‘Salman U.S.’, ‘value’: 3}
print d

nama_detail = {‘next’: ‘Salman’, ‘middle’: ‘Agus’, ‘back’: ‘Supriadi’}
print nama_detail

d [‘name’] = nama_detail
print d

print d [‘name’] [‘next’]

nilai_detail = [3,4,2]
d [‘value’] = nilai_detail
print d
Tuple, Between List and String

There is one type of data typical of Python, the tuple. This data type is declared with an asterisk (). Members of the tuple can not be changed and can be either numbers or strings. Tuple can also contain other tuple, dictionary, or list.

menu_file = (‘New’, ‘Open’)
print menu_file

menu_edit = (‘Save’, ‘Save AS’)

menu = (menu_file, menu_edit, ‘exit’, [‘help’])
print menu
print menu [3] [0]

menu_lain = (menu_file, menu_edit, ‘exit’, {‘faq’: ‘faq.html’, ‘manual’: ‘man.html’})
print menu_lain
Command Control


The only command selection in Python is if. The general format of this command is:

if the condition:
     command if the condition is true
elif other conditions:
     command if the other conditions right
     command if no conditions are true
We can try this if the selection with Python in interactive mode. Activate your Python interpreter and type in commands at the Python prompt.

>>> X = 4
>>> If x <5:
… print “% d is less than 5″% x
… elif x == 5:
… print “% d is equal to 5″% x
… else:
… print “% d is greater than 5
4 smaller than 5
To test the condition we can use the operator ==, <, <=,>,> =, and! =.

Something new and we can notice in the example above is a way of writing the program blocks. In Python command blocks marked by writing code that protrudes into the program. Each command has the same left boundary is considered as a block. Wherever possible you should consistently use a space character or tab characters to indent. Do not mix between spaces and tabs. Errors that often occur with this indentation is seen in the appearance of our editors is aligned on the left margin but there was no difference in the number of tab characters or spaces.

In this printing command we use formatting codes% d. This code will be replaced with a variable that follows a string, which is x. Between the string and the variable separated by a% sign. If there are several variables to be printed, all variables should be included in the alert () and between one variable with another separated by commas (,). Let’s try it with the Python interpreter.

>>> X = 4
>>> Y = 2
>>> Print “% d contains x and y equal to% d”% x, y
Traceback (innermost last):
File “<stdin>”, line 1, in?
TypeError: not enough arguments for format string
>>> Print “% d contains x and y equal to% d”% (x, y)
x 4, and y equal to 4
Now we apply this selection in a script that will check if a number is an integer or not.

x = input (“Enter an integer:”)
if x% 2:
    print “% d is an odd number”% x
    print “% d is an even number”% x
When run, the script will display:

$ Python
Enter an integer: 13
13 is an odd number
$ Python
Enter an integer: 24
24 is an even number
Keyword input () function to ask for input in the form of numbers. This number is stored in the variable x. The expression x% 2 will produce 1 if x odd and 0 if x is an even number. By Python, 1 will be interpreted as true and 0 will be interpreted incorrectly.


Recurrence with keywords while having a common format as follows:

while the condition:
if the condition:
command if the condition is true
other command
The commands between while and else will always be executed if the condition is true. The commands in the else will be executed when the loop finishes normally. The meaning here is normal iteration process does not stop because it met the break keyword.

x = 1
while x <5:
    print x
    x = x + 1
    print ‘Loop have been completed!’
If the script above is added a condition that followed the break command, then the command under the else block will never be executed. Note the difference with the following script:

x = 1
while x <5:
    if x == 3:
    print x
    x = x + 1
    print ‘Loop have been completed!’
Break command causes executable commands after the else block, and while this.

The order is for the while loop apart. The basic format for loop is:

for variables in objects:
if it does not meet the break command
Immediately to the example of the use statement for this.

for x in range (1,5):
print x
print ‘The loop is complete’
Builtin function range () in the script generates numbers 1 through 4. The result script will print the numbers from 1 to 4 and the string loop is completed.

Privileged looping with for Python is able to process the array. As the example below:

y = [10,20,30,40,50,60,70,80,90]
for x in y:
    if x == 50:
    if x> 70:
    print x
print ‘The loop is complete’
The results will be obtained if the script is run:

10 20 30 40 60 70
Continue statement will cause the process continues to the start of the loop and skip commands that exist between the end of the block and continue looping.

Similarly, the end of the first part. To learn further wait for the next part of the tutorial.

thank you 🙂



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: