Python Data Science Jobs & Interviews
20.4K subscribers
190 photos
4 videos
25 files
330 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
In Python, loops are essential for repeating code efficiently: for loops iterate over known sequences (like lists or ranges) when you know the number of iterations, while loops run based on a condition until it's false (ideal for unknown iteration counts or sentinel values), and nested loops handle multi-dimensional data by embedding one inside another—use break/continue for control, and comprehensions for concise alternatives in interviews.

# For loop: Use for fixed iterations over iterables (e.g., processing lists)
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterates each element
print(fruit) # Output: apple \n banana \n cherry

for i in range(3): # Numeric sequence (start=0, stop=3)
print(i) # Output: 0 \n 1 \n 2

# While loop: Use when iterations depend on a dynamic condition (e.g., user input, convergence)
count = 0
while count < 3: # Runs as long as condition is True
print(count)
count += 1 # Increment to avoid infinite loop! Output: 0 \n 1 \n 2

# Nested loops: Use for 2D data (e.g., matrices, grids); outer for rows, inner for columns
matrix = [[1, 2], [3, 4]]
for row in matrix: # Outer: each sublist
for num in row: # Inner: elements in row
print(num) # Output: 1 \n 2 \n 3 \n 4

# Control statements: break (exit loop), continue (skip iteration)
for i in range(5):
if i == 2:
continue # Skip 2
if i == 4:
break # Exit at 4
print(i) # Output: 0 \n 1 \n 3

# List comprehension: Concise for loop alternative (use for simple transformations/filtering)
squares = [x**2 for x in range(5) if x % 2 == 0] # Even squares
print(squares) # Output: [0, 4, 16]


#python #loops #forloop #whileloop #nestedloops #comprehensions #interviewtips #controlflow

👉 @DataScience4
In Python, loops are essential for repeating code efficiently: for loops iterate over known sequences (like lists or ranges) when you know the number of iterations, while loops run based on a condition until it's false (ideal for unknown iteration counts or sentinel values), and nested loops handle multi-dimensional data by embedding one inside another—use break/continue for control, and comprehensions for concise alternatives in interviews.

# For loop: Use for fixed iterations over iterables (e.g., processing lists)
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: # Iterates each element
print(fruit) # Output: apple \n banana \n cherry

for i in range(3): # Numeric sequence (start=0, stop=3)
print(i) # Output: 0 \n 1 \n 2

# While loop: Use when iterations depend on a dynamic condition (e.g., user input, convergence)
count = 0
while count < 3: # Runs as long as condition is True
print(count)
count += 1 # Increment to avoid infinite loop! Output: 0 \n 1 \n 2

# Nested loops: Use for 2D data (e.g., matrices, grids); outer for rows, inner for columns
matrix = [[1, 2], [3, 4]]
for row in matrix: # Outer: each sublist
for num in row: # Inner: elements in row
print(num) # Output: 1 \n 2 \n 3 \n 4

# Control statements: break (exit loop), continue (skip iteration)
for i in range(5):
if i == 2:
continue # Skip 2
if i == 4:
break # Exit at 4
print(i) # Output: 0 \n 1 \n 3

# List comprehension: Concise for loop alternative (use for simple transformations/filtering)
squares = [x**2 for x in range(5) if x % 2 == 0] # Even squares
print(squares) # Output: [0, 4, 16]


#python #loops #forloop #whileloop #nestedloops #comprehensions #interviewtips #controlflow

👉 https://t.iss.one/CodeProgrammer
2
💡 Python Conditionals: if, elif, and else

The if-elif-else structure allows your program to execute different code blocks based on a series of conditions. It evaluates them sequentially:

if: The first condition to check. If it's True, its code block runs, and the entire structure is exited.
elif: (short for "else if") If the preceding if (or elif) was False, this condition is checked. You can have multiple elif blocks.
else: This is an optional final block. Its code runs only if all preceding if and elif conditions were False.

This provides a clear and efficient way to handle multiple mutually exclusive scenarios.

# A program to categorize a number
number = 75

if number < 0:
category = "Negative"
elif number == 0:
category = "Zero"
elif 0 < number <= 50:
category = "Small Positive (1-50)"
elif 50 < number <= 100:
category = "Medium Positive (51-100)"
else:
category = "Large Positive (>100)"

print(f"The number {number} is in the category: {category}")
# Output: The number 75 is in the category: Medium Positive (51-100)


Code explanation: The script evaluates the variable number. It first checks if it's negative, then if it's zero. After that, it checks two positive ranges using elif. Since 75 is greater than 50 and less than or equal to 100, the condition 50 < number <= 100 is met, the category is set to "Medium Positive", and the final else block is skipped.

#Python #ControlFlow #IfStatement #PythonTips #ProgrammingLogic

━━━━━━━━━━━━━━━
By: @DataScienceQ
#13. auto
A keyword that lets the compiler automatically deduce the data type of a variable at compile-time.

#include <iostream>

int main() {
auto number = 10; // Compiler deduces int
auto pi = 3.14; // Compiler deduces double
std::cout << "Type of 'number' is deduced.";
return 0;
}

Type of 'number' is deduced.


#14. & (Address-of Operator)
Returns the memory address of a variable.

#include <iostream>

int main() {
int var = 20;
std::cout << "Memory address of var: " << &var;
return 0;
}

Memory address of var: 0x61ff08 
(Note: Address will vary)


#15. * (Dereference Operator)
Accesses the value stored at a memory address held by a pointer.

#include <iostream>

int main() {
int var = 50;
int* ptr = &var; // ptr holds the address of var
std::cout << "Value at address " << ptr << " is " << *ptr;
return 0;
}

Value at address 0x61ff04 is 50
(Note: Address will vary)

---
#CPP #ControlFlow #Conditional

#16. if
Executes a block of code if a specified condition is true.

#include <iostream>

int main() {
int age = 18;
if (age >= 18) {
std::cout << "You are an adult.";
}
return 0;
}

You are an adult.


#17. else
Executes a block of code if the condition in the if statement is false.

#include <iostream>

int main() {
int age = 16;
if (age >= 18) {
std::cout << "You are an adult.";
} else {
std::cout << "You are not an adult.";
}
return 0;
}

You are not an adult.


#18. else if
Specifies a new condition to test, if the first if condition is false.

#include <iostream>

int main() {
int score = 85;
if (score >= 90) {
std::cout << "Grade: A";
} else if (score >= 80) {
std::cout << "Grade: B";
} else {
std::cout << "Grade: C";
}
return 0;
}

Grade: B


#19. switch / case
Selects one of many code blocks to be executed.

#include <iostream>

int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
}
return 0;
}

Wednesday


#20. break
Used to exit a switch statement or a loop.

#include <iostream>

int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) {
break; // Exit the loop when i is 5
}
std::cout << i << " ";
}
return 0;
}

0 1 2 3 4

---
#CPP #Loops

#21. for
Executes a block of code a specified number of times.

#include <iostream>

int main() {
for (int i = 0; i < 5; ++i) {
std::cout << i << " ";
}
return 0;
}

0 1 2 3 4


#22. while
Loops through a block of code as long as a specified condition is true.

#include <iostream>

int main() {
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
return 0;
}

0 1 2 3 4
1