0% found this document useful (0 votes)
17 views31 pages

Lecture 2

Uploaded by

Uzma Mohammed
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)
17 views31 pages

Lecture 2

Uploaded by

Uzma Mohammed
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

Lecture 2: Programming Concepts (Python)

Md. Shahriar Hussain


ECE Department, NSU

North South University CSE465 Md. Shahriar Hussain


Syntax of Python

• Printing Output

North South University CSE465 Md. Shahriar Hussain 2


Basic Data Types

• Numeric Data Types:

North South University CSE465 Md. Shahriar Hussain 3


Basic Data Types

• Sring Data Type

North South University CSE465 Md. Shahriar Hussain 4


String

String1 = “Hello"
print("Initial String: ") Output:
print(String1)
Initial String:
# Printing First character Hello
print("\nFirst character of String First character of String is:
is: ")
H
print(String1[0])
Last character of String is:
# Printing Last character o
print("\nLast character of String
is: ")
print(String1[-1])

North South University CSE465 Md. Shahriar Hussain 5


String Slicing
# Creating a String
String1 = "Hello World"
print("Initial String: ") Output:
print(String1)
Initial String:
# Printing 3rd to 6th character HelloWorld
print("\nSlicing characters from 3-6: ") Slicing characters from 3-6:
print(String1[3:7]) loWo
# Printing characters between
Slicing characters between 3rd
# 3rd and 2nd last character and 2nd last character:
print("\nSlicing characters between " + loWorl
"3rd and 2nd last character: ")
print(String1[3:-2])

North South University CSE465 Md. Shahriar Hussain 6


Reversing a String

#Program to reverse a string


str = “HelloWorld" Output:
print(str[::-1]) dlroWolleH

North South University CSE465 Md. Shahriar Hussain 7


Deleting a Character

# Python Program to Delete


# characters from a String

String1 = "HelloWorld" Output:


String2 = String1[0:4] + HellWorld
String1[5:]
print("\nDeleting character at
4th Index: ")
print(String2)

North South University CSE465 Md. Shahriar Hussain 8


Lists

North South University CSE465 Md. Shahriar Hussain 9


Lists

North South University CSE465 Md. Shahriar Hussain 10


Tuples
• Tuple is a collection of Python objects much like a list. The sequence of values stored in a
tuple can be of any type, and they are indexed by integers

# Creating a Tuple with Mixed Datatype


Tuple1 = (5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ") Output:
print(Tuple1)
Tuple with Mixed Datatypes:
# Creating a Tuple with nested tuples (5, 'Welcome', 7, 'Geeks')
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2) Tuple with nested tuples:
print("\nTuple with nested tuples: ")
print(Tuple3) ((0, 1, 2, 3), ('python', 'geek'))
# Creating a Tuple
# with repetition Tuple with repetition:
Tuple1 = (‘Hi',) * 3 (‘Hi', ‘Hi', ‘Hi')
print("\nTuple with repetition: ")
print(Tuple1)

North South University CSE465 Md. Shahriar Hussain 11


Dictionaries

• Dictionaries in Python is a data structure, used to store values in key:value


format.

North South University CSE465 Md. Shahriar Hussain 12


Dictionaries

North South University CSE465 Md. Shahriar Hussain 13


Dictionaries

• Dictionaries provide several methods and operations to work with their


elements. Some common methods include keys(), values(), and items()

North South University CSE465 Md. Shahriar Hussain 14


Conditional Statements (if, else, elif)

North South University CSE465 Md. Shahriar Hussain 15


Functions

North South University CSE465 Md. Shahriar Hussain 16


Functions

• Return Statement:

North South University CSE465 Md. Shahriar Hussain 17


Loops

North South University CSE465 Md. Shahriar Hussain 18


Loops

• In Python, the range() function generates a sequence of numbers. When


specifying a range, the end value is excluded. This design choice is made to
align with the zero-based indexing used in Python and many other
programming languages.

North South University CSE465 Md. Shahriar Hussain 19


Loops
• The range() function follows the syntax range(start, stop, step), where start is the
starting value (inclusive), stop is the ending value (exclusive), and step is the step
size between values

North South University CSE465 Md. Shahriar Hussain 20


Function and Loop

• Variable Number of Arguments: Functions can accept a variable number of


arguments using special syntax. The *args parameter allows you to pass
multiple arguments as a tuple.

North South University CSE465 Md. Shahriar Hussain 21


While Loop

North South University CSE465 Md. Shahriar Hussain 22


Input From User

North South University CSE465 Md. Shahriar Hussain 23


Enumerate()

• The enumerate() function allows you to iterate over a sequence while


simultaneously accessing both the index and value of each element.

North South University CSE465 Md. Shahriar Hussain 24


items()

• Iterating Over a Dictionary: When iterating over a dictionary, you can use
the items() method to access both the keys and values of each item

North South University CSE465 Md. Shahriar Hussain 25


Loop and List

• Generating a list of squares

North South University CSE465 Md. Shahriar Hussain 26


Example
• Creating a list of uppercase letters from a string

North South University CSE465 Md. Shahriar Hussain 27


Classes and Object (OOP)

• In Python, the init method and the self parameter are used in classes to initialize the
object's attributes and enable proper attribute access within the class.
• __int__ method: The init method is a special method in Python classes that gets
called automatically when an object of the class is created. It is used to initialize the
attributes of the object.

North South University CSE465 Md. Shahriar Hussain 28


Classes and Object (OOP)

• the init method takes two parameters: self (which represents the instance of
the object being created) and the additional parameters brand and model
• self parameter: The self parameter is a convention in Python that represents
the instance of the object itself. It allows you to access and manipulate the
object's attributes and methods within the class.

North South University CSE465 Md. Shahriar Hussain 29


Inheritance

• Inheritance allows
creating a new class
(child class) from an
existing class (parent
class), inheriting its
attributes and methods.

• The child class can


extend the functionality
of the parent class or
override its methods

North South University CSE465 Md. Shahriar Hussain 30


Polymorphism

• Polymorphism
allows objects of
different classes to
respond to the
same method call
in different ways.

North South University CSE465 Md. Shahriar Hussain 31

You might also like