Python Lesson 1
Vocabulary
### Python Program Flow:
--> stdlibe (Standard Library)
- Programs --> Module(s) ->
--> imports
- Modules --> Statements
- Statements --> Expressions
- Expressions --> Objects
### Python is :
- Dinamiclly Typed
- Strongly Typed
>>> a = 2
>>> b = 'Tim'
>>> type (a)
<class 'int'>
>>> type (b)
<class 'str'>
>>> c = 3.14
>>> type (c)
<class 'float'>
### Create simple function
>>> def adder (x,y):
print(x + y)
>>> adder (2,2)
4
More Example:
>>> def oto (x,y):
print (x * y)
>>> oto (5,5)
25
>>> def tamta (x,y):
print (x / y)
>>> tamta (8,2)
4.0
>>> tamta (8, '2') (int and Str it will fail)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
tamta (8, '2')
File "<pyshell#36>", line 2, in tamta
print (x / y)
TypeError: unsupported operand type(s) for /: 'int' and 'str'
Note: If we make str + str it will work
>>> 'Tbil' + 'isi'
'Tbilisi'
### Python Core Data Types
- Immutable ( Number, String , Tuple )
- Mutable ( List , Dictionary , Files )
### Numbers
Precence / Obsence Dec. pt. (int = 0 , 1 , -2 and Float = 3,14 , 5,10 , -1,50)
- type () -> Verify data type
- isinstance () -> test for d.t (Bool)
- int () ; float() -> type conversion
operators : + , - , * , / , // , % , ** , < , > , == , != , AND , OR , NOT
#Order of operations : (PEMDAS)
P: Parenthesis
E: Exponentitation
M: Multiplication
D: Division
A: Addition
S: Subtraction
### Some Examples Of Calc
>>> 2+2
4
>>> 3-10
-7
>>> 10*10
100
>>> 100/10
10.0
>>> division = 100/10
>>> type (division)
<class 'float'>
>>> 100/6
16.666666666666668
>>> isinstance(division)
Traceback (most recent call last):
File "<pyshell#106>", line 1, in <module>
isinstance(division)
TypeError: isinstance expected 2 arguments, got 1
>>> help(isinstance)
Help on built-in function isinstance in module builtins:
isinstance(...)
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
>>> isinstance(division, int)
False
>>> isinstance(division, float)
True
>>> division
10.0
>>> int(division)
10
>>> division = int(division)
>>> division
10
>>> cbt = 'CBT Nuggets'
>>> type(cbt)
<class 'str'>
>>> cbt2 = int(cbt)
Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
cbt2 = int(cbt)
ValueError: invalid literal for int() with base 10: 'CBT Nuggets'
>>> numstring = '100'
>>> numconv = int(numstring)
>>> numconv
100
>>> c = 'cbt'
>>> n = "nuggets"
>>> n1 = 'cbt nuggets"
SyntaxError: EOL while scanning string literal
>>> n * 5
'nuggetsnuggetsnuggetsnuggetsnuggets'
>>> cnew = 'cbt '
>>> cnew * 5
'cbt cbt cbt cbt cbt '
>>> c + n
'cbtnuggets'
>>> c + ' ' n
SyntaxError: invalid syntax
>>> c + ' ' + n
'cbt nuggets'
>>> concat = c + ' ' + n
>>> concat
'cbt nuggets'
>>> len(concat) (Lenght, Number of carracters)
11
>>> corp = 'CBT Nuggets'
>>> corp[6:10]
'gget'
>>> corp[ :4]
'CBT '
>>> corp[4: ]
'Nuggets'
>>> corp[0:3]
'CBT'
### String Methods
- st.find() --> Gives offset ID
- st.replace() --> repl. substring
- st.split() --> cuts string @ delimiter outputs a list
- st.upper()
- st.lower()
>>> corp
'CBT Nuggets'
>>> corp.find(CBT)
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
corp.find(CBT)
NameError: name 'CBT' is not defined
>>> corp.find('CBT')
0
>>> corp.find('gets')
7
>>> corp.split(' ')
['CBT', 'Nuggets']
>>> slist = corp.split(' ')
>>> slist
['CBT', 'Nuggets']
>>> corp
'CBT Nuggets'
### First Simple Script to determine raised to the 8th.
ui = input("Enter a number: ")
print("The data type is originally", (type(ui)))
uconv = int(ui)
print("Now the d.t. is", (type(uconv)))
power = uconv ** 8
print(ui, "raised to the 8th power is: ", power)
! Run Script
Enter a number: 2
The data type is originally <class 'str'>
Now the d.t. is <class 'int'>
2 raised to the 8th power is: 256