Wednesday, June 19, 2019

elements access using index in python

mylist = ["black", "red", "orange"]
print( mylist[0])
print( mylist[1])
print( mylist[2])

output:


len(length) function in python

The number of elements can be determined with the len (length) function:

a=[0,1,2]

print(len(a))

output:



example 2:


a=[0,1,2,3,4]

print(len(a))

output:




list arrays in python

nameslist = ["Sam", "Lisy", "Pit"]
numberslist = [1, 2, 3.14]
mixedlist = ["ham", 'eggs', 3.14, 5]

and append function in python

a=[0,1,2]
print(a)
a.append(5)
a.append( "Zapzoo")
print(a)

output:



empty array is created like this
x=[]

array with zero initialisation values

y= [0]*10 # array of integers with 10 zero elements
z = [0.0]*20 # array of floats with 20 zero elements




Tuple in python


Tuple is similar to list but
list is indicate in '[]'square brackets and tuples indicates in "()" parenthesis(optional)
another difference is list in mutable
tuple is immutable

empty tuple
 t=()


example 1:
list=[10,20,30,40]
tuple=(10,20,30,40)
print(list)
print(tuple)


output:
[10, 20, 30, 40]
(10, 20, 30, 40)

example 2:

list=[10,20,30,40]
tuple=(10,20,30,40)
tuple2=10,20,30,40

print(list)
print(tuple)
print(type(tuple2))
print(tuple2)

output:
[10, 20, 30, 40]
(10, 20, 30, 40)
<class 'tuple'>
(10, 20, 30, 40)


example 3:

(x,y) = (5, 3)
coordinates = (x,y)
print(coordinates)

dimensions = (8, 5.0, 3.14)
print(dimensions)
print(dimensions[0])
print(dimensions[1])
print(dimensions[2])

output::
(5, 3)
(8, 5.0, 3.14)
8
5.0
3.14



if elif and else loop in python

s = input ("Input your name: ")
if s == "Tom":
    print("Hello ", s)
elif s == "Carmen":
    print("I'm so glad to see you ", s)
elif s == "Sonia":
    print("I didn't expect you ",s)
else:
    print("Hello unknown")


output::

if and else loops in python

s = input ("Input your name: ")
if s == "Tom":
    print("Hello ", s)
else:
    print("Hello unknown")

output:


note:

after if and else statements ":" is mandatory ,other wise compiler gives an error

if loop in python

s = input ("Input your name:")
if s == "Tom":
    print("HELLO ", s)


output::

note:after "if " statement ":" is mandatory other wise ,compiler through the error

while test in python

i = 0
while i<= 5:
    print("*");
    i = i + 1;


output:





while loop in python

i = 0
while i<= 20:
    print(i, "\t" ,i*i)
    i = i + 1
print("READY!\n")

Taking input from user using python

Example 1:

s = input("What is your name?\n");
print("HELLO ", s);

output:
What is your name?
anil
HELLO  anil



Example 2:

x = int(input("Input an integer: "))
y = float(input("Input a float: "))
print(x, y)

output:

Input an integer: 2
Input a float: 2.5

2 2.5

reading hex values from char array

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned char start[2]={0x01,0x02,0x00};
    printf("start[0]=%x\n",start[0]);
     printf("start[1]=%x\n",start[1]);
       // printf("start[2]=%x\n",start[2]);
    return 0;
}

python class topic video