Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tutorial for While Loops and Prime Numbers file and also updated in the Readme. #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions Python_Basics/Intro/PythonBasicsWhileLoopsAndPrimeNumbers.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a32ab707",
"metadata": {},
"source": [
"# Task : While Loops"
]
},
{
"cell_type": "markdown",
"id": "06d87f59",
"metadata": {},
"source": [
"is_prime Function:\n",
"\n",
"-> Checks if a given number num is prime.\n",
"-> Returns False for numbers less than or equal to 1.\n",
"-> Returns True if the number is 2 (since 2 is a prime number).\n",
"-> Checks divisibility starting from 3 up to the square root of num. If num is divisible by any of these numbers, it returns False."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c058005e",
"metadata": {},
"outputs": [],
"source": [
"def is_prime(num):\n",
" \"\"\"Check if a number is prime.\"\"\"\n",
" if num <= 1:\n",
" return False\n",
" if num == 2:\n",
" return True\n",
" if num % 2 == 0:\n",
" return False\n",
"\n",
" i = 3\n",
" while i * i <= num:\n",
" if num % i == 0:\n",
" return False\n",
" i += 2\n",
" \n",
" return True"
]
},
{
"cell_type": "markdown",
"id": "1523bd57",
"metadata": {},
"source": [
"prime_numbers_up_to Function:\n",
"\n",
"-> Finds all prime numbers up to n.\n",
"-> Initializes an empty list primes to store prime numbers.\n",
"-> Uses a while loop to iterate from 2 up to n.\n",
"-> If the current number num is prime (determined by is_prime), it adds num to the list of primes.\n",
"-> Increments num by 1 on each iteration."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "81580e93",
"metadata": {},
"outputs": [],
"source": [
"def prime_numbers_up_to(n):\n",
" \"\"\"Return a list of all prime numbers up to n.\"\"\"\n",
" primes = []\n",
" num = 2\n",
"\n",
" while num <= n:\n",
" if is_prime(num):\n",
" primes.append(num)\n",
" num += 1\n",
"\n",
" return primes"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e8e855ab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prime numbers up to 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\n",
"29 is a prime number.\n"
]
}
],
"source": [
"# Example usage:\n",
"n = 50\n",
"print(f\"Prime numbers up to {n}: {prime_numbers_up_to(n)}\")\n",
"\n",
"# Example usage of is_prime function:\n",
"number_to_check = 29\n",
"if is_prime(number_to_check):\n",
" print(f\"{number_to_check} is a prime number.\")\n",
"else:\n",
" print(f\"{number_to_check} is not a prime number.\")"
]
},
{
"cell_type": "markdown",
"id": "cbef9c8e",
"metadata": {},
"source": [
"if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)"
]
},
{
"cell_type": "markdown",
"id": "a578046b",
"metadata": {},
"source": [
"https://www.youtube.com/watch?v=apEjxRmIp0I"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ What is it? | Blog Post/IPython Notebook | Youtube Video
9: Tuples + Fibonacci Sequence | [9: Tuples + Fibonacci Sequence](https://github.com/mGalarnyk/Python_Tutorials/blob/master/Python_Basics/Intro/PythonBasicsTuples.ipynb) | [9: Tuples + Fibonacci Sequence](https://www.youtube.com/watch?v=gUHeaQ0qZaw)
10: Dictionaries + Dictionary Manipulation | [10: Dictionaries + Dictionary Manipulation](https://github.com/mGalarnyk/Python_Tutorials/blob/master/Python_Basics/Intro/PythonBasicsDictionaries.ipynb) | [10: Dictionaries + Dictionary Manipulation](https://www.youtube.com/watch?v=LlIqrWJaBcQ)
11: Word Count (PunctuationFilter out , Dictionary Manipulation, and Sorting Lists) | [11: Word Count (Filter out Punctuation, Dictionary Manipulation, and Sorting Lists)](https://github.com/mGalarnyk/Python_Tutorials/blob/master/Python_Basics/Intro/PythonBasicsWordCount.ipynb) | [11: Word Count (Filter out Punctuation, Dictionary Manipulation, and Sorting Lists)](https://www.youtube.com/watch?v=l_dIleafLZ8)
12: While Loops and Prime Numbers | None | [12: While Loops and Prime Numbers](https://youtu.be/apEjxRmIp0I)
12: While Loops and Prime Numbers | [While Loops and Prime Numbers](https://github.com/PrajwalaY26/Python_Tutorials/blob/master/Python_Basics/Intro/PythonBasicsWhileLoopsAndPrimeNumbers.ipynb) | [12: While Loops and Prime Numbers](https://youtu.be/apEjxRmIp0I)
13: Python Sets and Set Theory | [Python Sets and Set Theory](https://towardsdatascience.com/python-sets-and-set-theory-2ace093d1607) | [Python Sets and Set Theory](https://youtu.be/hZPNPh5Zg3M)
Anagrams | [Using Python to Detect Anagrams](https://medium.com/@GalarnykMichael/using-python-to-detect-anagrams-a002ddedb4cb) | None
Prime Numbers | [Prime Numbers](https://medium.com/@GalarnykMichael/prime-numbers-using-python-824ff4b3ea19) | None
Expand Down