Variables and types

A variable is a name that represents a value stored in the computer's memory

In [1]:
print("Hello World!")
Hello World!
In [2]:
# string data type
type("Hello World!")
Out[2]:
str
In [3]:
# integer data type
type(28)
Out[3]:
int
In [4]:
# floating point data type
type(5.5)
Out[4]:
float
In [1]:
# string
type('this is string')
Out[1]:
str
In [2]:
type("this is also' string")
print("this is also' string")
this is also' string
In [3]:
type("""this is again string""")
Out[3]:
str
In [4]:
1000000
Out[4]:
1000000
In [5]:
# variable
message = "What's up?"
n = 17
pi = 3.14159
In [6]:
print(pi)
3.14159
In [7]:
17 = n # this is an invalid assignment statement. The lefthand side must be a variable.
  File "<ipython-input-7-1751d37505e8>", line 1
    17 = n # this is an invalid assignment statement. The lefthand side must be a variable.
                                                                                           ^
SyntaxError: can't assign to literal
In [8]:
n = 18
print(n)
18
In [10]:
class = "Computer Programming SCMA241" # Python keywords cannot be used as a variable name.
  File "<ipython-input-10-c8e1bb021909>", line 1
    class = "Computer Programming SCMA241" # Python keywords cannot be used as a variable name.
          ^
SyntaxError: invalid syntax
In [11]:
# here is a list of python keywords
import keyword 
keyword.kwlist
Out[11]:
['False',
 'None',
 'True',
 'and',
 'as',
 'assert',
 'break',
 'class',
 'continue',
 'def',
 'del',
 'elif',
 'else',
 'except',
 'finally',
 'for',
 'from',
 'global',
 'if',
 'import',
 'in',
 'is',
 'lambda',
 'nonlocal',
 'not',
 'or',
 'pass',
 'raise',
 'return',
 'try',
 'while',
 'with',
 'yield']

Expressions and statements

An expression is a combination of values, variables, operators, etc.

In [12]:
1+1 # the value of this expression is 2
Out[12]:
2
In [14]:
len("Hello") # this statement returns the length of the string "Hello"
Out[14]:
5
In [15]:
17
Out[15]:
17

Operators

  • addition +
  • subtraction -
  • multiplication *
  • division / (//)
  • exponentiation **
In [16]:
2**3
Out[16]:
8
In [32]:
3**2
Out[32]:
9
In [19]:
minutes = 645
hours = minutes // 60

print(hours)
10
In [20]:
7/4
Out[20]:
1.75
In [21]:
7//4
Out[21]:
1

Type converter functions : int(), float(), str()

  • int(x) : convert the value of x to integer
  • float(x) : convert the value of x to float
  • str(x) : convert the value of x to string

In [22]:
int(3.14)
Out[22]:
3
In [23]:
int(3.99999999)
Out[23]:
3
In [24]:
int(3.0)
Out[24]:
3
In [25]:
int(minutes / 60)
Out[25]:
10
In [26]:
type(int("2345"))
Out[26]:
int
In [27]:
int(17)
Out[27]:
17
In [29]:
int("abc 123") # the value "abc 123" cannot be converted to integer!
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-753d8aa75243> in <module>()
----> 1 int("abc 123") # the value "abc 123" cannot be converted to integer!

ValueError: invalid literal for int() with base 10: 'abc 123'
In [30]:
float(27)
Out[30]:
27.0
In [31]:
float("123.45")
Out[31]:
123.45
In [32]:
str(17)
Out[32]:
'17'
In [33]:
str(123.45)
Out[33]:
'123.45'

Order of operations

In [36]:
2**3**2
Out[36]:
512
In [37]:
(2**3)**2
Out[37]:
64
In [38]:
(minutes * 100) / 60
Out[38]:
1075.0
In [39]:
# operation on string
message
Out[39]:
"What's up?"
In [40]:
message*3
Out[40]:
"What's up?What's up?What's up?"
In [41]:
# Input
age = int(input('Please enter your age: '))
Please enter your age: 18
In [42]:
type(age)
Out[42]:
int
In [43]:
# modulus
6%2
Out[43]:
0
In [44]:
# example : calculate the area of circle
# area = pi*r*r

r = float(input("What is you radius? "))
print("The area is ",3.14159*r*r)
What is you radius? 3
The area is  28.274309999999996
In [ ]: