0% found this document useful (0 votes)
21 views32 pages

Fall Semester 2023-24 Freshers - SWE1004 - ETH - AP2023243000025 - 2023-09-20 - Reference-Material-I

Swe freshers

Uploaded by

Kankshith
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)
21 views32 pages

Fall Semester 2023-24 Freshers - SWE1004 - ETH - AP2023243000025 - 2023-09-20 - Reference-Material-I

Swe freshers

Uploaded by

Kankshith
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/ 32

Problem

solving
concepts
Variable Naming Rules:
• Variable names can consist of letters, numbers, and
underscores.
• Variable names must start with a letter (a-z, A-Z) or an
underscore (_).
• Variable names are case-sensitive, so my_variable and
My_Variable would be considered different variables.
• Python has some reserved words (keywords) that cannot be
used as variable names because they have special meanings in
the language.
21-09-2023 Dr. Helen Sharmila A 2
DATA
TYPES

21-09-2023 Dr. Helen Sharmila A 3


DATA
TYPES
In programming, data types
are fundamental categories
that define the type of value a
variable can hold.

They determine how the data


is stored in memory, how it
can be manipulated, and what
operations can be performed
on it.

Different programming
languages provide various data
types to represent different kinds
of values, such as numbers,
characters, and more complex
structures.
9/21/2023 Dr. Helen Sharmila A 4
21-09-2023 Dr. Helen Sharmila A 5
21-09-2023 Dr. Helen Sharmila A 6
Integer (int)
This data type is used to represent whole
numbers.

Example:

age = 25
num_students = 150

21-09-2023 Dr. Helen Sharmila A 7


Floating-Point (float)
This data type is used to represent numbers
with decimal points.

Example:

pi = 3.14159
temperature = 98.6
21-09-2023 Dr. Helen Sharmila A 8
Boolean (bool)
Booleans represent either True or False
values, often used in logical operations.

Example:

is_raining = True
has_license = False
21-09-2023 Dr. Helen Sharmila A 9
List
A list is an ordered collection of items. Lists
can contain elements of different data types.

Example:

numbers = [1, 2, 3, 4, 5]
mixed_list = [10, "apple", 3.14, True]
21-09-2023 Dr. Helen Sharmila A 10
Dictionary
Dictionaries store key-value pairs, allowing
you to associate values with unique keys.

Example:

student = {"name": "Bob", "age": 22, "major":


"Computer Science"}
21-09-2023 Dr. Helen Sharmila A 11
Set:
Sets are unordered collections of unique
elements.

Example:

unique_numbers = {1, 2, 3, 4, 5}

21-09-2023 Dr. Helen Sharmila A 12


Problem solving
concepts
1. Exchanging the Values of Two Variables:

Introduction:

•Variable swapping is the act of exchanging the values held by two


variables.

•It's a foundational concept in computer science and finds applications


in algorithms like sorting.
a=4 b=6

Result a=6 b=4


Algorithm a b
Step 1: Start 4 6
Step 2: Declare a, b, c
Step 3: Enter two numbers a, b
Step 4: Set c=0 c
Step 5: c=a 0
Step 6: a=b
Step 7: b=c
Step 8: Print a, b
Step 9: Stop
Flowchart
Start

Declare a, b, c

Read a, b

c=a, a=b, b=c

Print a, b S top
a=5
b = 10
print("Before swapping: a =", a, ", b =", b)

# Swapping
temp = a
a=b
b = temp

print("After swapping: a =", a, ", b =", b)


Importance of Variable Swapping:
1.Sorting Algorithms: Variable swapping is a fundamental operation in many
sorting algorithms. For instance, in algorithms like Bubble Sort or Selection Sort,
we swap elements to place them in their correct positions.
2.Memory Efficient Algorithms: Some algorithms require the repositioning of
elements (not necessarily in the context of sorting). Swapping can achieve this
without the need for additional data structures, making the algorithm more memory
efficient.
3.Algorithm Simplification: Swapping can sometimes simplify the logic of an
algorithm or method. By changing the positions of certain elements or values, the
subsequent steps might become more straightforward.
4.Data Manipulation: In data processing tasks, there might be scenarios where the
positions of two data elements need to be exchanged for better organization or
further processing.
5. Permutations & Combinations: In algorithms that generate
permutations or combinations, swapping is used to produce
different arrangements of a set of values.

6. Value Rotation: In some situations, it's necessary to rotate


values between multiple variables. For instance, if you had three
variables a, b, and c, and you wanted the value of a to go to b, the
value of b to go to c, and the value of c to go to a, you'd use a series
of swaps.

7. Improving Readability: Sometimes, reordering variables can


make certain algorithms or operations more intuitive and easier to
understand. Swapping aids in achieving this reordering.
2. Counting:

• It means determining the number of elements


in a collection.

• Essential for tasks like computing sizes or


lengths.
Counting without using len():
We can use a loop to count elements.

numbers = [1, 2, 3, 4, 5,9,10]


count = 0
for n in numbers:
count += 1
print("Count of numbers:", count)
Counting with Conditions:
Example:
numbers = [1, 2, 3, 4, 5,9,10]

# Counting even numbers


even_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
print("Count of even numbers:", even_count)
3. Summation of a Set of Numbers:

•Summation is adding together numbers.

•Vital for a myriad of applications: from calculating


scores to financial analysis.
Mathematical Example:

For the set S={1,2,3,4,5}, the sum is obtained by adding


all the numbers together.

Mathematical Calculation:

The sum is 1+2+3+4+5=15.


Summation without using sum():

Example:

total = 0
for num in numbers:
total += num
print("Sum of numbers:", total)
Summation with Conditions:

Example:

# Sum of even numbers


even_sum = 0
for num in numbers:
if num % 2 == 0:
even_sum += num
print("Sum of even numbers:", even_sum)
4. Factorial Computation:

The factorial of a non-negative integer n is the product of all positive


integers less than or equal to n. It is denoted by n!.

Mathematical Example:

5!=5×4×3×2×1=120
number = int(input("Enter a number: "))
factorial_result = 1

for i in range(1, number + 1):


factorial_result *= i

print(f"Factorial of {number} is {factorial_result}")

The range() function returns a sequence of numbers, starting


from 0 by default, and increments by 1 (by default), and stops
before a specified number.
number = int(input("Enter a number: ")) # User inputs a number, let's
say 5

factorial_result = 1 # Initialize a variable to store the factorial result

for i in range(1, number + 1): # Loop from 1 to 5 (inclusive)


factorial_result *= i # Multiply factorial_result by each value of i

print(f"Factorial of {number} is {factorial_result}") # Print the result


Let's calculate the factorial of 5 step by step:

In the first iteration of the loop, i is 1, so factorial_result becomes 1 * 1 = 1.


In the second iteration, i is 2, so factorial_result becomes 1 * 2 = 2.
In the third iteration, i is 3, so factorial_result becomes 2 * 3 = 6.
In the fourth iteration, i is 4, so factorial_result becomes 6 * 4 = 24.
In the fifth and final iteration, i is 5, so factorial_result becomes 24 * 5 =
120.

Factorial of 5 is 120
THANK YOU

You might also like