Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
197 photos
14 videos
56 files
240 links
Join us and let's tackle leet code questions together: improve your problem-solving skills
Preparing for coding interviews
learning new algorithms and data structures
connect with other coding enthusiasts
Download Telegram
# code challenge
Create a program that calculates the factorial of a given number using a for loop
👍61
for answering all of the question you can leave a comment and share the channel for your friends
2❤‍🔥1
which concept is not clear for you until know
🆒1
Here's an explanation of tuples, dictionaries, and sets in Python:

1. Tuples:
A tuple is an ordered, immutable collection of elements enclosed in parentheses (). Tuples are similar to lists, but the main difference is that tuples cannot be modified once created. They are commonly used to store related pieces of data together.

Example:
my_tuple = (1, 2, 3, "hello", True)


2. Dictionaries:
A dictionary is an unordered collection of key-value pairs enclosed in curly braces {}. Each key-value pair is separated by a colon (:), and the keys must be unique. Dictionaries are useful for storing and retrieving data based on a specific key.

Example:
my_dict = {"name": "John", "age": 25, "city": "New York"}


3. Sets:
A set is an unordered collection of unique elements enclosed in curly braces {}. Sets are useful when you want to store a collection of items without any duplicates. They also support mathematical set operations like union, intersection, and difference.

Example:
my_set = {1, 2, 3, 4, 5}


In addition to the basic operations, Python provides various built-in functions and methods to manipulate and work with tuples, dictionaries, and sets. It's important to note that dictionaries and sets are mutable, meaning you can modify their contents after creation, while tuples are immutable and cannot be changed once created.

I hope this explanation helps! Let me know if you have any further questions.
4👍41
Leetcode with dani pinned «Here's an explanation of tuples, dictionaries, and sets in Python: 1. Tuples: A tuple is an ordered, immutable collection of elements enclosed in parentheses (). Tuples are similar to lists, but the main difference is that tuples cannot be modified once created.…»
because of the next question
I'll explain both the while loop and the for loop in Python with examples.

1. While Loop:
A while loop repeatedly executes a block of code as long as a specified condition is true. It continues to execute the code until the condition becomes false. The general syntax of a while loop is as follows:

while condition:
    # code to be executed


Example:
count = 0
while count < 5:
    print("Count:", count)
    count += 1

In this example, the while loop will execute the code block as long as the condition count < 5 is true. It will print the value of count and increment it by 1 in each iteration. The loop will stop when count becomes 5.

Output:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4


2. For Loop:
A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) or any other iterable object. It executes a block of code for each item in the sequence. The general syntax of a for loop is as follows:

for item in sequence:
    # code to be executed


Example 1:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

In this example, the for loop iterates over each item in the fruits list. It assigns each item to the variable fruit and then executes the code block, which simply prints the value of fruit.

Output:
apple
banana
cherry


Example 2:
for i in range(5):
    print(i)

In this example, the for loop uses the range() function to generate a sequence of numbers from 0 to 4. It assigns each number to the variable i and then executes the code block, which prints the value of i.

Output:
0
1
2
3
4


Both while loops and for loops are powerful tools for automating repetitive tasks and iterating over sequences. The choice between them depends on the specific requirements of your program.
👍5😎1
What is module?
2
In Python, a module is like a special file that contains helpful code that we can use in our programs. It's like having a toolbox full of useful tools that we can use whenever we need them.

To create a module, we just need to make a new file with a .py ending. Inside this file, we can write functions, classes, or variables that we want to use in other programs.

To use a module in our program, we can import it using the import statement. This is like opening up our toolbox and taking out the tools we need.

For example, let's say we have a module called "math_operations.py" that has functions for doing math calculations. We can import this module in our program like this:


import math_operations

result = math_operations.add(5, 10)
print(result)


In this example, we imported the "math_operations" module and used its "add" function to add 5 and 10 together. The result is then printed out.

Modules are important because they help us keep our code organized and allow us to reuse code that we've written before. They make it easier for us to manage and maintain our programs.

Python also has many built-in modules that come with the language. These modules provide extra functionality, like doing math or working with dates and times. They're like extra tools that we can use whenever we need them.
👍4
In amharic
👍1
To create your own module in Python, you can follow these steps:

1. Create a new Python file with the desired functions, classes, or variables. Let's say you want to create a module called my_module with a function greet() that prints a greeting message.

      # my_module.py

   def greet(name):
       print(f"Hello, {name}!")
  


2. Save the file with a .py extension. In this case, save it as my_module.py.

3. Now, you can import and use the greet() function from the my_module module in another Python program.

      # main.py

   import my_module

   my_module.greet("Alice")
  


   When you run the main.py program, it will import the my_module module and call the greet() function, which will print the greeting message "Hello, Alice!".

