Category Archives: Python

Input from Keyboard

Input via keyboard There are hardly any programs without any input. Input can come in various ways, for example from a database, another computer, mouse clicks and movements or from the internet. Yet, in most cases the input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string.

If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The text of the optional parameter, i.e. the prompt, will be printed on the screen.

The input of the user will be interpreted. If the user e.g. puts in an integer value, the input function returns this integer value. If the user on the other hand inputs a list, the function will return a list.

Let’s have a look at the following example:

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are are already " + str(age) + " years old, " + name + "!")

We save the program as “input_test.py” and run it: Continue reading Input from Keyboard

Arithmetic and Comparison Operators

Introduction

This chapter covers the various built-in operators, which Python has to offer.

Operators

These operations (operators) can be applied to all numeric types:

Operator Description Example
+, – Addition, Subtraction 10 -3
*, /, % Multiplication, Division, Modulo 27 % 7
Result: 6
// Truncation Division (also known as floordivision or floor division)
The result of this division is the integral part of the result, i.e. the fractional part is truncated, if there is any. If both the divident and the divisor are integers, the result will be also an integer. If either the divident or the divisor is a float, the result will be the truncated result as a float.
>>> 10 // 3
3 >>> 10.0 // 3
3.0 >>>
+x, -x Unary minus and Unary plus (Algebraic signs) -3
~x Bitwise negation ~3 – 4
Result: -8
** Exponentiation 10 ** 3
Result: 1000
or, and, not Boolean Or, Boolean And, Boolean Not (a or b) and c
in “Element of” 1 in [3, 2, 1]
<, <=, >, >=, !=, == The usual comparison operators 2 <= 3
|, &, ^ Bitwise Or, Bitwise And, Bitwise XOR 6 ^ 3
<<, >> Shift Operators 6 << 3

 

Sumber: https://www.python-course.eu/operators.php

Data Types and Variables

Introduction

Even if you think that you know a lot about data types and variables, because you have programmed lower level languages like C, C++ or other similar programming languages, we would recommend to read this chapter. Data types and variables in Python are different in some aspects from other programming languages. There are integers, floating point numbers, strings, and many more, but things are not the same as in C or C++. If you want to use lists in C e.g., you will have to construe the data type list from scratch, i.e. design memory structure and the allocation management. You will have to implement the necessary search and access methods as well. Python provides power data types like lists as a genuine part of the language. Continue reading Data Types and Variables

Structuring with Indentation

Most programming languages use certain characters or keywords to group statements:Blocks in Python through indentation

  • begin … end
  • do … done
  • { … }
  • if … fi

Python uses a different principle. Programs get structured through indentation, this means that code blocks are defined by their indentation. Okay that’s what we expect from any program code, isn’t it? Yes, but in the case of Python it’s a language requirement not a matter of style. This principle makes it easier to read and understand other people’s Python code.

So, how does it work? All statements with the same distance to the right belong to the same block of code, i.e. the statements within a block line up vertically. The block ends at a line less indented or the end of the file. If a block has to be more deeply nested, it is simply indented further to the right.

A statement can be continued on the next line with the continuation character “\”.

Execute a Python script

In the following example we will write a variation of the mandatory “Hello World” script:

>>> print "It's easy to write a Python script!"
It's easy to write a Python script!
>>> 

The interactive interpreter is great for checking small bits of code, but if we have to write a serious program or script, we need to save our script in a file.

Let’s save our mini program in a source file. To save and edit programs in a file we need an editor. There are lots of editors, but you should choose one, which supports syntax highlighting and indentation. Under Linux you can use vi, vim, emacs, geany, gedit and umpteen others.
So, after you have chosen your editor, you can input your mini script and save it as easy_to_write.py.
The suffix .py is not really necessary under Linux but it’s good style to use it. But it is essential, if you want to write modules. Continue reading Execute a Python script

Using the Python Interpreter

With the Python interactive interpreter it is easy to check Python commands. The Python interpreter can be invoked by typing the command “python” without any parameter followed by the “return” key at the shell prompt:

python

Python comes back with the following information:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Once the Python interpreter is started, you can issue any command at the command prompt “>>>”.
The first thing we will do is write the mandatory “Hello World” statement: Continue reading Using the Python Interpreter

Python Overview

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

  • Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
  • Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
  • Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
  • Python is a Beginner’s Language: Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games. Continue reading Python Overview

[PYTHON] Install PyQt5 untuk Pyhton3.4 pada Ubuntu 14.04

Bismillah,

  1. Unduh PyQt5 dan SIP

    https://downloads.sourceforge.net/project/pyqt/PyQt5/PyQt-5.4.1/PyQt-gpl-5.4.1.tar.gz?r=http%3A%2F%2Fwww.riverbankcomputing.com%2Fsoftware%2Fpyqt%2Fdownload5&ts=1433118831&use_mirror=softlayer-sng

    https://downloads.sourceforge.net/project/pyqt/sip/sip-4.16.7/sip-4.16.7.tar.gz?r=http%3A%2F%2Fwww.riverbankcomputing.com%2Fsoftware%2Fsip%2Fdownload&ts=1433119078&use_mirror=softlayer-sng

  2. Install python3.4, python3.4-dev, idle-python3.4, qt5-default, checkinstall
    sudo apt-get install python3.4 python3.4-dev qt5-default idle-python3.4 checkinstall

  3. Edit .bashrc untuk menjadikan python3.4 sebagai default (secara alias)
    sudo nano ~.bashrc

    tambahkan:
    alias python=python3.4
  4. Ekstrak SIP hasil unduhan, masuk folder SIP hasil ekstrak. Pada folder tersebut jalankan perintah via terminal:
    python configure.py
    make
    sudo checkinstall

  5. Ekstrak PyQt5 hasil unduhan, masuk folder PyQt5 hasil ekstrak. Pada folder tersebut jalankan perintah via terminal:
    python configure.py
    make
    sudo checkinstall

    6. Jika sudah selesai step ke 5, selanjutnya check apakah sudah berhasil. Buka idle-python3.4 kemudian ketik:
    import PyQt5

    tekan enter
    jika tidak muncul error berarti sukses.

    Semoga bermanfaat

[Python] Bab 1 Perkenalan

Python merupakan bahasa pemrograman yang mudah dipelajari, dan powerfull. Python mempunyai struktur data tingkat tinggi yang efisien, sederhana dan efektif untuk “object oriented programming”. Bahasa pemrograman python cenderung fokus pada solusi dari masalah daripada sintaks (syntax) dan struktur pemrogramanan.

1.1 Fitur-fitur Python

  • Sederhana
  • Mudah dipelajari
  • Free and opensource
  • Bahasa tingkat tinggi
  • Portable
  • Interpreted
  • Orientasi object
  • Extensible
  • Embeddable
  • Extensive Libraries