diff --git a/2102020.ipynb b/2102020.ipynb index d39a89b..d6d357f 100644 --- a/2102020.ipynb +++ b/2102020.ipynb @@ -1,531 +1,580 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "736887bb", - "metadata": { - "id": "736887bb" - }, - "source": [ - "# Python Exercises - Easy Level\n", - "\n", - "These exercises test basic Python concepts including:\n", - "- Variables and data types\n", - "- Basic arithmetic operations\n", - "- Simple functions\n", - "- Basic conditionals\n", - "- String operations\n", - "- Lists and basic operations\n", - "\n", - "**Instructions:** Complete each exercise by writing Python code in the provided cells." - ] - }, - { - "cell_type": "markdown", - "id": "1fcc41ef", - "metadata": { - "id": "1fcc41ef" - }, - "source": [ - "## Question 1: Variables and Data Types\n", - "\n", - "Create variables for the following:\n", - "- `name` (string): Your name\n", - "- `age` (integer): Your age\n", - "- `height` (float): Your height in meters\n", - "- `is_student` (boolean): Whether you are a student\n", - "\n", - "Print each variable along with its type." - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "id": "6aacf345", - "metadata": { - "id": "6aacf345" - }, - "outputs": [], - "source": [ - "name=\"Sad man\"\n", - "age=23\n", - "height=5.7\n", - "is_student=True" - ] - }, - { - "cell_type": "markdown", - "id": "2c1683e1", - "metadata": { - "id": "2c1683e1" - }, - "source": [ - "## Question 2: Basic Arithmetic\n", - "\n", - "Given two numbers `a = 15` and `b = 4`, calculate and print:\n", - "- Sum\n", - "- Difference\n", - "- Product\n", - "- True division\n", - "- Floor division\n", - "- Remainder (modulus)\n", - "- `a` raised to the power of `b`" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "id": "86d758e2", - "metadata": { - "id": "86d758e2" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "19\n", - "11\n", - "60\n", - "3.75\n", - "3\n", - "3\n", - "50625\n" - ] - } - ], - "source": [ - "a = 15\n", - "b = 4\n", - "\n", - "sm=a+b\n", - "print(sm)\n", - "dif=a-b\n", - "print(dif)\n", - "pro=a*b\n", - "print(pro)\n", - "div=a/b\n", - "print(div)\n", - "flr=a//b\n", - "print(flr)\n", - "mod=a%b\n", - "print(mod)\n", - "pwr=a**b\n", - "print(pwr)\n" - ] - }, - { - "cell_type": "markdown", - "id": "e49abb2e", - "metadata": { - "id": "e49abb2e" - }, - "source": [ - "## Question 3: Simple Function\n", - "\n", - "Write a function called `greet_user` that:\n", - "- Takes a name as a parameter\n", - "- Returns a greeting message like \"Hello, [name]!\"\n", - "- Test it with your name" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "id": "34a366ec", - "metadata": { - "id": "34a366ec" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello Sad man\n" - ] - } - ], - "source": [ - "def greet_user(name):\n", - " print(\"hello\",name)\n", - "\n", - "name=\"Sad man\"\n", - "greet_user(name)" - ] - }, - { - "cell_type": "markdown", - "id": "f73d26e6", - "metadata": { - "id": "f73d26e6" - }, - "source": [ - "## Question 4: Basic Conditionals\n", - "\n", - "Write a function called `check_number` that:\n", - "- Takes a number as input\n", - "- Returns \"positive\" if the number is greater than 0\n", - "- Returns \"negative\" if the number is less than 0\n", - "- Returns \"zero\" if the number equals 0\n", - "\n", - "Test with numbers: 5, -3, 0" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "id": "19ae4d41", - "metadata": { - "id": "19ae4d41" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "positive\n" - ] - } - ], - "source": [ - "def check_number(num):\n", - " if(num>0):\n", - " print(\"positive\")\n", - " elif(num<0):\n", - " print(\"negative\")\n", - " else:\n", - " print(\"zero\")\n", - "\n", - "num=int(input(\"Enter a number: \"))\n", - "check_number(num)\n" - ] - }, - { - "cell_type": "markdown", - "id": "c8b83226", - "metadata": { - "id": "c8b83226" - }, - "source": [ - "## Question 5: String Operations\n", - "\n", - "Given the string `text = \"Python Programming\"`, perform the following operations:\n", - "- Print the length of the string\n", - "- Convert to uppercase\n", - "- Convert to lowercase\n", - "- Check if it starts with \"Python\"\n", - "- Replace \"Programming\" with \"is Fun\"" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "id": "f8221848", - "metadata": { - "id": "f8221848" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "18\n", - "PYTHON PROGRAMMING\n", - "python programming\n", - "Yes, it is Python\n", - "Python fun\n" - ] - } - ], - "source": [ - "text = \"Python Programming\"\n", - "\n", - "print(len(text))\n", - "print(text.upper())\n", - "print(text.lower())\n", - "if(\"Python\" == text[:6]):\n", - " print(\"Yes, it is Python\")\n", - "\n", - "t1=text.replace(\"Programming\", \"fun\")\n", - "print(t1)\n" - ] - }, - { - "cell_type": "markdown", - "id": "61723857", - "metadata": { - "id": "61723857" - }, - "source": [ - "## Question 6: Basic Lists\n", - "\n", - "Create a list called `fruits` with the following items: \"apple\", \"banana\", \"orange\", \"grape\"\n", - "\n", - "Then:\n", - "- Print the first fruit\n", - "- Print the last fruit\n", - "- Add \"mango\" to the end of the list\n", - "- Print the length of the list\n", - "- Check if \"banana\" is in the list" - ] - }, + "cells": [ + { + "cell_type": "markdown", + "id": "58f3c807", + "metadata": {}, + "source": [ + "# Python Exercises - Medium Level\n", + "\n", + "These exercises test intermediate Python concepts including:\n", + "- Functions with multiple parameters and return values\n", + "- List comprehensions\n", + "- Dictionary operations\n", + "- Advanced string manipulation\n", + "- Nested loops and conditionals\n", + "- Working with multiple data structures\n", + "\n", + "**Instructions:** Complete each exercise by writing Python code in the provided cells." + ] + }, + { + "cell_type": "markdown", + "id": "2eaf75cb", + "metadata": {}, + "source": [ + "## Question 1: Function with Multiple Return Values\n", + "\n", + "Write a function called `analyze_numbers` that:\n", + "- Takes a list of numbers as input\n", + "- Returns a tuple containing: (sum, average, maximum, minimum)\n", + "- Test with the list: [4, 7, 2, 9, 1, 6, 8, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "beb2582b", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": 62, - "id": "3bc3bb66", - "metadata": { - "id": "3bc3bb66" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "apple\n", - "grape\n", - "['apple', 'banana', 'orange', 'grape', 'mango']\n", - "5\n", - "yes\n" - ] - } - ], - "source": [ - "fruits=['apple','banana','orange','grape']\n", - "\n", - "print(fruits[0])\n", - "\n", - "print(fruits[-1])\n", - "\n", - "fruits.append('mango')\n", - "print(fruits)\n", - "\n", - "print(len(fruits))\n", - "i=0\n", - "while i < len(fruits):\n", - " if fruits[i] =='banana':\n", - " print(\"yes\")\n", - " break\n", - " else:\n", - " i=i+1\n" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "The numbers are: [4, 7, 2, 9, 1, 6, 8, 3]\n", + "sum is 40\n", + "max is 9\n", + "min is 1\n", + "avg is 5.0\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def analyze_numbers(numbers):\n", + " number=tuple(numbers)\n", + " print(\"The numbers are:\", numbers)\n", + " print('sum is ',sum(numbers))\n", + " print('max is ', max(numbers))\n", + " print('min is ', min(numbers))\n", + " print('avg is ', sum(numbers)/len(numbers))\n", + " \n", + "\n", + "lt=[4,7,2,9,1,6,8,3]\n", + "analyze_numbers(lt)" + ] + }, + { + "cell_type": "markdown", + "id": "32aa97ad", + "metadata": {}, + "source": [ + "## Question 2: List Comprehension Challenge\n", + "\n", + "Given the list `numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, create the following using list comprehensions:\n", + "- A list of squares of all even numbers\n", + "- A list of numbers divisible by 3\n", + "- A list of strings saying \"[number] is odd\" for odd numbers only" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "8745c171", + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "id": "ae8ce51f", - "metadata": { - "id": "ae8ce51f" - }, - "source": [ - "## Question 7: Simple Loop\n", - "\n", - "Write a function called `count_to_n` that:\n", - "- Takes a number `n` as input\n", - "- Prints all numbers from 1 to n (inclusive)\n", - "- Test it with n = 5" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "number are odd 1\n", + "squares are 4\n", + "number are odd 3\n", + "divisible by 3 3\n", + "squares are 16\n", + "number are odd 5\n", + "squares are 36\n", + "divisible by 3 6\n", + "number are odd 7\n", + "squares are 64\n", + "number are odd 9\n", + "divisible by 3 9\n", + "squares are 100\n" + ] + } + ], + "source": [ + "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "\n", + "# Your code here\n", + "i=0\n", + "while i < len(numbers):\n", + " if numbers[i] % 2 == 0:\n", + " print('squares are ', numbers[i] ** 2)\n", + " else:\n", + " print(\"number are odd \", numbers[i])\n", + " if numbers[i] % 3 == 0:\n", + " print(\"divisible by 3 \",numbers[i])\n", + "\n", + " i += 1" + ] + }, + { + "cell_type": "markdown", + "id": "e21948e9", + "metadata": {}, + "source": [ + "## Question 3: Dictionary Operations\n", + "\n", + "Create a dictionary called `student_grades` with the following data:\n", + "- \"Alice\": [85, 90, 78, 92]\n", + "- \"Bob\": [76, 88, 81, 79]\n", + "- \"Charlie\": [92, 95, 87, 91]\n", + "\n", + "Write a function that:\n", + "- Calculates the average grade for each student\n", + "- Returns a new dictionary with student names as keys and average grades as values\n", + "- Finds and prints the student with the highest average" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1f8c090", + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": null, - "id": "af9b0da0", - "metadata": { - "id": "af9b0da0" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n", - "5\n" - ] - } - ], - "source": [ - "def count_to_n(num):\n", - " i=1\n", - " while i 3.7:\n", + " print(gpa)\n", + "\n", + " group={}\n", + " if major not in group:\n", + " group[major]=[]\n", + " group[major].append(item[\"name\"])\n", + "\n", + " print(group)\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "58f3fcde", + "metadata": {}, + "source": [ + "## Question 10: Password Validator\n", + "\n", + "Write a function called `validate_password` that checks if a password meets these criteria:\n", + "- At least 8 characters long\n", + "- Contains at least one uppercase letter\n", + "- Contains at least one lowercase letter\n", + "- Contains at least one digit\n", + "- Contains at least one special character (!@#$%^&*)\n", + "\n", + "The function should return a dictionary with:\n", + "- \"valid\": True/False\n", + "- \"messages\": List of specific requirements that failed\n", + "\n", + "Test with passwords: \"Password123!\", \"weak\", \"NoDigits!\"" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "27ef3283", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "password choto boro vai\n", + "password e digit nai boro vai\n", + "password thik ase boro vai\n" + ] } + ], + "source": [ + "def validate_password(password):\n", + " if len(password) < 8:\n", + " return \"password choto boro vai\"\n", + " if not any(char.isupper() for char in password):\n", + " return \"password e uppercase nai boro vai\"\n", + " if not any(char.islower() for char in password):\n", + " return \"password e lowercase nai boro vai\"\n", + " if not any(char.isdigit() for char in password):\n", + " return \"password e digit nai boro vai\"\n", + " if not any(char in \"!@#$%^&*\" for char in password):\n", + " return \"password e special character nai boro vai\"\n", + " return \"password thik ase boro vai\"\n", + "\n", + "\n", + "print(validate_password(\"weak\"))\n", + "print(validate_password(\"NoDigits!\"))\n", + "print(validate_password(\"Password123!\"))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62f5fbb5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "myenv", + "language": "python", + "name": "python3" }, - "nbformat": 4, - "nbformat_minor": 5 + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 }