Note: Make sure that the module file (my_module.py in this example) is in the same directory as the program that imports it. If the module file is in a different directory, you may need to specify the path or add the directory to the Python module search path.

By following these steps, you can create your own module in Python and use it in other programs to organize and reuse your code.
👍4🎉2
[Python Learning YouTube Playlist by former student]

Abel Gideon was one of our the former Pre-Engineering students who is currently studying Biomedical Engineering. Now, he communicated our office that he created a YouTube playlist focused on learning Python for beginners. Even if the level may not fit to you all, we beleve that this may assist many of our Pre-Engineering students who are taking the course "Computer Programming". If you need to check it, please click the link below, send him your your feedbacks, and encourage him to do more.

https://youtube.com/playlist?list=PLf1VjpIMOtxSi7m60Zf2q-vEvBdC0qI-D&si=yR6pGmo6opCStSiM

The PECC Office (@peccaait)
4🆒1
:object-oriented programming (OOP):
It is a programming paradigm based on the concept of "objects" that contain data (attributes) and code (methods).
Objects are instances of classes, which are like blueprints for creating objects. Classes define what attributes and methods an object has.
OOP aims to organize code and model real-world concepts by creating objects that represent those concepts. For example, a Car class with attributes like make, model, year, and methods like drive(), brake(), etc.
Encapsulation is a key principle - objects encapsulate their state and code, hiding the internal details from the outside. Access is through well-defined interfaces.
Inheritance allows new child classes to be defined that inherit attributes and methods from parent classes, promoting
👍5
some tips for learning object-oriented programming (OOP) in Python as a beginner with examples:

1. Understand the basic concepts:

- Classes - user defined blueprint consisting of attributes (data) and methods (functions)
- Objects - instances of a class created from the class blueprint
- Inheritance - creating a new class from an existing class
- Polymorphism - objects can take on different forms and behave in different ways
- Encapsulation - binding data and functions together in an object

2. Create a simple class:

python
class Vehicle:

    def
init(self, make, model):
        self.make = make
        self.model = model
   
    def drive(self):
        print("Driving", self.model)

car = Vehicle("Toyota", "Corolla")
car.drive()


3.
Use inheritance to create subclasses:

python
class ElectricVehicle(Vehicle):

    def
init(self, make, model, battery_size):
        super().
init(make, model)
        self.battery_size = battery_size

    def charge_battery(self):
        print("Charging battery...")

ev = ElectricVehicle("Tesla", "Model 3", 85)
ev.charge_battery()


4. Ove
rride parent methods:

python
class ElectricVehicle(Vehicle):

    def drive(self):
        print("Driving silently!")

ev = ElectricVehicle("Tesla", "Model 3", 85)
ev.drive()


5. Use encapsulation to restrict access:

python
class BankAccount:

    def
init(self, balance=0):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount

    def get_balance(self):
        return self.balance

account = BankAccount(100)


St
art with simple examples, and try building up classes of your own. This will help reinforce the concepts.

https://news.1rj.ru/str/zethioprograming
4👍2
# examples
object-oriented programming (OOP) in Python:

Class for a Bank Account:

python
class BankAccount:
    def init(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance
       
    def deposit(self, amount):
        self.balance += amount
       
    def withdraw(self, amount):
        if self.balance >= amount:
            self.balance -= amount
        else:
            print("Insufficient funds")

    def get_balance(self):
        return self.balance

account = BankAccount("1234", 500)
account.deposit(200)
print(account.get_balance()) # Prints 700
account.withdraw(800) # Prints "Insufficient funds"


Cl
ass for a Student:

python
class Student:
    def init(self, name, age, gpa):
        self.name = name
        self.age = age
        self.gpa = gpa

    def get_grade(self):
        if self.gpa >= 4.0:
            return "A"
        elif self.gpa >= 3.0:
            return "B"
        elif self.gpa >= 2.0:
            return "C"
        else:
            return "D"
           
student = Student("Jim", 19, 3.5)
print(student.get_grade()) # Prints "B"


Cl
ass for a Circle:

python
from math import pi

class Circle:
    def init(self, radius):
        self.radius = radius

    def area(self):
        return pi * (self.radius ** 2)

    def circumference(self):
        return 2 * pi * self.radius

circle = Circle(3)
print(circle.area()) # Prints 28.274333882308138
print(circle.circumference()) # Prints 18.84955592153876


leave a comment for more clarification.
6👍4
pydroid premium
write a program that check a given number prime or not?
here is one way to do the above question
n=int(input('number:- '))
if n > 2:
for i in range(2,n):
if n%i==0:
print(n,'is not prime')
break
else:
print(n,'is prime')
break
elif n ==2:
print(n,'is prime')
else:
print(n,'is not prime')

https://news.1rj.ru/str/zethioprograming
4