Variables

Value – one of the basic things a program works with, like a letter or a number. Examples 1, 2, and 'Hello, World!'.
The previously given values belong to different types:
-          1,2 are integers,
-          'Hello, World!' is a string – it's so called because it contains a „string“ of letters. You and the interpreter can identify strings because they are enclosed in quotation marks. String is a sequence of characters.
Important:
The print statement also works for integers. We used python command to start the interpreter.
>>> print 4 
4 
>>>print 'Hello World!'
Hello World! 
If you're not sure what type a value has, the interpreter can tell you.
>>>type('Hello, World!')

>>>type(17)

As mentioned before, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating-point.
>>>type(4.2)


 Variables

In last lesson we've introduced values and type and now it's time to store them somewhere. Variables are one of the most powerful features of a programming language. A variable is a name that refers to a value.
Assignment statement creates new variables and gives them values. Using a variable you can store all types of values such as strings (str), integers(int) , float ...
>>>message = 'Hello, it's me!'
>>>n = 121 
>>>pi = 3.141592 
With previous assignment statements we've assigned string to a new variable called message, the integer 121 is assigned to variable n and with third assigned approximate value of pi to variable named pi.
Using print statements you can display a value of variable. For example:
>>>print message
Hello, it's me!
>>> print n
121
>>>print pi 
3.141592
Type statement can be used to see what the type of a variable is the type of the value it refers to.
>>>type(message)

>>>type(n)

>>>type(pi)

Nema komentara:

Objavi komentar