0% found this document useful (0 votes)
2 views12 pages

Chapter 1 and 2 Cs

The document provides a comprehensive overview of Python programming concepts, including dynamic vs static typing, data types such as lists and strings, and error types like syntax and runtime errors. It also covers control flow structures like for and while loops, as well as various list and string methods. Key differences between mutable and immutable data types, along with conversion methods and tuple operations, are also highlighted.

Uploaded by

Usha A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Chapter 1 and 2 Cs

The document provides a comprehensive overview of Python programming concepts, including dynamic vs static typing, data types such as lists and strings, and error types like syntax and runtime errors. It also covers control flow structures like for and while loops, as well as various list and string methods. Key differences between mutable and immutable data types, along with conversion methods and tuple operations, are also highlighted.

Uploaded by

Usha A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON REVISION

TOUR 1 & 2
DYNAMIC TYPING STATIC TYPING

The datatype is assigned to the The datatype is not assigned to the


variable automatically. variable automatically.

Human interference is needed to


No human interference is needed.
assign the datatype of the variable.
a = 5, python interpreter
In Java, C++, the programmer has to
automatically assigns the datatype
declare datatype.
as integer.

print() input()
Used to give output Used to get input
print ("Hello") a = input("Enter data: ")

2
LIST STRING
A collection of ordered, mutable A sequence of characters enclosed in
elements in Python. quotes.
Can store multiple data types and
Immutable (cannot be changed once
allows modifications (add, remove,
created).
update).
[1,2,3,4,5] "Hello world"

Syntax Error Runtime Error


Errors due to incorrect syntax in Errors that occur while the program is
the code. running.
Before execution (during During execution (when the program
compilation or interpretation). is running).
print("Hello) (Missing closing quote) x = 10 / 0 (Division by zero error)

3
Mutable Datatype Immutable Datatype
The value stored in the variable can The value stored in the variable cannot
be altered without changing the be altered without changing the
memory address of memory address of
the variable the variable
list, dictionary (partially mutable) String

Syntax Error Logical error


Occurs when the syntax of the Occurs when he logic applied to solve
language is not obeyed the given problem is wrong
No output is given, error is raised Wrong output is given
Area = 3.14*r*2 instead of Area =
PRINT("hello")
3.14*r**2

4
IMPLICIT CONVERSION EXPLICIT CONVERSION

Automatically converts one data Manually converts one data type to


type to another. another using type casting.
No data loss occurs Data loss may occur.

num = 10 # int f = 10.5 # float


f = num + 2.5 # int is automatically num = int(f) # float is explicitly
converted to float converted to int
print(f) print(num)
>>> 12.5 >>> 10

5
for loop while loop
Counting loop Conditional loop
Ends when the count is over Ends when the condition is met
for i in range(2): a=1
print (i) while a < 10:
print (a)
a += 1

break continue
Used to terminate the loop when a Skips the current iteration and moves
certain condition is met. to the next iteration.
Ends the loop Does not end the loop
if a == 10: if a == 10:
break continue

6
== is
Checks if values of two objects are Checks if two objects refer to the same
equal. memory location.
Compares the content (value) of Compares the memory address (identity)
objects. of objects.
a = [1, 2, 3]; b = [1, 2, 3]; print(a == b) print(a is b)
>>> True >>> False (Different memory locations)

extend() append()
Adds a single element (as a whole) to Adds multiple elements from an iterable
the list. to the list.
Inserts the element as a single item. Unpacks and adds individual elements.
lst = [1, 2]; lst.append([3, 4]); print(lst) lst = [1, 2]; lst.extend([3, 4]); print(lst)
>>> [1, 2, [3, 4]] >>> [1, 2, 3, 4]

7
find() count()
Returns the index of the first Returns number of occurence of a
occurrence of a substring. substring
Returns -1 if the substring is not
found. Returns 0 if the substring is not found.
string.find(substring, start, end) string.count(substring, start, end)

count() index()
Returns the number of occurrences Returns the index of the first
of a substring or element. occurrence of a substring or element.
Returns 0 if the substring or Raises value error if substring is not
element is not found found
string.count(substring, start, end) string.index(substring, start, end)

8
split() partition()
Splits the string into three parts: before
Splits the string in list from the first
the separator, the separator itself, and
occurence of the substring given
after the separator.
Requires a separator; if not found,
If no separator is provided, splits at
returns the original string and two
whitespace by default.
empty strings.
string.split(separator, maxsplit) string.partition(separator)
s = "apple,orange,banana" s = "apple,orange,banana"
print(s.split(",")) print(s.partition(","))
>>> ['apple', 'orange', 'banana'] >>> ('apple', ',', 'orange,banana')

9
pop() remove()
Removes and returns the element at
removes the first occurence of the Value
the specified index
Removes last element if the index is not
Raises ValueError if value is not given
given
Index is given as parameter Value is given as parameter
list.pop(index) list.remove(value)

packing in tuples unpacking in tuples


Separates the values of a tuple into
Groups values into a single tuple.
individual variables.
my_tuple = (1, 2, 3)
my_tuple = (1, 2, 3) # Packing
a, b, c = my_tuple # Unpacking
print(my_tuple)
print(a, b, c)
>>> (1, 2, 3)
>>> 1 2 3
10
pop() popitem() in dictionary

Removes a specific key-value pair Removes and returns the last inserted
using the key. key-value pair.
Requires a key as an argument. No arguments required.

d = {"a": 1, "b": 2, "c": 3} d = {"a": 1, "b": 2, "c": 3}


d.pop("b") d.popitem()
print(d) print(d)
>>> {'a': 1, 'c': 3} >>> {'a': 1, 'b': 2} (last item removed)

11
sort() sorted()

Sorts the list in-place (modifies the Returns a new sorted list without
original list). changing the original.

Works only on lists. Works on any iterable (lists, tuples,


strings, etc.).
lst = [3, 1, 2]
lst = [3, 1, 2]
new_lst = sorted(lst)
lst.sort()
print(new_lst)
print(lst)
>>> [1, 2, 3] (original remains
>>> [1, 2, 3] (modified)
unchanged)

12

You might also like