In this material you will learn about variables and data types in Python Program. Make sure before you already know how to create a script or program in Python programs.
Variables and Data Type Definition: A variable is a place to store data, whereas the data type is the type of data stored in a variable.
Variables and Data Types
Variables are mutable, which means that its value can fluctuate.
Creating Variables in Python Program
Variables in Python programs can be created with the following format:
variable_name = <value>
variabel_me= "The contents of the variable"
Then to see the contents of a variable, you can use the print function.
print variabel_me print variabelsecond
Writing Rules Variables
The variable name may not begin to use letters or an underscore (_), for example: name, _name, my name, variable_name.
Subsequent characters can be letters, underscores (_) or number, for example: __name, n2, value1.
Characters on sensitive variable name ( case-sensitive ). This means uppercase and lowercase letters are distinguished. For example, variabel_Me and variabel_me , both are different variables.
The variable name can not use keywords that are already in a Python program like if, while, for, and so on.
When a variable is no longer needed, then you can remove the function del ().
>>> name = "fajaryusuf.com" >>> print name fajaryusuf.com >>> del(name) >>> print name Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined >>>
At last command, you will get NameError. This means that the variable is not in memory alias has been deleted.
This type of data
How to fill the variable's value is determined by the type of data, eg for data type text (string), then it must be enclosed in double quotes ( "..."). As for the number (integer) and boolean does not need to be enclosed in quotation marks.
name_me = "Fajaryusuf.com" age = 25 height = 170.14
Python programs would be to automatically recognize the type of data or types of data stored in a variable.
To check the data type of a variable, you can use the function type ().
>>> age= 25 >>> type(age) <type 'int'> >>> age= "25" >>> type(age) <type 'str'> >>> age= '25' >>> type(age) <type 'str'> >>> age= 25.5 >>> type(age) <type 'float'> >>> age= true Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined >>> age= True >>> type(age) <type 'bool'>
Types of Data Types
In general, primitive data types in Python program is divided into three types:
Text type data
Boolean type data
Let's discuss them one by one ...
1.) Numeric Data Types
Number data types are divided into several types anymore:
int (integer): integer, example : 32, 22, 12, 10, etc. float: fractions, for example 1.3, 4.2, 22.3, etc.
price = 12000 #tipe int weight = 23:12 #float distance = 3E3 #float 3000.0, the letter E means exponent 10
2.) Data Type Text
Text data type again divided into two types:
Char: character, example : 'F'. String: set of characters, example "I no longer eat".
Writing text data type must be enclosed in quotation marks. Can use single quotes ( '...'), double quotes ( "..."), and three ( '' '...' '' or "" "..." "").
Boolean data type is a data type that has only two values: True and False or 0 and 1.
Writing True and False, its first letter should be capitalized and without quotation marks.
move = True flame = 1 #acctualy the type int, but could also be a bool
Sample Program Using Variables and Data Types
Here is a simple example of the application of a variable in the program:
# Python bio population data program
# creates variables and their values
name = "Fajar Yusuf"
address = 'Depok'
age = 25
height = 170.5
married = False
# print variable contents
print "Name:", name
print "Address:", address
print "Age:", age
print "Height:", high
print "Status: married"
print "Status: not married"
Converting Data Types
Although Python program has been automatically detect the type of data stored in a variable, but there are times when you need to convert data types.
For example, in the following examples:
a = 10 b = 3 c = a / b
print c #output: 3
The division of the value of a and b produces 3 (integer). Why is that?
Because the value of a and b integer, then the result would be an integer.
How to keep the results are there coma?
Of course you have to change the type of variables a and b into fractions (float) first, and only after it is divided.
as follows :
a = 10 b = 3 c = float(a) / float(b) #output: 3.3333333333333335 print c
Float function () will change the values of a and b being 10.0 to 3.0.
SITUS INI ADALAH SITUS PEMBELAJARAN PEMROGRAMAN DAN JUGA PEMAHAMAN TERHADAP TEKNOLOGI KOMPUTER. KALIAN BISA MENGIKUTI PEMBELAJARAN PEMROGRAMAN DENGAN BERTAHAP PADA MATERI YANG SUDAH SAYA SEDIAKAN, JIKA ADA MATERI YANG MEMBINGUNGKAN SILAKAN LAKUKAN KOMENTAR PADA MATERI YANG ANDA TANYAKAN ATAU KALIAN BISA MENGHUBUNGI SAYA DIHALAMAN KONTAK.