Read the excel file
>>> df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')
Read the excel file
>>> df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')
>>> f=open('/tmp/1.txt','r').readlines()
>>> [a.strip() for a in f if 'pe' in a.lower()]
>>> f=open('/tmp/1.txt','r')
>>> f.readlines()
['Ambiorix Rodriguez Placenio\n', 'Ruth Cedeno Lara\n', 'Katherine Montolio Perez\n', 'Armando Rodriguez Rosario\n']
do the magic
>>> f=open('/tmp/1.txt','r')
>>> [a.strip() for a in f.readlines()]
['Ambiorix Rodriguez Placenio', 'Ruth Cedeno Lara', 'Katherine Montolio Perez', 'Armando Rodriguez Rosario']
>>>
>>>
>>> f=open('/tmp/1.txt','r')
>>> [a.strip().upper() for a in f.readlines()]
['AMBIORIX RODRIGUEZ PLACENIO', 'RUTH CEDENO LARA', 'KATHERINE MONTOLIO PEREZ', 'ARMANDO RODRIGUEZ ROSARIO
>>> test = lambda c, a: print(a) if a > c else print(c)
>>>
>>> test(12,1)
12
>>> test(12,111)
111
>>>
Below are a set of scores that students have received in the past semester. Write code to determine how many are 90 or above and assign that result to the value a_scores
.
scores = "67 80 90 78 93 20 79 89 96 97 92 88 79 68 58 90 98 100 79 74 83 88 80 86 85 70 90 100"
a_scores=len([c for c in [ int(a) for a in scores.split()] if c>=90])
Write code that uses the string stored in org
and creates an acronym which is assigned to the variable acro
. Only the first letter of each word should be used, each letter in the acronym should be a capital letter, and there should be nothing to separate the letters of the acronym. Words that should not be included in the acronym are stored in the list stopwords
. For example, if org
was assigned the string “hello to world” then the resulting acronym should be “HW”.
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro=[a[0].upper() for a in org.split() if a not in stopwords]
acro=''.join(acro)
print(''.join(acro))
-------------
p_phrase = "was it a car or a cat I saw"
# Reverse the original phrase, keeping the spaces and case
r_phrase = p_phrase[::-1]
# Check if the original phrase is equal to the reversed version
is_palindrome = p_phrase.replace(" ", "").lower() == r_phrase.replace(" ", "").lower()
# Print the results
print("Original phrase:", p_phrase)
print("Reversed phrase:", r_phrase)
print("Is palindrome:", is_palindrome)
First, ensure you have Python installed. You can check by running:
python3 --version
If it's not installed, you can install it using:
sudo apt update sudo apt install python3 python3-venv python3-pip
Step 2: Create a Project Directory
Navigate to your desired project directory (or create a new one):
mkdir my_project cd my_project
Step 3: Create a Virtual Environment
Create a virtual environment named
myenv
(you can choose a different name if you prefer):python3 -m venv myenv
Step 4: Activate the Virtual Environment
Activate the virtual environment with:
source myenv/bin/activate
You should see the environment name in your terminal prompt, indicating that it’s active.
Step 5: Install Packages
Install any packages you need using
pip
. For example:pip install requests flask
Step 6: Generate a Requirements File
To create a
requirements.txt
file that lists all installed packages and their versions, run:pip freeze > requirements.txt
Step 7: List Installed Packages
To see all installed packages in the virtual environment, use:
pip list
Step 8: Deactivate the Virtual Environment
When you're done working in the virtual environment, deactivate it by running:
deactivate
l1 #list to evaluate
[100, 11, 12, 3, 4, 12, 3, 10, 100, 11, 100, 2, 200]
// list with numbers even postion on the list l1
[l1.index(c) for c in [a for a in l1 if a % 2==0]]
[0, 2, 4, 2, 7, 0, 0, 11, 12]
// list list with the value
[l1[e] for e in [l1.index(c) for c in [a for a in l1 if a % 2==0]]]
[100, 12, 4, 12, 10, 100, 100, 2, 200]
// if l1 in l2
[a for a in l1 if a in l2]
[11, 90]
//if l1 not in l2
[a for a in l1 if a not in l2]
[12, 31, 42]
https://docs.python.org/3/
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
https://docs.python.org/3/library/index.html
In Python, `__main__` is a special name used to denote the main entry point of a script. It allows code to determine whether a Python file is being run as the main program or being imported as a module in another script. This distinction is useful for organizing and controlling code execution.
### **Understanding `__main__` in Python**
#### **The `__name__` Variable**
- When a Python file is run directly, its `__name__` attribute is set to `"__main__"`.
- If the file is imported as a module in another script, its `__name__` attribute is set to the name of the file (excluding the `.py` extension).
#### **Common Usage**
The `if __name__ == "__main__":` construct is used to execute code only when the file is run directly, and not when it is imported as a module.
### **Example Usage**
Here’s a simple example demonstrating how to use `__main__`:
```python
# mymodule.py
def greet(name):
return f"Hello, {name}!"
def main():
print("This is the main function.")
name = input("Enter your name: ")
print(greet(name))
if __name__ == "__main__":
main()
```
### **Explanation:**
1. **Function Definitions:**
- `greet(name)`: A function that returns a greeting message.
- `main()`: A function that contains the main code to be executed.
2. **The `if __name__ == "__main__":` Block:**
- This block checks if the script is being run directly (`__name__` is `"__main__"`).
- If true, it calls the `main()` function.
3. **When Imported:**
- If you import `mymodule` in another script (e.g., `import mymodule`), the `main()` function will not execute automatically.
### **Running the Script**
1. **Direct Execution:**
- Running the script directly via the command line or an IDE:
```bash
python mymodule.py
```
- This will execute the `main()` function and produce output.
2. **Importing as a Module:**
- Importing in another Python script:
```python
import mymodule
# This will not execute the main() function automatically
```
### **Why Use `__main__`?**
1. **Modular Code:**
- Allows you to write code that can be both reusable as a module and executable as a standalone script.
2. **Organized Testing:**
- Helps in writing test code or script-specific code that should not run when the file is imported elsewhere.
3. **Prevent Unwanted Execution:**
- Prevents the execution of code blocks when the script is used as a module in other scripts, promoting code reuse and modular design.
### **Summary**
- `__main__` is used to define the main entry point of a Python script.
- The `if __name__ == "__main__":` construct helps to differentiate between running a script directly and importing it as a module.
- This practice helps in writing clean, modular, and testable code.
https://docs.python.org/3/library/__main__.html
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])
https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZF9pbnN0cnVjdGlvbnNfdXJsIjoiaHR0cHM6Ly9jZi1jb3Vyc2VzLWRhdGEuczMudXMuY2xvdWQtb2JqZWN0LXN0b3JhZ2UuYXBwZG9tYWluLmNsb3VkL0lCTURldmVsb3BlclNraWxsc05ldHdvcmstUFkwMTAxRU4tU2tpbGxzTmV0d29yay9sYWJzL2hhbmRvdXRzL0NoZWF0X1NoZWV0X1dlZWstMi5tZCIsInRvb2xfdHlwZSI6Imluc3RydWN0aW9uYWwtbGFiIiwiYWRtaW4iOmZhbHNlLCJpYXQiOjE3MTE2Mzg1OTl9.4L5yEN_BpE8odg36ZiLmkRTf3clYER2hiGe0Bu74ljo
https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZF9pbnN0cnVjdGlvbnNfdXJsIjoiaHR0cHM6Ly9jZi1jb3Vyc2VzLWRhdGEuczMudXMuY2xvdWQtb2JqZWN0LXN0b3JhZ2UuYXBwZG9tYWluLmNsb3VkL0lCTURldmVsb3BlclNraWxsc05ldHdvcmstUFkwMTAxRU4tU2tpbGxzTmV0d29yay9sYWJzL2hhbmRvdXRzL0NoZWF0X1NoZWV0X1dlZWstMl9QYXJ0LTIubWQiLCJ0b29sX3R5cGUiOiJpbnN0cnVjdGlvbmFsLWxhYiIsImFkbWluIjpmYWxzZSwiaWF0IjoxNzExNjM4NTk2fQ.luBpuMXe9qHg0AyG0CeoUDtX9HzrpDcLZMm6gP-p5Dw
users=['ambiorixg12', 'root', 'admin']
sec=[1330, 7813, 1333]
userdb = {a: sec.__getitem__(users.index(a)) for a in users}
Alternative method
userdb = {user: sec[users.index(user)] for user in users}
st
{'Ambiorix': 90, 'Rosa': 78, 'Eddy': 51, 'Maria': 32}
Max values from a dict
top=[a for a in st.keys() if st[a]==max(st.values())]
['Ambiorix']
[st[a] for a in st.keys()]
[90, 78, 51, 32]
list(st.values())
[90, 78, 51, 32]
list(st.keys())
['Ambiorix', 'Rosa', 'Eddy', 'Maria']
>>> scores =[103, 88, 67, 54, 23, 971, 34, 679]
>>> top=[a for a in scores if a==max(scores)] ##top socres
[971]
>>>
>>> pos=[scores.index(a) for a in scores if a==max(scores)] ##position on the list
[5]