The document provides an introduction and comparison of Python and C programming languages. Some key points:
- Python is an interpreted language while C needs compilation. Python makes program development faster.
- Variables, input/output, arrays, control structures like if/else, for loops work differently in Python compared to C.
- Python uses lists instead of arrays. Lists are mutable and support slicing.
- Strings are treated as character lists in Python.
- Functions are defined using def keyword in Python.
- The document also introduces sequences (strings, tuples, lists), dictionaries, and sets in Python - their usage and operations.
2. Python
â Python is an interepted language, which can save you considerable time
during program development because no compilation and linking is
necessary.
3. How to install Python
âą Download latest version of python and install it on any drive: say
D:python
âą Then follow the steps
âGot to Control Panel -> System -> Advanced system settings
âClick the Environment variables... button
âEdit PATH and append ;d:Python to the end
âClick OK.
âOpen command prompt type python and enter
5. Commenting
// comment single line
/* comment
multiple lines */
# comment single line
â â â comment
multiple lines â â â
C Python
6. Variables
//Declaring a variable
Int a=10;
Char c=âaâ;
Float f=1.12;
//Cannot assign multiple values
a=b=4 // Will result error
#No need of prior Declarations
a=10
c=âaâ
f=1.12
#Can assign multiple values simultaneously
x = y = z = 10
a, b, c = 1, 2, "john"
C Python
11. Lists in detail
âą Print(a[2:5]) # prints 15,16,65
âą Print(a[-6:-2]) # prints 14,15,16,65
âą Print(a[4:]) # prints 65,34,baabtra
âą Print(a[:2]) # prints 12,14,15
âą a[2] = a[2] + 23; # Lists are mutable,we can change individual items
âą a[0:2] = [1, 12] # We can replace a group of items together
âą a[0:2] = [] # We can remove items together
âą a[:] = [] # Clear the list
12. Lists in detail
âą a.append(25) # adds an element at the end of list
âą b =[55,66,77]
a.extend(b)
a=a+b;
âą a.insert(1,99) # Inserts 99 at position 1
âą a.pop(0) # pop elements at position 0
# Combines two lists
13. Strings
char a[]=âbaabtraâ; a= âbaabtraâ
b=âdoesnâtâ
C=âbaabtra âmentoring partnerââ
Strings are character lists, So can be used like any
other lists as we discussed earlier
print (a[0])
a.append(âmâ);
C Python
14. Strings in detail
âą String slicing
word=âhello baabtraâ
print(word[6:] # prints baabtra
word[: 6] # prints âhello â
word2= âgood morningâ + word[6:]
Print(word2) # prints âgood morning baabtraâ
15. Control structures
âą Conditional Control Structures
âą If
âą If else
âą Switch
âą Loops
âą For
âą While
âą Do while
Conditional Control Structures
âą If
âą If else
âą Switch
Loops
âą For
âą While
âąDo while
C Python
16. If else
int a;
Printf(âEnter the numberâ);
scanf(â%dâ,&a);
If(a>80)
Printf(âDistictionâ);
else if(a>60)
Printf(âFirst classâ);
else {
Printf(âPoor performancenâ);
Printf(âRepeat the examnâ); }
a=input(âEnter the numberâ)
if a>80 : print(âDistinctionâ)
elif a>60 : print(âFirst Classâ)
else :
print(âPoor performanceâ)
print(âRepeat the examâ)
C Python
18. For Loop
int i=0;
for(i=0;i<10;i++)
{
printf(â%dâ, i);
}
Itâs quite a bit untraditional . We need to
define a range on which loop has to iterate.
This can be done using
Range(10,20) // creating a list with
elements from 10 to 20
For i in range(10) :
print(i) //print numbers up to 10
a=[12,14,16,âbaabtraâ]
For i in a :
print(i) //prints 12,14,16,baabtra
C Python
19. Other Control structure statements
âą Break
Eg: If(a%2==0)
{
Print(âevenâ);
break;
}
âą Break
The break statement is allowed only inside a loop
body. When break executes, the loop terminates.
Eg: for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
C Python
20. Other Control structure statements
âą Continue
for(i=1;i<20;i++)
{
if(i%2==1)
Continue;
Print(â%d is evenâ,i);
}
âą Continue
The continue statement is allowed only inside a
loop body. When continue executes, the current
iteration of the loop body terminates, and
execution continues with the next iteration of the
loop
Eg:
For i in range(1,20)
If i%2==0:
continue
print(â%d is evenâ %(i))
C Python
21. Other Control structure statements
for(i=1;i<20;i++)
{
if(i%2==1)
{}
else
Print(â%d is evenâ,i);
}
âą pass
The pass statement, which performs no action,
can be used when you have nothing specific to do.
Eg:
if a<10:
print(âless than 10â)
elif x>20:
pass # nothing to be done in this case
Else:
print(âin between 10 and 20â)
C Python
22. Functions
Int findSum(int a,int b)
{
int c;
c=a+b;
return c
}
d=findSum(10,15);
def findSum(a,b) :
return a+b
sum=findSum(112,321)
print(sum)
C Python
23. Task
âą Write a simple python program which will have an array variable as below
âą a= [50,15,12,4,2]
âą Create 3 functions which will take the above array as argument and returns
the arithmetic output
âAdd() //Outputs 83
âSubstract() //Outputs 17
âMultiply() //Outputs 72000
24. That was the comparison !
So whatâs new in python?
26. Sequences
âą A sequence is an ordered container of items, indexed by non-
negative integers. Python provides built-in sequence types ,they
are:-
â Strings (plain and Unicode), // We already have discussed
â Tuples
â Lists // We already have discussed
28. Tuples
âą A tuple is an immutable ordered sequence of items which may be of different
types.
â (100,200,300) # Tuple with three items
â (3.14,) # Tuple with one item
â ( ) # Empty tuple
âą Immutable means we cant change the values of a tuple
âą A tuple with exactly two items is also often called a pair.
29. Operation on Tuples
tpl_laptop = ('acer','lenova','hp','TOSHIBA')
tpl_numbers = (10,250,10,21,10)
tpl_numbers.count(10) # prints 3
tpl_laptop.index('hp') # prints 2
30. Task
âą Create a python program that will accept two tuples as arguments
and return the difference between the tuple values,
32. Dictionaries
âą A dictionary is an arbitrary collection of objects indexed by nearly
arbitrary values called keys. They are mutable and, unlike
sequences, are unordered.
âEg :{ 'x':42, 'y':3.14, 'z':7 }
âdict([[1,2],[3,4]]) # similar to {1:2,3:4}
33. Operation on Dictionaries
âą dic={'a':1,'b':2,'c':3}
â len(dic) # returns 3
â del dic['a'] # removes element with key âaâ
â a in dic # returns âTrueâ .Bur
â dic.items() #Displays elements
â for i in dic.iterkeys():
... print i # Returns key
â for i in dic. itervalues():
... print i # Return values
34. Task
âą Write a python program with a dictionary variable with key as
English word and value as meaning of the word.
âą User should be able to give an input value which must be checked
whether exist inside the dictionary or not and if it is there print
the meaning of that word