Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
35 views9 pages

Class 02

The document is a tutorial on programming in Python, focusing on fundamental concepts such as data types, variables, arithmetic operations, string operations, and data structures like lists, tuples, sets, and dictionaries. It emphasizes the importance of Python for data science and provides practical examples to illustrate coding logic and syntax. Additionally, it covers comparison and logical operators used in Python for evaluating conditions.

Uploaded by

ahmedfaraz1102
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)
35 views9 pages

Class 02

The document is a tutorial on programming in Python, focusing on fundamental concepts such as data types, variables, arithmetic operations, string operations, and data structures like lists, tuples, sets, and dictionaries. It emphasizes the importance of Python for data science and provides practical examples to illustrate coding logic and syntax. Additionally, it covers comparison and logical operators used in Python for evaluating conditions.

Uploaded by

ahmedfaraz1102
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/ 9

10/03/2025, 10:02 Class02.

ipynb - Colab

Class 02

keyboard_arrow_down "Programming in Python".


Programming is one of the most fundamental skills you will need as a data scientist/analyst.

Among the numerous tools available for analyzing data, we choose Python as it is the most robust, has an intuitive syntax, and is a modern
and widely used language.

Here you will learn how to think and do coding for real life scenarios.

This portion will be mostly based on building a strong sense of logic using only the basics.

Below are the fundamental data types, data structures and operations.

# Python's print statements runs line by line

Unlike other languages Python program works on an Interpreter so to run a code all you have to do is type it out.

Python language runs line by line, so incase you want to update a piece of code all you have to do is update the line let's see how -

#Imaginary giraffe

print("O ")
print("| ")
print("|____")
print(" ||||")
print(" ||||")

O
|
|____
||||
||||

# Type Casting # checking the data type of the variable

keyboard_arrow_down Data types :

int(numerical)

float(decimals)

str(text) - immutable

bool

# Python variables are a container that can store any type of value and of any particular size.Unlike other languages such a
a = 1
b = 1.3
c = 'Mike'
d = True

keyboard_arrow_down Changing the of variables type is called typecasting

How to find the Data Type for a variables in Python

Here in the below piece of code you can see that we have assigned each variable with a particular value and use the type method we can
check the datatype of the variable

type(a)

int

type(b) # decimal

float

type(c)

str

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 1/9
10/03/2025, 10:02 Class02.ipynb - Colab
type(d)

bool

a = 1
bool(a)

True

str(a)

'1'

float(a)

1.0

# Arthemetic Operators

a = 6
b = 10
a+b

16

a-b

-4

a*b

60

a/b

0.6

x = 345345
y = 765
x//y #floor division , it takes only the integer part of the division

451

a%b # remainder or modulus

a**b # expenential or a to the power of b

60466176

# https://docs.python.org/3.7/reference/expressions.html?#arithmetic-conversions

print((4 * 5) - 9 + 6 / 7)

11.857142857142858

keyboard_arrow_down String Operations :

We should look at strings as a banner where multiple characters are linked together.

The string data is an immutable type which means it cannot be modified.

c = 'z'
d = 'Mike'
e = "John"

#Now when we want to include words with a single quote such as "didn't", "I'll".. we might end up with a error

