Let us Review Python a bit and look at List & Lambda Function!
Python is the easiest popular programming language. Anything and everything you can do with python! Everyone knows about Google. Google uses Python extensively.
We learn some fundamentals by examples.
Ex.1 Print
Code | result |
print("Hello, Friends!") | Hello, Friends! |
Types and functions
Python automatically recognizes the types of data. Here are a few examples:
Character Strings: ‘ODL’, ‘AMET’
List of Objects: [10, 20, 30, 40], [‘Male’, ‘Female’]
In addition, Python has special datum meaning nothing: None
Python is rich in libraries, built-in-function. Now let us learn about variables
Ex.2 Add two constants
Code | Result |
print(5+5) | 10 |
Ex. 3 Add variables
Code | Result |
X,Y = 5,5 | |
print(X+Y) | 10 |
X,Y = 5,’FIVE’ print(X+Y) | SyntaxError: invalid character '’' (U+2019) |
Note multiple assignments in a single statement
Ex. 4 Add string variables
Code | Result |
X,Y = ‘5’,’5’ | |
print(X+Y) | 55 |
X,Y = ‘AMET’,’UNIVERSITY’ |
|
print(X+Y) | AMET University |
Let us print some useful Multiplication Table
Ex 5. For loop with range -
Multiplication Table for 5: Here we use for loop with simple syntax as shown:
Code | Result |
for x in range(1,11): print(x*5) | 5 |
for x in range(1,11,2): print(x*5) #Note range function (start,stop,step) | 1 |
for x in range(1,10): txt = '{} * {} = {}' print(txt.format(x,5,x*5)) #note simple formatting | 1 * 5 = 05 |
For repetitive steps, we used for loop. See the syntax range. Note range (start, stop, step)
The start is inclusive and the stop is excluded with the increment step.
Ex 6. For Loop iteration
code | result |
for x in "AMET": print(x) | A |
for x in ['marine’, ‘nautical science']: print(x) | marine |
Let us check how PVM (Python Virtual Machine) handles internally through some simulator Click This Link Python Tutor - Visualize Python and paste the above code in the space given and see the steps forward. |
|
Ex 7. Introspection to Python variable types
#Ex 7 basic and collection variables
var_i = 5
var_f = 5.0
var_str = "AMET"
print(type(var_i))
print(type(var_f))
print(type(var_str))
print(25*'-','Collection Variables')
# Numeric collection variables
var_list = [1,2,3]
var_tuple = ("one", "two", "three")
var_set = {1,2,3}
var_dict = {"1":"one", "2": "Two", "3":"Three"}
print(type(var_list))
print(type(var_tuple))
print(type(var_set))
print(type(var_dict))
#
<class 'int'>
<class 'float'>
<class 'str'>
------------------------- Collection Variables
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
No comments:
Post a Comment