print('I didn't know that today was a class')

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 2/9
10/03/2025, 10:02 Class02.ipynb - Colab
Cell In[26], line 1
print('I didn't know that today was a class')
^
SyntaxError: unterminated string literal (detected at line 1)

print("I didn't know that today was a class")


#You can also use " " double quotes instead of single quotes for strings

I didn't know that today was a class

#Now to solve this we can use an escape character also


print('I didn\'t know that today was a class')

I didn't know that today was a class

#Now that '\' escape character which we saw earlier can also be used for other stuff like adding a new line

print('Hi Bob \n\nHow are you?')

Hi Bob

How are you?

# string concatenation

dessert_type = input("What is your favourite dessert?")


flavour_type = input("What is your favourite flavour?")
print("You have ordered",flavour_type+ "-" +dessert_type)

What is your favourite dessert? Ice Cream


What is your favourite flavour? Vanilla
You have ordered Vanilla-Ice Cream

A = "Data"
B = "Analysis"
C = "Pandas"
print("{0} {1} using {2}".format(A,B,C))

Data Analysis using Pandas

# String indexing , manipulation , slicing

text = "I have not failed. I've just found 10,000 ways that does not work. - Thomas A Edison"

text

"I have not failed. I've just found 10,000 ways that does not work. - Thomas A Edison"

len(text) # len function helps in find the no of charaters in a variable

84

#### Slicing In Strings


text[-84] # negative indexing

'I'

text[0]

'I'

text[2]='t' # String data is immuatable(you can not change the charater or modify)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[60], line 1
----> 1 text[2]='t'

TypeError: 'str' object does not support item assignment

# String slicing [start:end:step]

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 3/9
10/03/2025, 10:02 Class02.ipynb - Colab

text[0:65] #fetching the part of the data from text variable

"I have not failed. I've just found 10,000 ways that does not work"

batch = '5 girls and 4 boys in a class'


len(batch)

29

girls = batch[:7]
girls

'5 girls'

boys = batch[12:18]
boys

'4 boys'

# Membership
string = "John! Did you attend the conference on Data Science?"
print('Lincoln'in string)

False

print('John'in string)

True

#Now there are a plenty of other string operations available in Python you could explore those using
help(str)

Help on class str in module builtins:

class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return bool(key in self).
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(...)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 4/9
10/03/2025, 10:02 Class02.ipynb - Colab
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| mod (self, value, /)

# https://docs.python.org/3.7/library/stdtypes.html#string-methods

keyboard_arrow_down Data Structures


List

Tuple

Set

Dictionary

keyboard_arrow_down List:
Lists are ordered collections of items, which can be of different data types.

Lists are mutable,meaning their elements can be changed after they are defined.

Syntax: They are defined using square brackets [ ].

# Defining a list
nums = [1,2,3,4,5]
# 0 1 2 3
# 01234
fruits = ['apple','pineapple','orange','banana']

# indexing / slicing in list data structure


# Accessing elements of a list
nums[0]

fruits[0]

'apple'

len(fruits)

fruits[0][3]

'l'

# you can modify the items or charaters in a list

fruits[0] = 'xyz'
fruits

['xyz', 'pineapple', 'orange', 'banana']

keyboard_arrow_down Tuple:

Tuples are ordered collections of items, which can be of different data types.

Tuples are immutable, meaning their elements cannot be changed once defined.

Syntax: They are defined using parentheses ().

# Defining a tuple
point = (10,20,30)
dimensions = (100,200,300)

point[0] # Accessing elements of a tuple

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 5/9
10/03/2025, 10:02 Class02.ipynb - Colab
10

dimensions[-1] # Accessing elements of a tuple

300

point[0] = (60) # its immutable and hence cannot change the charaters or items in it
point

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[98], line 1
----> 1 point[0] = (60)
2 point

TypeError: 'tuple' object does not support item assignment

keyboard_arrow_down Set:

A set is an unordered collection of unique elements.

It is iterable, mutable, and does not allow duplicate elements.

This makes sets unfit for indexing and slicing.

Syntax: Sets are created using curly braces {} or by using the set() constructor.

#Creating a set
set = {1,2,3,4,5}

# Set operations
set.add(6) # Adding elements to a set
set

{1, 2, 3, 4, 5, 6}

set.remove(3) # Removing elements from a set


set

{1, 2, 4, 5, 6}

set1 = {1,2,3}
set2 = {10,20,30}
set1.union(set2)

{1, 2, 3, 10, 20, 30}

set1.intersection(set2)

set()

set[0]

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[108], line 1
----> 1 set[0]

TypeError: 'set' object is not subscriptable

keyboard_arrow_down Dictionary:

A dictionary is a collection of key-value pairs.

It is unordered, mutable, and does not allow duplicate keys.

Key is like a index and it is always unique & immutable .

Values are the objects that contain information and its accessed using their keys.

Each key is followed by value separated by colon.

Values can be immutable , mutable and duplicates.

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 6/9
10/03/2025, 10:02 Class02.ipynb - Colab
Syntax: Dictionaries are created using curly braces {} and colon : to separate keys and values, or by using the dict()
constructor.

# Creating a dictionary
values = {'a':1 , 'b':30,'c':60}
print(values)

{'a': 1, 'b': 30, 'c': 60}

values['a'] # Accessing elements in a dictionary

values['b'] = 5 # Modifying elements in a dictionary


values

{'a': 1, 'b': 5, 'c': 60}

# Dictionary operations

del values['c'] # Removing elements from a dictionary


values

# Adding new key-value pairs to a dictionary


values['d'] = 4
values

{'a': 1, 'b': 5, 'c': 60, 'd': 4}

'a' in values

True

'c' in values

True

values.keys()

dict_keys(['a', 'b', 'c'])

values.values()

dict_values([1, 5, 60])

keyboard_arrow_down Comparison Operators :

Comparison operators in Python are used to compare values.

They return either True or False depending on whether the comparison is true or false.

Here are the comparison operators in Python along with examples:

# Equal to (==): Checks if two values are equal.


x = 5
y = 10
x == y

False

# Not equal to (!=): Checks if two values are not equal.


x != y

True

# Greater than (>): Checks if the left operand is greater than the right operand.
x > y

False

# Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
x >= y

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 7/9
10/03/2025, 10:02 Class02.ipynb - Colab
False

#Less than (<): Checks if the left operand is less than the right operand.
x < y

True

#Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
x <= y

True

These comparison operators can be used to compare numbers, strings, or any other comparable objects in Python, and they return a boolean
value (True or False) based on the comparison result.

keyboard_arrow_down Python, logical operators

are used to combine conditional statements. Here are the three logical operators in Python along with examples:

x < 10 and y > 5 #Returns True if both conditions on its left and right are True.

True

x > 6 or y > 5 # Returns True if at least one of the conditions on its left or right is True.

True

x > 6 and y > 5

False

x>6

False

not(x>6) # returns opposite of the statement


#Returns True if the condition after it is False, and vice versa.

True

These logical operators can be used to create more complex conditions by combining simpler conditions.

They are often used in if statements, while loops, and other control flow statements to control the flow of execution based on multiple
conditions.

keyboard_arrow_down F-String

F-strings, introduced in Python 3.6, provide a concise and convenient way to embed expressions within string literals. Here's an example
demonstrating how to use f-strings:

Using f-string to embed variables within a string

name = 'alice'
age = 30
message = f"My name is {name} and I am {age} years old"
print(message)

My name is alice and I am 30 years old

In this example: name and age are variables containing string and integer values respectively.

The f-string f"My name is {name} and I am {age} years old." allows embedding of variables directly within the string using curly braces {}.

When this f-string is evaluated, the expressions within curly braces are replaced with the values of the corresponding variables.

The resulting string is then stored in the variable message. Finally, the message variable is printed, resulting in the formatted string being
displayed.

# Data Structures : type casting in data structures

li t [1 2 3 4]
https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 8/9
10/03/2025, 10:02 Class02.ipynb - Colab
list = [1,2,3,4]
tuples = tuple(list)
tuples

(1, 2, 3, 4)

print(set)

[10, 20, 30]

del set

set_3 = [10, 20, 30]


lst = [1, 2, 2, 3, 4]
print(set(lst))

{1, 2, 3, 4}

print(list)

[1, 2, 3, 4]

del list

dict1 = {'a': 1, 'b': 30, 'c': 60}


list(dict1.keys()) # retuns only the keys

['a', 'b', 'c']

list(dict1.values()) # retuns only the values

[1, 30, 60]

list(dict1.items()) # returns both the keys and values of the dictionary

[('a', 1), ('b', 30), ('c', 60)]

Start coding or generate with AI.

https://colab.research.google.com/drive/1x481GWbKsZj61bfcRsZIVIuLF7jh1tlG#printMode=true 9/9

You might also like