├── Assignments └── if_else.md ├── Day 01 ├── Getting Started.md ├── Python Basic Input and Output.md ├── Python Comments.md ├── Python Data Types.md ├── Python Keywords and Identifiers.md ├── Python Operator.md ├── Python Type Conversion.md └── Python Variables.md ├── Day 02 ├── Python For loop.md ├── Python break and continue.md ├── Python if...else Statement.md ├── Python pass Statement.md └── Python while Loop.md ├── Day 03 └── Python Functions.md ├── G_Tec_Jain_Keerti_Education_Employee_Training_.ipynb ├── LICENSE └── README.md /Assignments/if_else.md: -------------------------------------------------------------------------------- 1 | # Topic: Assignments and Tasks - Python `if...else` Statement 2 | 3 | #### Introduction: 4 | Welcome to the Assignments and Tasks section at CodesWithPankaj.com! In this series, we'll delve into practical assignments and tasks to enhance your understanding and mastery of Python programming. This edition focuses on the versatile `if...else` statement, a key element in making decisions within your code. 5 | 6 | ## Assignment - 7 | 8 | 1. Write a Program to Check Whether a Number is Even or Odd.? 9 | 10 | ```yaml 11 | Enter Number to Find Even or odd = 3 12 | 13 | -- Your Number 3 is Odd 14 | ``` 15 | 2. Write a Program to Check Whether an Alphabet is Vowel or Consonant ? 16 | 17 | ```yaml 18 | Enter Alphabet : A 19 | 20 | -- A is Vowel 21 | ``` 22 | 3. Write a Program to Find the Largest Among Three Numbers ? 23 | 24 | ```yaml 25 | Enter Number 1 : 100 26 | Enter Number 2 : 300 27 | Enter Number 3 : 400 28 | -- Number 300 is Largest 29 | ``` 30 | 31 | 4. Write a Program to Check Leap Year ? 32 | 33 | ```yaml 34 | Enter Year : 2023 35 | 36 | -- 2023 is not Leap Year 37 | ``` 38 | 39 | 5. Write a program to find Grade ? 40 | 41 | ```yaml 42 | Example : Grading System 43 | 80 - 100 = Grade A 44 | 60 - 80 = Grade B 45 | 40 - 60 = Grade C 46 | 30 - 40 = Grade D 47 | 0 - 30 Grade F 48 | 49 | ``` 50 | 51 | 6. Find Age ? 52 | 53 | ```yaml 54 | -- Date of birth Section 55 | 56 | Enter Your Birth Year : 1992 57 | Enter Your Birth Month : 4 58 | Enter Your Birth Day : 16 59 | 60 | -- Current Date 61 | 62 | Enter Your Current Year : 2023 63 | Enter Your Current Month : 7 64 | Enter Your Current Day : 26 65 | 66 | -- output 67 | 68 | 30 years 3 months 10 days 69 | or 363 months 10 days 70 | or 1579 weeks 5 days 71 | or 11,058 days 72 | or 265,392 hours 73 | 74 | ``` 75 | 76 | 7. Password Checker 77 | Example : 78 | ```yaml 79 | Set your password : 80 | p4n@in 81 | Enter your Password : 82 | p4n 83 | wrong password ... try 2 more time out of 2 84 | p4n@ 85 | wrong password ... try 1 more time 1 86 | p4n@34 87 | wrong password ... try 0 more time 0 88 | note : user select right password 89 | then start MCQ EXAM... 90 | 91 | 92 | 1. Who invented Java Programming? 93 | 1. ) Guido van Rossum 94 | 2. ) James Gosling 95 | 3. ) Dennis Ritchie 96 | 4. ) Bjarne Stroustrup 97 | 98 | Select Answer 2 99 | 100 | wrong answer [ Try Next year ] 101 | 102 | Note :if select Right Answer 103 | ask 2nd Question ... 104 | 105 | 2. Which component is used to compile, debug and execute the java programs? 106 | 1. ) JRE 107 | 2. ) JIT 108 | 3. ) JDK 109 | 4. ) JVM 110 | 111 | Select Answer 2 ... con.. 112 | ``` 113 | 114 | 8. Student Report Card System 115 | ```Yaml 116 | ---- Input Section 117 | Enter your name : Joy 118 | Enter Your Roll Number : A1023 119 | 120 | Enter Your JAVA Marks : 50 121 | Enter Your C++ Marks : 20 122 | Enter Your go Marks : 25 123 | Enter Your Ruby Marks : 96 124 | Enter Your C# Marks : 70 125 | Enter Your Python Marks : 65 126 | 127 | ---- Output Section 128 | JAVA = 50/100 129 | C++ = 20/100 F 130 | go = 25/100 F 131 | Ruby = 96/100 132 | C# = 70/100 133 | Python = 65/100 134 | 135 | Total = 326/600 136 | per = 54% FAIL 137 | IF PASS 138 | Grading System 139 | 80 - 100 = Grade A 140 | 60 - 80 = Grade B 141 | 40 - 60 = Grade C 142 | 30 - 40 = Grade D 143 | ``` 144 | 145 | 146 | #### Conclusion: 147 | These assignments and tasks are designed to reinforce your skills in using the `if...else` statement in Python. They cover a range of scenarios, helping you build confidence and proficiency in decision-making within your code. 148 | 149 | Stay tuned for more challenging assignments and tasks at [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 150 | -------------------------------------------------------------------------------- /Day 01 /Getting Started.md: -------------------------------------------------------------------------------- 1 | # Getting started with Python is a great idea, as it's a versatile and beginner-friendly programming language. 2 | ### Here's a step-by-step tutorial on how to install Python and get started with your first program: 3 | 4 | ### Step 1: Install Python 5 | 6 | 1. **Download Python:** 7 | - Visit the official Python website: [Python Downloads](https://www.python.org/downloads/). 8 | - The website should automatically suggest the latest version suitable for your operating system (Windows, macOS, or Linux). Click on the "Download" button. 9 | 10 | 2. **Run the Installer:** 11 | - For Windows: Run the downloaded executable file and follow the installation wizard's instructions. 12 | - For macOS: Open the downloaded `.pkg` file and follow the installation instructions. 13 | - For Linux: Python is often pre-installed. If not, use your package manager (e.g., `apt` for Ubuntu) to install Python. 14 | 15 | 3. **Check Installation:** 16 | - Open a command prompt (Windows) or terminal (macOS/Linux). 17 | - Type `python --version` or `python3 --version` and press Enter. You should see the installed Python version. 18 | 19 | ### Step 2: Set Up a Development Environment 20 | 21 | 1. **Text Editor or IDE:** 22 | - Choose a code editor or integrated development environment (IDE). Some popular choices include: 23 | - VSCode 24 | - PyCharm 25 | - Atom 26 | - Sublime Text 27 | - IDLE (comes with Python) 28 | 29 | ### Step 3: Write Your First Python Program 30 | 31 | 1. **Open Your Text Editor/IDE:** 32 | - Launch your chosen editor or IDE. 33 | 34 | 2. **Create a New File:** 35 | - Open a new file in your editor. 36 | 37 | 3. **Write Your First Program:** 38 | - Type the following simple Python program: 39 | ```python 40 | print("Hello, Python!") 41 | ``` 42 | 43 | 4. **Save the File:** 44 | - Save the file with a `.py` extension, for example, `first_program.py`. 45 | 46 | 5. **Run the Program:** 47 | - Open a terminal or command prompt in the same directory where you saved your Python file. 48 | - Type `python first_program.py` (or `python3 first_program.py` on some systems) and press Enter. 49 | - You should see the output: `Hello, Python!` 50 | 51 | Congratulations! You've successfully installed Python, set up a development environment, and written and executed your first Python program. 52 | 53 | ### Step 4: Learn and Explore 54 | 55 | 1. **Official Python Documentation:** 56 | - Explore the [Python Documentation](https://docs.python.org/3/). 57 | 58 | 2. **Tutorials and Online Courses:** 59 | - Utilize online platforms like Codecademy, Coursera, or edX for structured Python courses. 60 | 61 | 3. **Practice and Experiment:** 62 | - Try solving coding challenges on platforms like HackerRank or LeetCode. 63 | - Build small projects to apply your knowledge. 64 | 65 | Remember that programming is a skill that improves with practice, so keep coding and have fun exploring the world of Python! 66 | -------------------------------------------------------------------------------- /Day 01 /Python Basic Input and Output.md: -------------------------------------------------------------------------------- 1 | # Python Basic Input and Output 2 | 3 | #### Introduction: 4 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the fundamental concepts of input and output in Python. Input allows users to provide data to a program, while output enables the program to display information to the user. Let's dive into the world of basic input and output in Python! 5 | 6 | #### Basic Output (print function): 7 | 8 | The `print()` function is used to display output in Python. 9 | 10 | ```python 11 | # Basic Output 12 | print("Hello, Python!") 13 | ``` 14 | 15 | #### Formatted Output: 16 | 17 | You can format output using the `format()` method or f-strings. 18 | 19 | ```python 20 | # Formatted Output 21 | name = "Pankaj" 22 | age = 25 23 | print("My name is {} and I am {} years old.".format(name, age)) 24 | 25 | # Using f-string (Python 3.6 and above) 26 | print(f"My name is {name} and I am {age} years old.") 27 | ``` 28 | 29 | #### Basic Input (input function): 30 | 31 | The `input()` function is used to take user input. It returns a string that can be converted to the desired data type. 32 | 33 | ```python 34 | # Basic Input 35 | user_input = input("Enter your name: ") 36 | print("Hello, " + user_input + "!") 37 | ``` 38 | 39 | #### Converting Input to Numeric Types: 40 | 41 | You can convert user input to numeric types (int, float) using type casting. 42 | 43 | ```python 44 | # Converting Input to Numeric Types 45 | age_input = input("Enter your age: ") 46 | age = int(age_input) # Convert the input to an integer 47 | print("Next year, you'll be", age + 1, "years old.") 48 | ``` 49 | 50 | #### Example Code: 51 | 52 | Now, let's create a Python script that combines basic input and output: 53 | 54 | ```python 55 | # codeswithpankaj_input_output.py 56 | 57 | # Basic Output 58 | print("Welcome to CodesWithPankaj.com!") 59 | 60 | # Formatted Output 61 | name = input("Enter your name: ") 62 | age = int(input("Enter your age: ")) 63 | print("Hello, {}! You are {} years old.".format(name, age)) 64 | 65 | # Basic Input 66 | user_input = input("Enter any message: ") 67 | print("You entered:", user_input) 68 | ``` 69 | 70 | #### Conclusion: 71 | 72 | Understanding basic input and output is essential for creating interactive and user-friendly Python programs. By incorporating these concepts, you can build applications that respond to user input and provide meaningful output. 73 | 74 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 75 | -------------------------------------------------------------------------------- /Day 01 /Python Comments.md: -------------------------------------------------------------------------------- 1 | 2 | # Python Comments 3 | 4 | #### Introduction: 5 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the importance of comments in Python programming. Comments are essential for documenting your code, making it more readable, and helping others (or even yourself) understand your thought process. Let's dive into the world of Python comments! 6 | 7 | #### Types of Comments: 8 | 9 | Python supports two types of comments: 10 | 11 | 1. **Single-line comments:** These comments are created using the `#` symbol and are used for commenting on a single line. 12 | 13 | Example: 14 | ```python 15 | # This is a single-line comment 16 | print("Hello, Python!") 17 | ``` 18 | 19 | 2. **Multi-line comments (Docstrings):** These comments span multiple lines and are enclosed within triple-quotes (`'''` or `"""`). They are often used as docstrings to provide documentation for functions, modules, or classes. 20 | 21 | Example: 22 | ```python 23 | ''' 24 | This is a multi-line comment (docstring). 25 | It provides documentation for the following function. 26 | ''' 27 | 28 | def my_function(): 29 | """This is also a docstring for the function.""" 30 | print("Doing something...") 31 | ``` 32 | 33 | #### Best Practices for Using Comments: 34 | 35 | 1. **Be Clear and Concise:** 36 | - Clearly explain complex sections or algorithms. 37 | - Avoid unnecessary comments that merely restate the code. 38 | 39 | 2. **Update Comments Regularly:** 40 | - Keep comments up-to-date with code changes. 41 | - Outdated comments can lead to confusion. 42 | 43 | 3. **Use Descriptive Variable and Function Names:** 44 | - Well-named variables and functions reduce the need for excessive comments. 45 | 46 | 4. **Docstrings for Functions and Classes:** 47 | - Include docstrings to describe the purpose, parameters, and return values of functions and classes. 48 | 49 | #### Example Code with Comments: 50 | 51 | Now, let's enhance a simple Python script with comments to explain the code: 52 | 53 | ```python 54 | # codeswithpankaj_comments.py 55 | 56 | # This script calculates the sum of two numbers 57 | 58 | # Function to add two numbers 59 | def add_numbers(a, b): 60 | """ 61 | This function takes two parameters and returns their sum. 62 | :param a: The first number 63 | :param b: The second number 64 | :return: The sum of a and b 65 | """ 66 | result = a + b 67 | return result 68 | 69 | # Input values 70 | num1 = 5 71 | num2 = 7 72 | 73 | # Calculate the sum using the function 74 | sum_result = add_numbers(num1, num2) 75 | 76 | # Display the result 77 | print(f"The sum of {num1} and {num2} is: {sum_result}") 78 | ``` 79 | 80 | #### Conclusion: 81 | 82 | Comments play a vital role in making your code more understandable and maintainable. By incorporating comments effectively, you contribute to a collaborative coding environment. 83 | 84 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 85 | -------------------------------------------------------------------------------- /Day 01 /Python Data Types.md: -------------------------------------------------------------------------------- 1 | # Python Data Types 2 | 3 | #### Introduction: 4 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the various data types in Python. Understanding data types is crucial for writing effective and efficient Python code. Let's dive into the world of Python data types! 5 | 6 | #### Common Data Types: 7 | 8 | Python supports several built-in data types. Here are some of the most common ones: 9 | 10 | | Data Types | Classes | Description | 11 | |------------|-------------------|--------------------------------------------| 12 | | Numeric | int, float, complex| Holds numeric values | 13 | | String | str | Holds a sequence of characters | 14 | | Sequence | list, tuple, range | Holds a collection of items | 15 | | Mapping | dict | Holds data in key-value pair form | 16 | | Boolean | bool | Holds either True or False | 17 | | Set | set, frozenset | Holds a collection of unique items | 18 | 19 | 1. **Numeric Types:** 20 | - **int:** Integer type, e.g., `5`, `-10`. 21 | - **float:** Floating-point type, e.g., `3.14`, `2.0`. 22 | - **complex:** Complex number type, e.g., `2 + 3j`. 23 | 24 | ```python 25 | # Numeric types 26 | integer_number = 42 27 | float_number = 3.14 28 | complex_number = 2 + 3j 29 | ``` 30 | 31 | 2. **Sequence Types:** 32 | - **str:** String type, e.g., `"Hello, Python!"`. 33 | - **list:** List type, e.g., `[1, 2, 3]`. 34 | - **tuple:** Tuple type, e.g., `(1, 2, 3)`. 35 | 36 | ```python 37 | # Sequence types 38 | string_data = "Hello, Python!" 39 | list_data = [1, 2, 3] 40 | tuple_data = (1, 2, 3) 41 | ``` 42 | 43 | 3. **Boolean Type:** 44 | - **bool:** Boolean type, either `True` or `False`. 45 | 46 | ```python 47 | # Boolean type 48 | is_true = True 49 | is_false = False 50 | ``` 51 | 52 | 4. **Set Types:** 53 | - **set:** Unordered collection of unique elements. 54 | - **frozenset:** Immutable set. 55 | 56 | ```python 57 | # Set types 58 | set_data = {1, 2, 3} 59 | frozen_set_data = frozenset({4, 5, 6}) 60 | ``` 61 | 62 | 5. **Mapping Type:** 63 | - **dict:** Dictionary type, a collection of key-value pairs. 64 | 65 | ```python 66 | # Mapping type 67 | dictionary_data = {"name": "John", "age": 25, "city": "New York"} 68 | ``` 69 | 70 | 6. **None Type:** 71 | - **None:** Represents the absence of a value. 72 | 73 | ```python 74 | # None type 75 | no_value = None 76 | ``` 77 | 78 | #### Example Code: 79 | 80 | Now, let's create a Python script that demonstrates the use of various data types: 81 | 82 | ```python 83 | # codeswithpankaj_data_types.py 84 | 85 | # Numeric types 86 | integer_number = 42 87 | float_number = 3.14 88 | complex_number = 2 + 3j 89 | 90 | # Sequence types 91 | string_data = "Hello, Python!" 92 | list_data = [1, 2, 3] 93 | tuple_data = (1, 2, 3) 94 | 95 | # Boolean type 96 | is_true = True 97 | is_false = False 98 | 99 | # Set types 100 | set_data = {1, 2, 3} 101 | frozen_set_data = frozenset({4, 5, 6}) 102 | 103 | # Mapping type 104 | dictionary_data = {"name": "John", "age": 25, "city": "New York"} 105 | 106 | # None type 107 | no_value = None 108 | 109 | # Displaying values 110 | print("Numeric Types:", integer_number, float_number, complex_number) 111 | print("Sequence Types:", string_data, list_data, tuple_data) 112 | print("Boolean Type:", is_true, is_false) 113 | print("Set Types:", set_data, frozen_set_data) 114 | print("Mapping Type:", dictionary_data) 115 | print("None Type:", no_value) 116 | ``` 117 | 118 | #### Conclusion: 119 | 120 | Understanding Python data types is crucial for writing robust and flexible code. By utilizing the appropriate data type for your variables, you can ensure efficient data manipulation and enhance the overall quality of your programs. 121 | 122 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 123 | -------------------------------------------------------------------------------- /Day 01 /Python Keywords and Identifiers.md: -------------------------------------------------------------------------------- 1 | 2 | # Python Keywords and Identifiers 3 | 4 | #### Introduction: 5 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore Python keywords and identifiers—essential concepts for understanding Python programming. Whether you're a beginner or looking to reinforce your knowledge, let's dive in. 6 | 7 | #### Python Keywords: 8 | 9 | Keywords are reserved words in Python that have specific meanings and cannot be used as identifiers (variable names, function names, etc.). Here's a table of Python keywords: 10 | 11 | | | | | | | 12 | |-------|-------|--------|-------|--------| 13 | | False | class | finally| is | return | 14 | | None | continue | for | lambda| try | 15 | | True | def | from | nonlocal | while| 16 | | and | del | global | not | with | 17 | | as | elif | if | or | yield | 18 | | assert| else | import | pass | | 19 | | break | except| in | raise | | 20 | 21 | Feel free to reference this table whenever you encounter a keyword in your Python code. 22 | 23 | #### Identifiers: 24 | 25 | Identifiers are names given to variables, functions, classes, etc. They follow certain rules: 26 | 27 | - Must start with a letter (a-z, A-Z) or an underscore (_). 28 | - Can be followed by letters, underscores, and digits (0-9). 29 | - Case-sensitive. 30 | 31 | Examples of valid identifiers: `my_variable`, `_count`, `Total2`. 32 | 33 | #### Creating Identifiers: 34 | 35 | Now, let's write a simple Python script to demonstrate the creation of identifiers: 36 | 37 | ```python 38 | # codeswithpankaj_identifiers.py 39 | 40 | # Valid identifiers 41 | my_variable = 42 42 | _count = 10 43 | Total2 = 100 44 | 45 | # Invalid identifiers - Uncommenting these lines will result in an error 46 | # 2nd_variable = 20 # starts with a digit 47 | # my-variable = 5 # contains a hyphen 48 | # class = "Python" # keyword used as an identifier 49 | 50 | # Displaying values 51 | print("Valid Identifiers:") 52 | print("my_variable =", my_variable) 53 | print("_count =", _count) 54 | print("Total2 =", Total2) 55 | 56 | # Uncommenting the next line will result in an error 57 | # print("2nd_variable =", 2nd_variable) 58 | ``` 59 | 60 | This script demonstrates valid and invalid identifiers. Uncomment the invalid lines to observe the errors they produce. 61 | 62 | #### Conclusion: 63 | 64 | Understanding Python keywords and identifiers is crucial for writing clean, error-free code. Now that you've grasped these concepts, you're well on your way to becoming a proficient Python coder! 65 | 66 | Stay tuned for more tutorials and coding tips at [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 67 | -------------------------------------------------------------------------------- /Day 01 /Python Operator.md: -------------------------------------------------------------------------------- 1 | 2 | #### Introduction: 3 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the various operators in Python. Operators are symbols that perform operations on variables and values. Understanding these operators is fundamental for writing effective Python code. Let's dive into the world of Python operators! 4 | 5 | #### Arithmetic Operators: 6 | 7 | 1. **Addition (`+`):** Adds two operands. 8 | 2. **Subtraction (`-`):** Subtracts the right operand from the left operand. 9 | 3. **Multiplication (`*`):** Multiplies two operands. 10 | 4. **Division (`/`):** Divides the left operand by the right operand. 11 | 5. **Modulus (`%`):** Returns the remainder of the division. 12 | 6. **Exponentiation (`**`):** Raises the left operand to the power of the right operand. 13 | 7. **Floor Division (`//`):** Returns the floor of the division (quotient without remainder). 14 | 15 | ```python 16 | # Arithmetic Operators 17 | a = 10 18 | b = 3 19 | 20 | addition_result = a + b 21 | subtraction_result = a - b 22 | multiplication_result = a * b 23 | division_result = a / b 24 | modulus_result = a % b 25 | exponentiation_result = a ** b 26 | floor_division_result = a // b 27 | 28 | print("Addition:", addition_result) 29 | print("Subtraction:", subtraction_result) 30 | print("Multiplication:", multiplication_result) 31 | print("Division:", division_result) 32 | print("Modulus:", modulus_result) 33 | print("Exponentiation:", exponentiation_result) 34 | print("Floor Division:", floor_division_result) 35 | ``` 36 | 37 | #### Comparison Operators: 38 | 39 | 1. **Equal to (`==`):** True if both operands are equal. 40 | 2. **Not equal to (`!=`):** True if operands are not equal. 41 | 3. **Greater than (`>`):** True if the left operand is greater than the right operand. 42 | 4. **Less than (`<`):** True if the left operand is less than the right operand. 43 | 5. **Greater than or equal to (`>=`):** True if the left operand is greater than or equal to the right operand. 44 | 6. **Less than or equal to (`<=`):** True if the left operand is less than or equal to the right operand. 45 | 46 | ```python 47 | # Comparison Operators 48 | x = 5 49 | y = 8 50 | 51 | equal_result = x == y 52 | not_equal_result = x != y 53 | greater_than_result = x > y 54 | less_than_result = x < y 55 | greater_equal_result = x >= y 56 | less_equal_result = x <= y 57 | 58 | print("Equal:", equal_result) 59 | print("Not Equal:", not_equal_result) 60 | print("Greater Than:", greater_than_result) 61 | print("Less Than:", less_than_result) 62 | print("Greater Than or Equal To:", greater_equal_result) 63 | print("Less Than or Equal To:", less_equal_result) 64 | ``` 65 | 66 | #### Logical Operators: 67 | 68 | 1. **Logical AND (`and`):** True if both operands are true. 69 | 2. **Logical OR (`or`):** True if at least one operand is true. 70 | 3. **Logical NOT (`not`):** True if the operand is false. 71 | 72 | ```python 73 | # Logical Operators 74 | p = True 75 | q = False 76 | 77 | and_result = p and q 78 | or_result = p or q 79 | not_result_p = not p 80 | not_result_q = not q 81 | 82 | print("Logical AND:", and_result) 83 | print("Logical OR:", or_result) 84 | print("Logical NOT (p):", not_result_p) 85 | print("Logical NOT (q):", not_result_q) 86 | ``` 87 | 88 | #### Assignment Operators: 89 | 90 | 1. **Assignment (`=`):** Assigns the value on the right to the variable on the left. 91 | 2. **Addition Assignment (`+=`):** Adds the right operand to the left operand and assigns the result to the left operand. 92 | 3. **Subtraction Assignment (`-=`):** Subtracts the right operand from the left operand and assigns the result to the left operand. 93 | 4. **Multiplication Assignment (`*=`):** Multiplies the left operand by the right operand and assigns the result to the left operand. 94 | 5. **Division Assignment (`/=`):** Divides the left operand by the right operand and assigns the result to the left operand. 95 | 6. **Modulus Assignment (`%=`):** Computes the modulus of the left operand with the right operand and assigns the result to the left operand. 96 | 7. **Exponentiation Assignment (`**=`):** Raises the left operand to the power of the right operand and assigns the result to the left operand. 97 | 8. **Floor Division Assignment (`//=`):** Computes the floor division of the left operand by the right operand and assigns the result to the left operand. 98 | 99 | ```python 100 | # Assignment Operators 101 | x = 10 102 | y = 3 103 | 104 | # Basic assignment 105 | a = x 106 | 107 | # Addition assignment 108 | x += y # equivalent to x = x + y 109 | 110 | # Subtraction assignment 111 | x -= y # equivalent to x = x - y 112 | 113 | # Multiplication assignment 114 | x *= y # equivalent to x = x * y 115 | 116 | # Division assignment 117 | x /= y # equivalent to x = x / y 118 | 119 | # Modulus assignment 120 | x %= y # equivalent to x = x % y 121 | 122 | # Exponentiation assignment 123 | x **= y # equivalent to x = x ** y 124 | 125 | # Floor Division assignment 126 | x //= y # equivalent to x = x // y 127 | 128 | print("Basic Assignment:", a) 129 | print("Addition Assignment:", x) 130 | ``` 131 | 132 | #### Identity Operators: 133 | 134 | 1. **Identity (`is`):** True if both operands refer to the same object. 135 | 2. **Not Identity (`is not`):** True if both operands do not refer to the same object. 136 | 137 | ```python 138 | # Identity Operators 139 | list1 = [1, 2, 3] 140 | list2 = [1, 2, 3] 141 | list3 = list1 142 | 143 | is_result = list1 is list2 144 | is_not_result = list1 is not list3 145 | 146 | print("Identity (is):", is_result) 147 | print("Not Identity (is not):", is_not_result) 148 | ``` 149 | 150 | #### Membership Operators: 151 | 152 | 1. **Membership (`in`):** True if a value is found in the sequence. 153 | 2. **Not Membership (`not in`):** True if a value is not found in the sequence. 154 | 155 | ```python 156 | # Membership Operators 157 | numbers = [1, 2, 3, 4, 5] 158 | 159 | in_result = 3 in numbers 160 | not_in_result = 6 not in numbers 161 | 162 | print("Membership (in):", in_result) 163 | print("Not Membership (not in):", not_in_result) 164 | ``` 165 | 166 | #### Bitwise Operators: 167 | 168 | 1. **Bitwise AND (`&`):** Performs bitwise AND. 169 | 2. **Bitwise OR (`|`):** Performs bitwise OR. 170 | 3. **Bitwise XOR (`^`):** Performs bitwise XOR. 171 | 4. **Bitwise NOT (`~`):** Inverts the bits of a number. 172 | 5. **Left Shift (`<<`):** Shifts the bits to the left. 173 | 6. **Right Shift (`>>`):** Shifts the bits to the right. 174 | 175 | ```python 176 | # Bitwise Operators 177 | a = 5 # binary: 0101 178 | b = 3 # binary: 001 179 | 180 | 1 181 | 182 | bitwise_and_result = a & b # binary: 0001 (decimal: 1) 183 | bitwise_or_result = a | b # binary: 0111 (decimal: 7) 184 | bitwise_xor_result = a ^ b # binary: 0110 (decimal: 6) 185 | bitwise_not_result = ~a # binary: 1010 (decimal: -6, due to two's complement representation) 186 | left_shift_result = a << 1 # binary: 1010 (decimal: 10) 187 | right_shift_result = a >> 1 # binary: 0010 (decimal: 2) 188 | 189 | print("Bitwise AND:", bitwise_and_result) 190 | print("Bitwise OR:", bitwise_or_result) 191 | print("Bitwise XOR:", bitwise_xor_result) 192 | print("Bitwise NOT:", bitwise_not_result) 193 | print("Left Shift:", left_shift_result) 194 | print("Right Shift:", right_shift_result) 195 | ``` 196 | 197 | #### Conclusion: 198 | 199 | Understanding Python operators is crucial for performing various operations on data. By mastering these operators, you gain the ability to manipulate values, compare conditions, and perform bitwise operations in your Python programs. 200 | 201 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 202 | -------------------------------------------------------------------------------- /Day 01 /Python Type Conversion.md: -------------------------------------------------------------------------------- 1 | # Python Type Conversion 2 | 3 | 4 | #### Introduction: 5 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the concept of type conversion in Python. Type conversion, also known as type casting, is the process of converting one data type into another. Python provides built-in functions for this purpose, allowing you to work seamlessly with different types of data. Let's dive into the world of Python type conversion! 6 | 7 | #### Implicit vs. Explicit Type Conversion: 8 | 9 | 1. **Implicit Type Conversion (Coercion):** 10 | - Python automatically converts one data type to another. 11 | - It occurs during operations between different data types. 12 | 13 | ```python 14 | # Implicit Type Conversion 15 | int_number = 10 16 | float_number = 3.5 17 | 18 | result = int_number + float_number # int_number is implicitly converted to float 19 | ``` 20 | 21 | 2. **Explicit Type Conversion (Type Casting):** 22 | - You explicitly convert a data type using predefined functions. 23 | - Common functions: `int()`, `float()`, `str()`, etc. 24 | 25 | ```python 26 | # Explicit Type Conversion 27 | float_number = 3.5 28 | 29 | # Convert float to int 30 | int_number = int(float_number) 31 | ``` 32 | 33 | #### Common Type Conversion Functions: 34 | 35 | 1. **`int()`:** Converts a value to an integer. 36 | 37 | ```python 38 | float_number = 3.8 39 | int_number = int(float_number) # Result: 3 40 | ``` 41 | 42 | 2. **`float()`:** Converts a value to a floating-point number. 43 | 44 | ```python 45 | int_number = 5 46 | float_number = float(int_number) # Result: 5.0 47 | ``` 48 | 49 | 3. **`str()`:** Converts a value to a string. 50 | 51 | ```python 52 | numeric_value = 42 53 | string_value = str(numeric_value) # Result: "42" 54 | ``` 55 | 56 | 4. **`list()`, `tuple()`, `set()`:** Converts a sequence to a list, tuple, or set. 57 | 58 | ```python 59 | my_tuple = (1, 2, 3) 60 | list_version = list(my_tuple) # Result: [1, 2, 3] 61 | ``` 62 | 63 | 5. **`bool()`:** Converts a value to a boolean. 64 | 65 | ```python 66 | numeric_value = 0 67 | boolean_value = bool(numeric_value) # Result: False 68 | ``` 69 | 70 | #### Example Code: 71 | 72 | Now, let's create a Python script that demonstrates both implicit and explicit type conversion: 73 | 74 | ```python 75 | # codeswithpankaj_type_conversion.py 76 | 77 | # Implicit Type Conversion 78 | int_number = 10 79 | float_number = 3.5 80 | 81 | result = int_number + float_number # int_number is implicitly converted to float 82 | print("Implicit Conversion Result:", result) 83 | 84 | # Explicit Type Conversion 85 | float_number = 3.8 86 | 87 | # Convert float to int 88 | int_number = int(float_number) 89 | print("Explicit Conversion Result (float to int):", int_number) 90 | 91 | # Convert int to str 92 | numeric_value = 42 93 | string_value = str(numeric_value) 94 | print("Explicit Conversion Result (int to str):", string_value) 95 | 96 | # Convert list to set 97 | my_list = [1, 2, 3, 3, 4] 98 | set_version = set(my_list) 99 | print("Explicit Conversion Result (list to set):", set_version) 100 | ``` 101 | 102 | #### Conclusion: 103 | 104 | Understanding type conversion in Python is crucial for working with diverse data types. Whether it's automatic (implicit) or manually triggered (explicit), type conversion allows you to handle data more flexibly in your programs. 105 | 106 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 107 | -------------------------------------------------------------------------------- /Day 01 /Python Variables.md: -------------------------------------------------------------------------------- 1 | # Python Variables, Constants and Literals 2 | 3 | #### Introduction: 4 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the fundamental concepts of variables, constants, and literals in Python. Understanding these concepts is crucial for effective programming. Let's dive in! 5 | 6 | #### Variables: 7 | 8 | A variable is a named storage location that holds data. In Python, you can create a variable by assigning a value to it. Variable names should follow certain rules: 9 | 10 | - Must start with a letter (a-z, A-Z) or an underscore (_). 11 | - Can be followed by letters, underscores, and digits (0-9). 12 | - Case-sensitive. 13 | 14 | Example: 15 | ```python 16 | # Variable creation 17 | my_variable = 42 18 | 19 | # Accessing the variable 20 | print("Value of my_variable:", my_variable) 21 | ``` 22 | 23 | #### Constants: 24 | 25 | In Python, constants are not a distinct data type, but it's a convention to use uppercase names for values that should be treated as constants. By convention, constants are not meant to be changed during the execution of the program. 26 | 27 | Example: 28 | ```python 29 | # Constant declaration 30 | PI = 3.14159 31 | 32 | # Accessing the constant 33 | print("Value of PI:", PI) 34 | ``` 35 | 36 | #### Literals: 37 | 38 | Literals are data used for representing values in Python. They can be of various types, such as numeric literals, string literals, and boolean literals. 39 | 40 | - Numeric Literals: Represent numbers and can be integers, floats, or complex numbers. 41 | ```python 42 | # Integer literal 43 | integer_literal = 42 44 | 45 | # Float literal 46 | float_literal = 3.14 47 | 48 | # Complex number literal 49 | complex_literal = 2 + 3j 50 | ``` 51 | 52 | - String Literals: Represent sequences of characters. 53 | ```python 54 | # Single-quoted string literal 55 | single_quoted_literal = 'Hello' 56 | 57 | # Double-quoted string literal 58 | double_quoted_literal = "World" 59 | ``` 60 | 61 | - Boolean Literals: Represent the truth values `True` or `False`. 62 | ```python 63 | # Boolean literals 64 | true_literal = True 65 | false_literal = False 66 | ``` 67 | 68 | #### Example Code: 69 | 70 | Now, let's create a Python script that uses variables, constants, and literals: 71 | 72 | ```python 73 | # codeswithpankaj_variables.py 74 | 75 | # Variables 76 | my_variable = 42 77 | 78 | # Constants 79 | PI = 3.14159 80 | 81 | # Literals 82 | integer_literal = 123 83 | string_literal = "p4n, Python!" 84 | boolean_literal = True 85 | 86 | # Displaying values 87 | print("Value of my_variable:", my_variable) 88 | print("Value of PI:", PI) 89 | print("Integer Literal:", integer_literal) 90 | print("String Literal:", string_literal) 91 | print("Boolean Literal:", boolean_literal) 92 | ``` 93 | 94 | #### Conclusion: 95 | 96 | Understanding variables, constants, and literals is fundamental to Python programming. By using them effectively, you can store, represent, and manipulate data in your programs. 97 | 98 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 99 | -------------------------------------------------------------------------------- /Day 02/Python For loop.md: -------------------------------------------------------------------------------- 1 | # Python `for` Loop - In-Depth Explanation 2 | 3 | #### Introduction: 4 | Welcome to an in-depth exploration of the `for` loop in Python at CodesWithPankaj.com! The `for` loop is a powerful and versatile construct that allows you to iterate over elements in a sequence, making it an essential tool in your programming arsenal. Let's break down the intricacies of the `for` loop. 5 | 6 | #### Basic Syntax: 7 | The basic syntax of the `for` loop is as follows: 8 | 9 | ```python 10 | for variable in iterable: 11 | # Code to be executed for each iteration 12 | ``` 13 | 14 | - `variable`: Represents the current element in the iteration. 15 | - `iterable`: Refers to an iterable object like a list, tuple, string, or any other object that can be iterated over. 16 | 17 | #### Example 1: Iterating Over a List 18 | 19 | ```python 20 | # Example 1: Iterating Over a List 21 | fruits = ["apple", "banana", "orange"] 22 | 23 | for fruit in fruits: 24 | print(f"I love {fruit}s!") 25 | ``` 26 | 27 | **Output:** 28 | ``` 29 | I love apples! 30 | I love bananas! 31 | I love oranges! 32 | ``` 33 | 34 | #### Example 2: Iterating Over a String 35 | 36 | ```python 37 | # Example 2: Iterating Over a String 38 | word = "Python" 39 | 40 | for char in word: 41 | print(char) 42 | ``` 43 | 44 | **Output:** 45 | ``` 46 | P 47 | y 48 | t 49 | h 50 | o 51 | n 52 | ``` 53 | 54 | #### Example 3: Using `range()` Function 55 | 56 | ```python 57 | # Example 3: Using range() Function 58 | for number in range(1, 6): 59 | print(number) 60 | ``` 61 | 62 | **Output:** 63 | ``` 64 | 1 65 | 2 66 | 3 67 | 4 68 | 5 69 | ``` 70 | 71 | #### `range()` Function Explained: 72 | 73 | The `range(start, stop, step)` function generates a sequence of numbers from `start` to `stop - 1`, incrementing by `step`. If `step` is omitted, it defaults to 1. 74 | 75 | #### Example 4: Iterating Over a Dictionary 76 | 77 | ```python 78 | # Example 4: Iterating Over a Dictionary 79 | student_grades = {"Alice": 90, "Bob": 80, "Charlie": 95} 80 | 81 | for name, grade in student_grades.items(): 82 | print(f"{name}'s grade is {grade}") 83 | ``` 84 | 85 | **Output:** 86 | ``` 87 | Alice's grade is 90 88 | Bob's grade is 80 89 | Charlie's grade is 95 90 | ``` 91 | 92 | #### `items()` Method Explained: 93 | 94 | The `items()` method is used with dictionaries to return a view of key-value pairs as tuples. 95 | 96 | #### `break` and `continue` Statements: 97 | 98 | - The `break` statement exits the loop prematurely. 99 | - The `continue` statement skips the rest of the code inside the loop for the current iteration. 100 | 101 | #### Example 5: Using `break` and `continue` 102 | 103 | ```python 104 | # Example 5: Using break and continue 105 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 106 | 107 | for num in numbers: 108 | if num == 5: 109 | break # exit the loop when num is 5 110 | elif num % 2 == 0: 111 | continue # skip the rest of the code for even numbers 112 | print(num) 113 | ``` 114 | 115 | **Output:** 116 | ``` 117 | 1 118 | 3 119 | ``` 120 | 121 | #### `else` Clause in a `for` Loop: 122 | 123 | The `else` clause in a `for` loop is executed when the loop exhausts its iterable (reaches the end). 124 | 125 | #### Example 6: `else` Clause in a `for` Loop 126 | 127 | ```python 128 | # Example 6: else Clause in a for Loop 129 | for i in range(5): 130 | print(i) 131 | else: 132 | print("Loop completed successfully!") 133 | ``` 134 | 135 | **Output:** 136 | ``` 137 | 0 138 | 1 139 | 2 140 | 3 141 | 4 142 | Loop completed successfully! 143 | ``` 144 | 145 | #### Conclusion: 146 | 147 | The `for` loop is a versatile construct in Python, providing a clean and efficient way to iterate over elements in various data structures. Armed with the knowledge of `for` loop syntax, iterable objects, and associated statements, you can handle a wide range of tasks in your Python programs. 148 | 149 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 150 | -------------------------------------------------------------------------------- /Day 02/Python break and continue.md: -------------------------------------------------------------------------------- 1 | # Python break and continue 2 | 3 | #### Introduction: 4 | Welcome to an exploration of the `break` and `continue` statements in Python at CodesWithPankaj.com! These statements provide control over the execution flow within loops. Understanding how to use `break` and `continue` is crucial for efficient loop management. Let's dive into the details. 5 | 6 | #### `break` Statement: 7 | The `break` statement is used to exit a loop prematurely, regardless of whether the loop condition is still true. 8 | 9 | #### Example 1: Using `break` in a `for` Loop 10 | 11 | ```python 12 | # Example 1: Using break in a for Loop 13 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 14 | 15 | for num in numbers: 16 | if num == 5: 17 | break # exit the loop when num is 5 18 | print(num) 19 | ``` 20 | 21 | **Output:** 22 | ``` 23 | 1 24 | 2 25 | 3 26 | 4 27 | ``` 28 | 29 | #### Example 2: Using `break` in a `while` Loop 30 | 31 | ```python 32 | # Example 2: Using break in a while Loop 33 | counter = 1 34 | 35 | while counter <= 5: 36 | print(f"Count: {counter}") 37 | if counter == 3: 38 | break # exit the loop when counter is 3 39 | counter += 1 40 | ``` 41 | 42 | **Output:** 43 | ``` 44 | Count: 1 45 | Count: 2 46 | Count: 3 47 | ``` 48 | 49 | #### `continue` Statement: 50 | The `continue` statement is used to skip the rest of the code inside a loop for the current iteration and move on to the next iteration. 51 | 52 | #### Example 3: Using `continue` in a `for` Loop 53 | 54 | ```python 55 | # Example 3: Using continue in a for Loop 56 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 57 | 58 | for num in numbers: 59 | if num % 2 == 0: 60 | continue # skip the rest of the code for even numbers 61 | print(num) 62 | ``` 63 | 64 | **Output:** 65 | ``` 66 | 1 67 | 3 68 | 5 69 | 7 70 | 9 71 | ``` 72 | 73 | #### Example 4: Using `continue` in a `while` Loop 74 | 75 | ```python 76 | # Example 4: Using continue in a while Loop 77 | counter = 1 78 | 79 | while counter <= 5: 80 | if counter == 3: 81 | counter += 1 82 | continue # skip the rest of the code for counter equal to 3 83 | print(f"Count: {counter}") 84 | counter += 1 85 | ``` 86 | 87 | **Output:** 88 | ``` 89 | Count: 1 90 | Count: 2 91 | Count: 4 92 | Count: 5 93 | ``` 94 | 95 | #### Conclusion: 96 | 97 | The `break` and `continue` statements are powerful tools for controlling the flow of loops in Python. While `break` allows you to exit a loop prematurely, `continue` lets you skip the remaining code for the current iteration and move on to the next one. These statements contribute to writing cleaner and more efficient code. 98 | 99 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 100 | -------------------------------------------------------------------------------- /Day 02/Python if...else Statement.md: -------------------------------------------------------------------------------- 1 | # Python if...else Statement 2 | 3 | ### Tutorial: Python `if...else` Statement 4 | 5 | #### Introduction: 6 | Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the `if...else` statement in Python. The `if...else` statement allows you to make decisions in your code based on conditions. It's a fundamental building block for creating flexible and responsive programs. Let's dive into the world of `if...else` in Python! 7 | 8 | #### Basic `if...else` Structure: 9 | 10 | The `if...else` statement has the following structure: 11 | 12 | ```python 13 | if condition: 14 | # Code to execute if the condition is True 15 | else: 16 | # Code to execute if the condition is False 17 | ``` 18 | 19 | #### Example 1: Simple Comparison: 20 | 21 | ```python 22 | # Example 1: Simple Comparison 23 | x = 10 24 | 25 | if x > 5: 26 | print("x is greater than 5") 27 | else: 28 | print("x is not greater than 5") 29 | ``` 30 | 31 | #### Example 2: User Input: 32 | 33 | ```python 34 | # Example 2: User Input 35 | user_age = int(input("Enter your age: ")) 36 | 37 | if user_age >= 18: 38 | print("You are eligible to vote.") 39 | else: 40 | print("You are not eligible to vote.") 41 | ``` 42 | 43 | #### Example 3: Multiple Conditions: 44 | 45 | ```python 46 | # Example 3: Multiple Conditions 47 | num = int(input("Enter a number: ")) 48 | 49 | if num > 0: 50 | print("The number is positive.") 51 | elif num == 0: 52 | print("The number is zero.") 53 | else: 54 | print("The number is negative.") 55 | ``` 56 | 57 | #### Example 4: Nested `if...else`: 58 | 59 | ```python 60 | # Example 4: Nested if...else 61 | num = int(input("Enter a number: ")) 62 | 63 | if num >= 0: 64 | if num == 0: 65 | print("The number is zero.") 66 | else: 67 | print("The number is positive.") 68 | else: 69 | print("The number is negative.") 70 | ``` 71 | 72 | #### Conclusion: 73 | 74 | The `if...else` statement is a powerful tool for controlling the flow of your Python programs based on different conditions. Whether you're making simple comparisons or handling complex scenarios with multiple conditions, mastering the `if...else` statement is essential for writing dynamic and responsive code. 75 | 76 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 77 | -------------------------------------------------------------------------------- /Day 02/Python pass Statement.md: -------------------------------------------------------------------------------- 1 | # Python pass Statement 2 | 3 | #### Introduction: 4 | Welcome to an exploration of the `pass` statement in Python at CodesWithPankaj.com! The `pass` statement is a null operation or a no-op. It serves as a placeholder in situations where syntactically some code is required, but no action is desired. Let's understand how to use the `pass` statement effectively. 5 | 6 | #### Basic Syntax: 7 | The basic syntax of the `pass` statement is straightforward: 8 | 9 | ```python 10 | if condition: 11 | pass # No action, simply move on to the next statement 12 | ``` 13 | 14 | #### Example 1: Using `pass` in an `if` Statement 15 | 16 | ```python 17 | # Example 1: Using pass in an if Statement 18 | x = 10 19 | 20 | if x > 5: 21 | pass # No action needed for this condition 22 | else: 23 | print("x is not greater than 5") 24 | ``` 25 | 26 | In this example, the `pass` statement is used in the `if` block where no action is needed. If the condition is true, the `pass` statement allows the program to move to the next block of code. 27 | 28 | #### Example 2: Using `pass` in a Function 29 | 30 | ```python 31 | # Example 2: Using pass in a Function 32 | def my_function(): 33 | # To be implemented later 34 | pass 35 | 36 | # Rest of the code 37 | print("Function executed successfully!") 38 | ``` 39 | 40 | In this example, the `pass` statement is used as a placeholder within a function. It allows you to define the function without implementing its content immediately. This can be useful when you're in the early stages of code development and want to outline your functions before filling in the details. 41 | 42 | #### Example 3: Using `pass` in a Loop 43 | 44 | ```python 45 | # Example 3: Using pass in a Loop 46 | for i in range(5): 47 | # To be implemented later 48 | pass 49 | 50 | # Rest of the code 51 | print("Loop executed successfully!") 52 | ``` 53 | 54 | Similarly, in a loop, the `pass` statement can be used when you need a placeholder for future code implementation. 55 | 56 | #### Conclusion: 57 | 58 | The `pass` statement is a handy tool in Python when you need a syntactical placeholder but no action is required. It's particularly useful when defining functions, loops, or conditional statements where you want to provide a structure without immediate content. 59 | 60 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 61 | -------------------------------------------------------------------------------- /Day 02/Python while Loop.md: -------------------------------------------------------------------------------- 1 | # Python while Loop 2 | 3 | #### Introduction: 4 | Welcome to an in-depth exploration of the `while` loop in Python at CodesWithPankaj.com! The `while` loop is a powerful construct that allows you to repeatedly execute a block of code as long as a specified condition is true. Let's delve into the details of the `while` loop. 5 | 6 | #### Basic Syntax: 7 | The basic syntax of the `while` loop is as follows: 8 | 9 | ```python 10 | while condition: 11 | # Code to be executed as long as the condition is true 12 | ``` 13 | 14 | - `condition`: Represents the expression to be evaluated. The loop continues as long as this condition is true. 15 | 16 | #### Example 1: Simple `while` Loop 17 | 18 | ```python 19 | # Example 1: Simple while Loop 20 | counter = 1 21 | 22 | while counter <= 5: 23 | print(f"Count: {counter}") 24 | counter += 1 25 | ``` 26 | 27 | **Output:** 28 | ``` 29 | Count: 1 30 | Count: 2 31 | Count: 3 32 | Count: 4 33 | Count: 5 34 | ``` 35 | 36 | #### Example 2: `break` and `continue` in a `while` Loop 37 | 38 | ```python 39 | # Example 2: break and continue in a while Loop 40 | number = 1 41 | 42 | while number <= 10: 43 | if number == 5: 44 | break # exit the loop when number is 5 45 | elif number % 2 == 0: 46 | number += 1 47 | continue # skip the rest of the code for even numbers 48 | print(number) 49 | number += 1 50 | ``` 51 | 52 | **Output:** 53 | ``` 54 | 1 55 | 3 56 | ``` 57 | 58 | #### `break` and `continue` Statements in a `while` Loop: 59 | 60 | - The `break` statement exits the loop prematurely. 61 | - The `continue` statement skips the rest of the code inside the loop for the current iteration. 62 | 63 | #### Example 3: `else` Clause in a `while` Loop 64 | 65 | The `else` clause in a `while` loop is executed when the loop condition becomes false. 66 | 67 | ```python 68 | # Example 3: else Clause in a while Loop 69 | counter = 1 70 | 71 | while counter <= 5: 72 | print(f"Count: {counter}") 73 | counter += 1 74 | else: 75 | print("Loop completed successfully!") 76 | ``` 77 | 78 | **Output:** 79 | ``` 80 | Count: 1 81 | Count: 2 82 | Count: 3 83 | Count: 4 84 | Count: 5 85 | Loop completed successfully! 86 | ``` 87 | 88 | #### Infinite `while` Loop: 89 | 90 | Be cautious when using `while` loops to avoid creating infinite loops. An infinite loop runs indefinitely unless there is a mechanism to break out of it. 91 | 92 | #### Example 4: Infinite `while` Loop 93 | 94 | ```python 95 | # Example 4: Infinite while Loop 96 | while True: 97 | user_input = input("Enter 'exit' to break the loop: ") 98 | if user_input.lower() == 'exit': 99 | break 100 | ``` 101 | 102 | #### Nested `while` Loops: 103 | 104 | Just like `for` loops, `while` loops can be nested to handle more complex scenarios. 105 | 106 | #### Example 5: Nested `while` Loop 107 | 108 | ```python 109 | # Example 5: Nested while Loop 110 | outer_counter = 1 111 | 112 | while outer_counter <= 3: 113 | inner_counter = 1 114 | while inner_counter <= 2: 115 | print(f"Outer: {outer_counter}, Inner: {inner_counter}") 116 | inner_counter += 1 117 | outer_counter += 1 118 | ``` 119 | 120 | **Output:** 121 | ``` 122 | Outer: 1, Inner: 1 123 | Outer: 1, Inner: 2 124 | Outer: 2, Inner: 1 125 | Outer: 2, Inner: 2 126 | Outer: 3, Inner: 1 127 | Outer: 3, Inner: 2 128 | ``` 129 | 130 | #### Conclusion: 131 | 132 | The `while` loop is a dynamic construct in Python, allowing you to repeatedly execute code as long as a specified condition remains true. With a solid understanding of the `while` loop syntax, the use of `break` and `continue` statements, and the inclusion of an `else` clause, you can handle various looping scenarios in your Python programs. 133 | 134 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 135 | -------------------------------------------------------------------------------- /Day 03/Python Functions.md: -------------------------------------------------------------------------------- 1 | # Python Functions 2 | 3 | #### Introduction: 4 | Welcome to an in-depth exploration of functions in Python at CodesWithPankaj.com! Functions are blocks of organized, reusable code designed to perform a specific task. They provide a way to structure code, promote code reusability, and enhance maintainability. Let's delve into the details of Python functions. 5 | 6 | #### Basic Syntax: 7 | The basic syntax of a function in Python is as follows: 8 | 9 | ```python 10 | def function_name(parameters): 11 | """ 12 | Docstring: Description of the function 13 | """ 14 | # Code block 15 | return result # Optional return statement 16 | ``` 17 | 18 | - `def`: Keyword indicating the start of a function definition. 19 | - `function_name`: Name of the function. 20 | - `parameters`: Input parameters the function takes (if any). 21 | - `Docstring`: Optional documentation describing the function's purpose and usage. 22 | - `return`: Optional statement to return a value from the function. 23 | 24 | #### Example 1: Simple Function 25 | 26 | ```python 27 | # Example 1: Simple Function 28 | def greet(name): 29 | """This function greets the person passed in as a parameter.""" 30 | print("Hello, " + name + "!") 31 | 32 | # Function call 33 | greet("Alice") 34 | ``` 35 | 36 | **Output:** 37 | ``` 38 | Hello, Alice! 39 | ``` 40 | 41 | #### Parameters and Arguments: 42 | 43 | - **Parameters:** Variables that are used in a function definition. 44 | - **Arguments:** Values passed to a function when it is called. 45 | 46 | #### Example 2: Function with Multiple Parameters 47 | 48 | ```python 49 | # Example 2: Function with Multiple Parameters 50 | def add_numbers(x, y): 51 | """This function adds two numbers.""" 52 | result = x + y 53 | return result 54 | 55 | # Function call 56 | sum_result = add_numbers(5, 7) 57 | print("Sum:", sum_result) 58 | ``` 59 | 60 | **Output:** 61 | ``` 62 | Sum: 12 63 | ``` 64 | 65 | #### Default Parameters: 66 | 67 | You can assign default values to parameters in a function. 68 | 69 | #### Example 3: Function with Default Parameter 70 | 71 | ```python 72 | # Example 3: Function with Default Parameter 73 | def greet_person(name, greeting="Hello"): 74 | """This function greets a person with an optional custom greeting.""" 75 | print(greeting + ", " + name + "!") 76 | 77 | # Function calls 78 | greet_person("Bob") 79 | greet_person("Charlie", "Good morning") 80 | ``` 81 | 82 | **Output:** 83 | ``` 84 | Hello, Bob! 85 | Good morning, Charlie! 86 | ``` 87 | 88 | #### Variable-Length Arguments: 89 | 90 | You can use `*args` to pass a variable number of non-keyword arguments to a function. 91 | 92 | #### Example 4: Function with Variable-Length Arguments 93 | 94 | ```python 95 | # Example 4: Function with Variable-Length Arguments 96 | def calculate_sum(*args): 97 | """This function calculates the sum of a variable number of arguments.""" 98 | return sum(args) 99 | 100 | # Function calls 101 | result1 = calculate_sum(1, 2, 3) 102 | result2 = calculate_sum(10, 20, 30, 40) 103 | 104 | print("Sum 1:", result1) 105 | print("Sum 2:", result2) 106 | ``` 107 | 108 | **Output:** 109 | ``` 110 | Sum 1: 6 111 | Sum 2: 100 112 | ``` 113 | 114 | #### Keyword Arguments: 115 | 116 | You can use `**kwargs` to pass a variable number of keyword arguments to a function. 117 | 118 | #### Example 5: Function with Keyword Arguments 119 | 120 | ```python 121 | # Example 5: Function with Keyword Arguments 122 | def display_info(**kwargs): 123 | """This function displays information using keyword arguments.""" 124 | for key, value in kwargs.items(): 125 | print(key + ": " + value) 126 | 127 | # Function calls 128 | display_info(name="Alice", age="25", country="USA") 129 | display_info(name="Bob", profession="Engineer") 130 | ``` 131 | 132 | **Output:** 133 | ``` 134 | name: Alice 135 | age: 25 136 | country: USA 137 | name: Bob 138 | profession: Engineer 139 | ``` 140 | 141 | #### Return Statement: 142 | 143 | A function can use the `return` statement to send a result back to the caller. 144 | 145 | #### Example 6: Function with Return Statement 146 | 147 | ```python 148 | # Example 6: Function with Return Statement 149 | def square(number): 150 | """This function calculates the square of a number.""" 151 | return number ** 2 152 | 153 | # Function call 154 | result = square(4) 155 | print("Square:", result) 156 | ``` 157 | 158 | **Output:** 159 | ``` 160 | Square: 16 161 | ``` 162 | 163 | #### Global and Local Variables: 164 | 165 | Variables declared inside a function have local scope, while those declared outside have global scope. 166 | 167 | #### Example 7: Global and Local Variables 168 | 169 | ```python 170 | # Example 7: Global and Local Variables 171 | global_variable = "I am global!" 172 | 173 | def my_function(): 174 | local_variable = "I am local!" 175 | print(global_variable) 176 | print(local_variable) 177 | 178 | # Function call 179 | my_function() 180 | 181 | # Accessing global_variable outside the function 182 | print("Outside function:", global_variable) 183 | 184 | # Trying to access local_variable outside the function will result in an error 185 | # print("Outside function:", local_variable) 186 | ``` 187 | 188 | **Output:** 189 | ``` 190 | I am global! 191 | I am local! 192 | Outside function: I am global! 193 | ``` 194 | 195 | #### Lambda Functions (Anonymous Functions): 196 | 197 | Lambda functions are small, anonymous functions defined using the `lambda` keyword. 198 | 199 | #### Example 8: Lambda Function 200 | 201 | ```python 202 | # Example 8: Lambda Function 203 | multiply = lambda x, y: x * y 204 | 205 | # Function call 206 | result = multiply(3, 4) 207 | print("Multiplication:", result) 208 | ``` 209 | 210 | **Output:** 211 | ``` 212 | Multiplication: 12 213 | ``` 214 | 215 | #### Conclusion: 216 | 217 | Functions in Python provide a powerful way to organize and structure code. They enhance code readability, reusability, and maintainability. With the ability to define parameters, default values, variable-length arguments, and keyword arguments, Python functions offer flexibility for various programming needs. 218 | 219 | For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding! 220 | -------------------------------------------------------------------------------- /G_Tec_Jain_Keerti_Education_Employee_Training_.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMnFEXsfPDqKiOSE/Zv2q5p", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# Getting started with Python is a great idea, as it's a versatile and beginner-friendly programming language.\n", 33 | "### Here's a step-by-step tutorial on how to install Python and get started with your first program:\n", 34 | "\n", 35 | "### Step 1: Install Python\n", 36 | "\n", 37 | "1. **Download Python:**\n", 38 | " - Visit the official Python website: [Python Downloads](https://www.python.org/downloads/).\n", 39 | " - The website should automatically suggest the latest version suitable for your operating system (Windows, macOS, or Linux). Click on the \"Download\" button.\n", 40 | "\n", 41 | "2. **Run the Installer:**\n", 42 | " - For Windows: Run the downloaded executable file and follow the installation wizard's instructions.\n", 43 | " - For macOS: Open the downloaded `.pkg` file and follow the installation instructions.\n", 44 | " - For Linux: Python is often pre-installed. If not, use your package manager (e.g., `apt` for Ubuntu) to install Python.\n", 45 | "\n", 46 | "3. **Check Installation:**\n", 47 | " - Open a command prompt (Windows) or terminal (macOS/Linux).\n", 48 | " - Type `python --version` or `python3 --version` and press Enter. You should see the installed Python version.\n", 49 | "\n", 50 | "### Step 2: Set Up a Development Environment\n", 51 | "\n", 52 | "1. **Text Editor or IDE:**\n", 53 | " - Choose a code editor or integrated development environment (IDE). Some popular choices include:\n", 54 | " - VSCode\n", 55 | " - PyCharm\n", 56 | " - Atom\n", 57 | " - Sublime Text\n", 58 | " - IDLE (comes with Python)\n", 59 | "\n", 60 | "### Step 3: Write Your First Python Program\n", 61 | "\n", 62 | "1. **Open Your Text Editor/IDE:**\n", 63 | " - Launch your chosen editor or IDE.\n", 64 | "\n", 65 | "2. **Create a New File:**\n", 66 | " - Open a new file in your editor.\n", 67 | "\n", 68 | "3. **Write Your First Program:**\n", 69 | " - Type the following simple Python program:\n", 70 | " ```python\n", 71 | " print(\"Hello, Python!\")\n", 72 | " ```\n", 73 | "\n", 74 | "4. **Save the File:**\n", 75 | " - Save the file with a `.py` extension, for example, `first_program.py`.\n", 76 | "\n", 77 | "5. **Run the Program:**\n", 78 | " - Open a terminal or command prompt in the same directory where you saved your Python file.\n", 79 | " - Type `python first_program.py` (or `python3 first_program.py` on some systems) and press Enter.\n", 80 | " - You should see the output: `Hello, Python!`\n", 81 | "\n", 82 | "Congratulations! You've successfully installed Python, set up a development environment, and written and executed your first Python program.\n", 83 | "\n", 84 | "### Step 4: Learn and Explore\n", 85 | "\n", 86 | "1. **Official Python Documentation:**\n", 87 | " - Explore the [Python Documentation](https://docs.python.org/3/).\n", 88 | "\n", 89 | "2. **Tutorials and Online Courses:**\n", 90 | " - Utilize online platforms like Codecademy, Coursera, or edX for structured Python courses.\n", 91 | "\n", 92 | "3. **Practice and Experiment:**\n", 93 | " - Try solving coding challenges on platforms like HackerRank or LeetCode.\n", 94 | " - Build small projects to apply your knowledge.\n", 95 | "\n", 96 | "Remember that programming is a skill that improves with practice, so keep coding and have fun exploring the world of Python!" 97 | ], 98 | "metadata": { 99 | "id": "MEmmOMZY_N4Y" 100 | } 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "source": [ 105 | "# Python Basic Input and Output\n", 106 | "\n", 107 | "#### Introduction:\n", 108 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the fundamental concepts of input and output in Python. Input allows users to provide data to a program, while output enables the program to display information to the user. Let's dive into the world of basic input and output in Python!\n", 109 | "\n", 110 | "#### Basic Output (print function):\n", 111 | "\n", 112 | "The `print()` function is used to display output in Python.\n", 113 | "\n", 114 | "```python\n", 115 | "# Basic Output\n", 116 | "print(\"Hello, Python!\")\n", 117 | "```\n", 118 | "\n", 119 | "#### Formatted Output:\n", 120 | "\n", 121 | "You can format output using the `format()` method or f-strings.\n", 122 | "\n", 123 | "```python\n", 124 | "# Formatted Output\n", 125 | "name = \"Pankaj\"\n", 126 | "age = 25\n", 127 | "print(\"My name is {} and I am {} years old.\".format(name, age))\n", 128 | "\n", 129 | "# Using f-string (Python 3.6 and above)\n", 130 | "print(f\"My name is {name} and I am {age} years old.\")\n", 131 | "```\n", 132 | "\n", 133 | "#### Basic Input (input function):\n", 134 | "\n", 135 | "The `input()` function is used to take user input. It returns a string that can be converted to the desired data type.\n", 136 | "\n", 137 | "```python\n", 138 | "# Basic Input\n", 139 | "user_input = input(\"Enter your name: \")\n", 140 | "print(\"Hello, \" + user_input + \"!\")\n", 141 | "```\n", 142 | "\n", 143 | "#### Converting Input to Numeric Types:\n", 144 | "\n", 145 | "You can convert user input to numeric types (int, float) using type casting.\n", 146 | "\n", 147 | "```python\n", 148 | "# Converting Input to Numeric Types\n", 149 | "age_input = input(\"Enter your age: \")\n", 150 | "age = int(age_input) # Convert the input to an integer\n", 151 | "print(\"Next year, you'll be\", age + 1, \"years old.\")\n", 152 | "```\n", 153 | "\n", 154 | "#### Example Code:\n", 155 | "\n", 156 | "Now, let's create a Python script that combines basic input and output:\n", 157 | "\n", 158 | "```python\n", 159 | "# codeswithpankaj_input_output.py\n", 160 | "\n", 161 | "# Basic Output\n", 162 | "print(\"Welcome to CodesWithPankaj.com!\")\n", 163 | "\n", 164 | "# Formatted Output\n", 165 | "name = input(\"Enter your name: \")\n", 166 | "age = int(input(\"Enter your age: \"))\n", 167 | "print(\"Hello, {}! You are {} years old.\".format(name, age))\n", 168 | "\n", 169 | "# Basic Input\n", 170 | "user_input = input(\"Enter any message: \")\n", 171 | "print(\"You entered:\", user_input)\n", 172 | "```\n", 173 | "\n", 174 | "#### Conclusion:\n", 175 | "\n", 176 | "Understanding basic input and output is essential for creating interactive and user-friendly Python programs. By incorporating these concepts, you can build applications that respond to user input and provide meaningful output.\n", 177 | "\n", 178 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 179 | ], 180 | "metadata": { 181 | "id": "tqyWHNh4APyq" 182 | } 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "source": [ 187 | "\n", 188 | "# Python Comments\n", 189 | "\n", 190 | "#### Introduction:\n", 191 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the importance of comments in Python programming. Comments are essential for documenting your code, making it more readable, and helping others (or even yourself) understand your thought process. Let's dive into the world of Python comments!\n", 192 | "\n", 193 | "#### Types of Comments:\n", 194 | "\n", 195 | "Python supports two types of comments:\n", 196 | "\n", 197 | "1. **Single-line comments:** These comments are created using the `#` symbol and are used for commenting on a single line.\n", 198 | "\n", 199 | " Example:\n", 200 | " ```python\n", 201 | " # This is a single-line comment\n", 202 | " print(\"Hello, Python!\")\n", 203 | " ```\n", 204 | "\n", 205 | "2. **Multi-line comments (Docstrings):** These comments span multiple lines and are enclosed within triple-quotes (`'''` or `\"\"\"`). They are often used as docstrings to provide documentation for functions, modules, or classes.\n", 206 | "\n", 207 | " Example:\n", 208 | " ```python\n", 209 | " '''\n", 210 | " This is a multi-line comment (docstring).\n", 211 | " It provides documentation for the following function.\n", 212 | " '''\n", 213 | "\n", 214 | " def my_function():\n", 215 | " \"\"\"This is also a docstring for the function.\"\"\"\n", 216 | " print(\"Doing something...\")\n", 217 | " ```\n", 218 | "\n", 219 | "#### Best Practices for Using Comments:\n", 220 | "\n", 221 | "1. **Be Clear and Concise:**\n", 222 | " - Clearly explain complex sections or algorithms.\n", 223 | " - Avoid unnecessary comments that merely restate the code.\n", 224 | "\n", 225 | "2. **Update Comments Regularly:**\n", 226 | " - Keep comments up-to-date with code changes.\n", 227 | " - Outdated comments can lead to confusion.\n", 228 | "\n", 229 | "3. **Use Descriptive Variable and Function Names:**\n", 230 | " - Well-named variables and functions reduce the need for excessive comments.\n", 231 | "\n", 232 | "4. **Docstrings for Functions and Classes:**\n", 233 | " - Include docstrings to describe the purpose, parameters, and return values of functions and classes.\n", 234 | "\n", 235 | "#### Example Code with Comments:\n", 236 | "\n", 237 | "Now, let's enhance a simple Python script with comments to explain the code:\n", 238 | "\n", 239 | "```python\n", 240 | "# codeswithpankaj_comments.py\n", 241 | "\n", 242 | "# This script calculates the sum of two numbers\n", 243 | "\n", 244 | "# Function to add two numbers\n", 245 | "def add_numbers(a, b):\n", 246 | " \"\"\"\n", 247 | " This function takes two parameters and returns their sum.\n", 248 | " :param a: The first number\n", 249 | " :param b: The second number\n", 250 | " :return: The sum of a and b\n", 251 | " \"\"\"\n", 252 | " result = a + b\n", 253 | " return result\n", 254 | "\n", 255 | "# Input values\n", 256 | "num1 = 5\n", 257 | "num2 = 7\n", 258 | "\n", 259 | "# Calculate the sum using the function\n", 260 | "sum_result = add_numbers(num1, num2)\n", 261 | "\n", 262 | "# Display the result\n", 263 | "print(f\"The sum of {num1} and {num2} is: {sum_result}\")\n", 264 | "```\n", 265 | "\n", 266 | "#### Conclusion:\n", 267 | "\n", 268 | "Comments play a vital role in making your code more understandable and maintainable. By incorporating comments effectively, you contribute to a collaborative coding environment.\n", 269 | "\n", 270 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 271 | ], 272 | "metadata": { 273 | "id": "I_kE3zb_AY8J" 274 | } 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "source": [ 279 | "\n", 280 | "# Python Comments\n", 281 | "\n", 282 | "#### Introduction:\n", 283 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the importance of comments in Python programming. Comments are essential for documenting your code, making it more readable, and helping others (or even yourself) understand your thought process. Let's dive into the world of Python comments!\n", 284 | "\n", 285 | "#### Types of Comments:\n", 286 | "\n", 287 | "Python supports two types of comments:\n", 288 | "\n", 289 | "1. **Single-line comments:** These comments are created using the `#` symbol and are used for commenting on a single line.\n", 290 | "\n", 291 | " Example:\n", 292 | " ```python\n", 293 | " # This is a single-line comment\n", 294 | " print(\"Hello, Python!\")\n", 295 | " ```\n", 296 | "\n", 297 | "2. **Multi-line comments (Docstrings):** These comments span multiple lines and are enclosed within triple-quotes (`'''` or `\"\"\"`). They are often used as docstrings to provide documentation for functions, modules, or classes.\n", 298 | "\n", 299 | " Example:\n", 300 | " ```python\n", 301 | " '''\n", 302 | " This is a multi-line comment (docstring).\n", 303 | " It provides documentation for the following function.\n", 304 | " '''\n", 305 | "\n", 306 | " def my_function():\n", 307 | " \"\"\"This is also a docstring for the function.\"\"\"\n", 308 | " print(\"Doing something...\")\n", 309 | " ```\n", 310 | "\n", 311 | "#### Best Practices for Using Comments:\n", 312 | "\n", 313 | "1. **Be Clear and Concise:**\n", 314 | " - Clearly explain complex sections or algorithms.\n", 315 | " - Avoid unnecessary comments that merely restate the code.\n", 316 | "\n", 317 | "2. **Update Comments Regularly:**\n", 318 | " - Keep comments up-to-date with code changes.\n", 319 | " - Outdated comments can lead to confusion.\n", 320 | "\n", 321 | "3. **Use Descriptive Variable and Function Names:**\n", 322 | " - Well-named variables and functions reduce the need for excessive comments.\n", 323 | "\n", 324 | "4. **Docstrings for Functions and Classes:**\n", 325 | " - Include docstrings to describe the purpose, parameters, and return values of functions and classes.\n", 326 | "\n", 327 | "#### Example Code with Comments:\n", 328 | "\n", 329 | "Now, let's enhance a simple Python script with comments to explain the code:\n", 330 | "\n", 331 | "```python\n", 332 | "# codeswithpankaj_comments.py\n", 333 | "\n", 334 | "# This script calculates the sum of two numbers\n", 335 | "\n", 336 | "# Function to add two numbers\n", 337 | "def add_numbers(a, b):\n", 338 | " \"\"\"\n", 339 | " This function takes two parameters and returns their sum.\n", 340 | " :param a: The first number\n", 341 | " :param b: The second number\n", 342 | " :return: The sum of a and b\n", 343 | " \"\"\"\n", 344 | " result = a + b\n", 345 | " return result\n", 346 | "\n", 347 | "# Input values\n", 348 | "num1 = 5\n", 349 | "num2 = 7\n", 350 | "\n", 351 | "# Calculate the sum using the function\n", 352 | "sum_result = add_numbers(num1, num2)\n", 353 | "\n", 354 | "# Display the result\n", 355 | "print(f\"The sum of {num1} and {num2} is: {sum_result}\")\n", 356 | "```\n", 357 | "\n", 358 | "#### Conclusion:\n", 359 | "\n", 360 | "Comments play a vital role in making your code more understandable and maintainable. By incorporating comments effectively, you contribute to a collaborative coding environment.\n", 361 | "\n", 362 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 363 | ], 364 | "metadata": { 365 | "id": "XKIksS5mAdVB" 366 | } 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "source": [ 371 | "# Python Data Types\n", 372 | "\n", 373 | "#### Introduction:\n", 374 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the various data types in Python. Understanding data types is crucial for writing effective and efficient Python code. Let's dive into the world of Python data types!\n", 375 | "\n", 376 | "#### Common Data Types:\n", 377 | "\n", 378 | "Python supports several built-in data types. Here are some of the most common ones:\n", 379 | "\n", 380 | "| Data Types | Classes | Description |\n", 381 | "|------------|-------------------|--------------------------------------------|\n", 382 | "| Numeric | int, float, complex| Holds numeric values |\n", 383 | "| String | str | Holds a sequence of characters |\n", 384 | "| Sequence | list, tuple, range | Holds a collection of items |\n", 385 | "| Mapping | dict | Holds data in key-value pair form |\n", 386 | "| Boolean | bool | Holds either True or False |\n", 387 | "| Set | set, frozenset | Holds a collection of unique items |\n", 388 | "\n", 389 | "1. **Numeric Types:**\n", 390 | " - **int:** Integer type, e.g., `5`, `-10`.\n", 391 | " - **float:** Floating-point type, e.g., `3.14`, `2.0`.\n", 392 | " - **complex:** Complex number type, e.g., `2 + 3j`.\n", 393 | "\n", 394 | " ```python\n", 395 | " # Numeric types\n", 396 | " integer_number = 42\n", 397 | " float_number = 3.14\n", 398 | " complex_number = 2 + 3j\n", 399 | " ```\n", 400 | "\n", 401 | "2. **Sequence Types:**\n", 402 | " - **str:** String type, e.g., `\"Hello, Python!\"`.\n", 403 | " - **list:** List type, e.g., `[1, 2, 3]`.\n", 404 | " - **tuple:** Tuple type, e.g., `(1, 2, 3)`.\n", 405 | "\n", 406 | " ```python\n", 407 | " # Sequence types\n", 408 | " string_data = \"Hello, Python!\"\n", 409 | " list_data = [1, 2, 3]\n", 410 | " tuple_data = (1, 2, 3)\n", 411 | " ```\n", 412 | "\n", 413 | "3. **Boolean Type:**\n", 414 | " - **bool:** Boolean type, either `True` or `False`.\n", 415 | "\n", 416 | " ```python\n", 417 | " # Boolean type\n", 418 | " is_true = True\n", 419 | " is_false = False\n", 420 | " ```\n", 421 | "\n", 422 | "4. **Set Types:**\n", 423 | " - **set:** Unordered collection of unique elements.\n", 424 | " - **frozenset:** Immutable set.\n", 425 | "\n", 426 | " ```python\n", 427 | " # Set types\n", 428 | " set_data = {1, 2, 3}\n", 429 | " frozen_set_data = frozenset({4, 5, 6})\n", 430 | " ```\n", 431 | "\n", 432 | "5. **Mapping Type:**\n", 433 | " - **dict:** Dictionary type, a collection of key-value pairs.\n", 434 | "\n", 435 | " ```python\n", 436 | " # Mapping type\n", 437 | " dictionary_data = {\"name\": \"John\", \"age\": 25, \"city\": \"New York\"}\n", 438 | " ```\n", 439 | "\n", 440 | "6. **None Type:**\n", 441 | " - **None:** Represents the absence of a value.\n", 442 | "\n", 443 | " ```python\n", 444 | " # None type\n", 445 | " no_value = None\n", 446 | " ```\n", 447 | "\n", 448 | "#### Example Code:\n", 449 | "\n", 450 | "Now, let's create a Python script that demonstrates the use of various data types:\n", 451 | "\n", 452 | "```python\n", 453 | "# codeswithpankaj_data_types.py\n", 454 | "\n", 455 | "# Numeric types\n", 456 | "integer_number = 42\n", 457 | "float_number = 3.14\n", 458 | "complex_number = 2 + 3j\n", 459 | "\n", 460 | "# Sequence types\n", 461 | "string_data = \"Hello, Python!\"\n", 462 | "list_data = [1, 2, 3]\n", 463 | "tuple_data = (1, 2, 3)\n", 464 | "\n", 465 | "# Boolean type\n", 466 | "is_true = True\n", 467 | "is_false = False\n", 468 | "\n", 469 | "# Set types\n", 470 | "set_data = {1, 2, 3}\n", 471 | "frozen_set_data = frozenset({4, 5, 6})\n", 472 | "\n", 473 | "# Mapping type\n", 474 | "dictionary_data = {\"name\": \"John\", \"age\": 25, \"city\": \"New York\"}\n", 475 | "\n", 476 | "# None type\n", 477 | "no_value = None\n", 478 | "\n", 479 | "# Displaying values\n", 480 | "print(\"Numeric Types:\", integer_number, float_number, complex_number)\n", 481 | "print(\"Sequence Types:\", string_data, list_data, tuple_data)\n", 482 | "print(\"Boolean Type:\", is_true, is_false)\n", 483 | "print(\"Set Types:\", set_data, frozen_set_data)\n", 484 | "print(\"Mapping Type:\", dictionary_data)\n", 485 | "print(\"None Type:\", no_value)\n", 486 | "```\n", 487 | "\n", 488 | "#### Conclusion:\n", 489 | "\n", 490 | "Understanding Python data types is crucial for writing robust and flexible code. By utilizing the appropriate data type for your variables, you can ensure efficient data manipulation and enhance the overall quality of your programs.\n", 491 | "\n", 492 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 493 | ], 494 | "metadata": { 495 | "id": "6SzGMl2QAiLL" 496 | } 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "source": [ 501 | "\n", 502 | "# Python Keywords and Identifiers\n", 503 | "\n", 504 | "#### Introduction:\n", 505 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore Python keywords and identifiers—essential concepts for understanding Python programming. Whether you're a beginner or looking to reinforce your knowledge, let's dive in.\n", 506 | "\n", 507 | "#### Python Keywords:\n", 508 | "\n", 509 | "Keywords are reserved words in Python that have specific meanings and cannot be used as identifiers (variable names, function names, etc.). Here's a table of Python keywords:\n", 510 | "\n", 511 | "| | | | | |\n", 512 | "|-------|-------|--------|-------|--------|\n", 513 | "| False | class | finally| is | return |\n", 514 | "| None | continue | for | lambda| try |\n", 515 | "| True | def | from | nonlocal | while|\n", 516 | "| and | del | global | not | with |\n", 517 | "| as | elif | if | or | yield |\n", 518 | "| assert| else | import | pass | |\n", 519 | "| break | except| in | raise | |\n", 520 | "\n", 521 | "Feel free to reference this table whenever you encounter a keyword in your Python code.\n", 522 | "\n", 523 | "#### Identifiers:\n", 524 | "\n", 525 | "Identifiers are names given to variables, functions, classes, etc. They follow certain rules:\n", 526 | "\n", 527 | "- Must start with a letter (a-z, A-Z) or an underscore (_).\n", 528 | "- Can be followed by letters, underscores, and digits (0-9).\n", 529 | "- Case-sensitive.\n", 530 | "\n", 531 | "Examples of valid identifiers: `my_variable`, `_count`, `Total2`.\n", 532 | "\n", 533 | "#### Creating Identifiers:\n", 534 | "\n", 535 | "Now, let's write a simple Python script to demonstrate the creation of identifiers:\n", 536 | "\n", 537 | "```python\n", 538 | "# codeswithpankaj_identifiers.py\n", 539 | "\n", 540 | "# Valid identifiers\n", 541 | "my_variable = 42\n", 542 | "_count = 10\n", 543 | "Total2 = 100\n", 544 | "\n", 545 | "# Invalid identifiers - Uncommenting these lines will result in an error\n", 546 | "# 2nd_variable = 20 # starts with a digit\n", 547 | "# my-variable = 5 # contains a hyphen\n", 548 | "# class = \"Python\" # keyword used as an identifier\n", 549 | "\n", 550 | "# Displaying values\n", 551 | "print(\"Valid Identifiers:\")\n", 552 | "print(\"my_variable =\", my_variable)\n", 553 | "print(\"_count =\", _count)\n", 554 | "print(\"Total2 =\", Total2)\n", 555 | "\n", 556 | "# Uncommenting the next line will result in an error\n", 557 | "# print(\"2nd_variable =\", 2nd_variable)\n", 558 | "```\n", 559 | "\n", 560 | "This script demonstrates valid and invalid identifiers. Uncomment the invalid lines to observe the errors they produce.\n", 561 | "\n", 562 | "#### Conclusion:\n", 563 | "\n", 564 | "Understanding Python keywords and identifiers is crucial for writing clean, error-free code. Now that you've grasped these concepts, you're well on your way to becoming a proficient Python coder!\n", 565 | "\n", 566 | "Stay tuned for more tutorials and coding tips at [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 567 | ], 568 | "metadata": { 569 | "id": "aVXvCsbUAmMx" 570 | } 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "source": [ 575 | "\n", 576 | "#### Introduction:\n", 577 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the various operators in Python. Operators are symbols that perform operations on variables and values. Understanding these operators is fundamental for writing effective Python code. Let's dive into the world of Python operators!\n", 578 | "\n", 579 | "#### Arithmetic Operators:\n", 580 | "\n", 581 | "1. **Addition (`+`):** Adds two operands.\n", 582 | "2. **Subtraction (`-`):** Subtracts the right operand from the left operand.\n", 583 | "3. **Multiplication (`*`):** Multiplies two operands.\n", 584 | "4. **Division (`/`):** Divides the left operand by the right operand.\n", 585 | "5. **Modulus (`%`):** Returns the remainder of the division.\n", 586 | "6. **Exponentiation (`**`):** Raises the left operand to the power of the right operand.\n", 587 | "7. **Floor Division (`//`):** Returns the floor of the division (quotient without remainder).\n", 588 | "\n", 589 | "```python\n", 590 | "# Arithmetic Operators\n", 591 | "a = 10\n", 592 | "b = 3\n", 593 | "\n", 594 | "addition_result = a + b\n", 595 | "subtraction_result = a - b\n", 596 | "multiplication_result = a * b\n", 597 | "division_result = a / b\n", 598 | "modulus_result = a % b\n", 599 | "exponentiation_result = a ** b\n", 600 | "floor_division_result = a // b\n", 601 | "\n", 602 | "print(\"Addition:\", addition_result)\n", 603 | "print(\"Subtraction:\", subtraction_result)\n", 604 | "print(\"Multiplication:\", multiplication_result)\n", 605 | "print(\"Division:\", division_result)\n", 606 | "print(\"Modulus:\", modulus_result)\n", 607 | "print(\"Exponentiation:\", exponentiation_result)\n", 608 | "print(\"Floor Division:\", floor_division_result)\n", 609 | "```\n", 610 | "\n", 611 | "#### Comparison Operators:\n", 612 | "\n", 613 | "1. **Equal to (`==`):** True if both operands are equal.\n", 614 | "2. **Not equal to (`!=`):** True if operands are not equal.\n", 615 | "3. **Greater than (`>`):** True if the left operand is greater than the right operand.\n", 616 | "4. **Less than (`<`):** True if the left operand is less than the right operand.\n", 617 | "5. **Greater than or equal to (`>=`):** True if the left operand is greater than or equal to the right operand.\n", 618 | "6. **Less than or equal to (`<=`):** True if the left operand is less than or equal to the right operand.\n", 619 | "\n", 620 | "```python\n", 621 | "# Comparison Operators\n", 622 | "x = 5\n", 623 | "y = 8\n", 624 | "\n", 625 | "equal_result = x == y\n", 626 | "not_equal_result = x != y\n", 627 | "greater_than_result = x > y\n", 628 | "less_than_result = x < y\n", 629 | "greater_equal_result = x >= y\n", 630 | "less_equal_result = x <= y\n", 631 | "\n", 632 | "print(\"Equal:\", equal_result)\n", 633 | "print(\"Not Equal:\", not_equal_result)\n", 634 | "print(\"Greater Than:\", greater_than_result)\n", 635 | "print(\"Less Than:\", less_than_result)\n", 636 | "print(\"Greater Than or Equal To:\", greater_equal_result)\n", 637 | "print(\"Less Than or Equal To:\", less_equal_result)\n", 638 | "```\n", 639 | "\n", 640 | "#### Logical Operators:\n", 641 | "\n", 642 | "1. **Logical AND (`and`):** True if both operands are true.\n", 643 | "2. **Logical OR (`or`):** True if at least one operand is true.\n", 644 | "3. **Logical NOT (`not`):** True if the operand is false.\n", 645 | "\n", 646 | "```python\n", 647 | "# Logical Operators\n", 648 | "p = True\n", 649 | "q = False\n", 650 | "\n", 651 | "and_result = p and q\n", 652 | "or_result = p or q\n", 653 | "not_result_p = not p\n", 654 | "not_result_q = not q\n", 655 | "\n", 656 | "print(\"Logical AND:\", and_result)\n", 657 | "print(\"Logical OR:\", or_result)\n", 658 | "print(\"Logical NOT (p):\", not_result_p)\n", 659 | "print(\"Logical NOT (q):\", not_result_q)\n", 660 | "```\n", 661 | "\n", 662 | "#### Assignment Operators:\n", 663 | "\n", 664 | "1. **Assignment (`=`):** Assigns the value on the right to the variable on the left.\n", 665 | "2. **Addition Assignment (`+=`):** Adds the right operand to the left operand and assigns the result to the left operand.\n", 666 | "3. **Subtraction Assignment (`-=`):** Subtracts the right operand from the left operand and assigns the result to the left operand.\n", 667 | "4. **Multiplication Assignment (`*=`):** Multiplies the left operand by the right operand and assigns the result to the left operand.\n", 668 | "5. **Division Assignment (`/=`):** Divides the left operand by the right operand and assigns the result to the left operand.\n", 669 | "6. **Modulus Assignment (`%=`):** Computes the modulus of the left operand with the right operand and assigns the result to the left operand.\n", 670 | "7. **Exponentiation Assignment (`**=`):** Raises the left operand to the power of the right operand and assigns the result to the left operand.\n", 671 | "8. **Floor Division Assignment (`//=`):** Computes the floor division of the left operand by the right operand and assigns the result to the left operand.\n", 672 | "\n", 673 | "```python\n", 674 | "# Assignment Operators\n", 675 | "x = 10\n", 676 | "y = 3\n", 677 | "\n", 678 | "# Basic assignment\n", 679 | "a = x\n", 680 | "\n", 681 | "# Addition assignment\n", 682 | "x += y # equivalent to x = x + y\n", 683 | "\n", 684 | "# Subtraction assignment\n", 685 | "x -= y # equivalent to x = x - y\n", 686 | "\n", 687 | "# Multiplication assignment\n", 688 | "x *= y # equivalent to x = x * y\n", 689 | "\n", 690 | "# Division assignment\n", 691 | "x /= y # equivalent to x = x / y\n", 692 | "\n", 693 | "# Modulus assignment\n", 694 | "x %= y # equivalent to x = x % y\n", 695 | "\n", 696 | "# Exponentiation assignment\n", 697 | "x **= y # equivalent to x = x ** y\n", 698 | "\n", 699 | "# Floor Division assignment\n", 700 | "x //= y # equivalent to x = x // y\n", 701 | "\n", 702 | "print(\"Basic Assignment:\", a)\n", 703 | "print(\"Addition Assignment:\", x)\n", 704 | "```\n", 705 | "\n", 706 | "#### Identity Operators:\n", 707 | "\n", 708 | "1. **Identity (`is`):** True if both operands refer to the same object.\n", 709 | "2. **Not Identity (`is not`):** True if both operands do not refer to the same object.\n", 710 | "\n", 711 | "```python\n", 712 | "# Identity Operators\n", 713 | "list1 = [1, 2, 3]\n", 714 | "list2 = [1, 2, 3]\n", 715 | "list3 = list1\n", 716 | "\n", 717 | "is_result = list1 is list2\n", 718 | "is_not_result = list1 is not list3\n", 719 | "\n", 720 | "print(\"Identity (is):\", is_result)\n", 721 | "print(\"Not Identity (is not):\", is_not_result)\n", 722 | "```\n", 723 | "\n", 724 | "#### Membership Operators:\n", 725 | "\n", 726 | "1. **Membership (`in`):** True if a value is found in the sequence.\n", 727 | "2. **Not Membership (`not in`):** True if a value is not found in the sequence.\n", 728 | "\n", 729 | "```python\n", 730 | "# Membership Operators\n", 731 | "numbers = [1, 2, 3, 4, 5]\n", 732 | "\n", 733 | "in_result = 3 in numbers\n", 734 | "not_in_result = 6 not in numbers\n", 735 | "\n", 736 | "print(\"Membership (in):\", in_result)\n", 737 | "print(\"Not Membership (not in):\", not_in_result)\n", 738 | "```\n", 739 | "\n", 740 | "#### Bitwise Operators:\n", 741 | "\n", 742 | "1. **Bitwise AND (`&`):** Performs bitwise AND.\n", 743 | "2. **Bitwise OR (`|`):** Performs bitwise OR.\n", 744 | "3. **Bitwise XOR (`^`):** Performs bitwise XOR.\n", 745 | "4. **Bitwise NOT (`~`):** Inverts the bits of a number.\n", 746 | "5. **Left Shift (`<<`):** Shifts the bits to the left.\n", 747 | "6. **Right Shift (`>>`):** Shifts the bits to the right.\n", 748 | "\n", 749 | "```python\n", 750 | "# Bitwise Operators\n", 751 | "a = 5 # binary: 0101\n", 752 | "b = 3 # binary: 001\n", 753 | "\n", 754 | "1\n", 755 | "\n", 756 | "bitwise_and_result = a & b # binary: 0001 (decimal: 1)\n", 757 | "bitwise_or_result = a | b # binary: 0111 (decimal: 7)\n", 758 | "bitwise_xor_result = a ^ b # binary: 0110 (decimal: 6)\n", 759 | "bitwise_not_result = ~a # binary: 1010 (decimal: -6, due to two's complement representation)\n", 760 | "left_shift_result = a << 1 # binary: 1010 (decimal: 10)\n", 761 | "right_shift_result = a >> 1 # binary: 0010 (decimal: 2)\n", 762 | "\n", 763 | "print(\"Bitwise AND:\", bitwise_and_result)\n", 764 | "print(\"Bitwise OR:\", bitwise_or_result)\n", 765 | "print(\"Bitwise XOR:\", bitwise_xor_result)\n", 766 | "print(\"Bitwise NOT:\", bitwise_not_result)\n", 767 | "print(\"Left Shift:\", left_shift_result)\n", 768 | "print(\"Right Shift:\", right_shift_result)\n", 769 | "```\n", 770 | "\n", 771 | "#### Conclusion:\n", 772 | "\n", 773 | "Understanding Python operators is crucial for performing various operations on data. By mastering these operators, you gain the ability to manipulate values, compare conditions, and perform bitwise operations in your Python programs.\n", 774 | "\n", 775 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 776 | ], 777 | "metadata": { 778 | "id": "c-wj8VsZAuXh" 779 | } 780 | }, 781 | { 782 | "cell_type": "markdown", 783 | "source": [ 784 | "# Python Type Conversion\n", 785 | "\n", 786 | "\n", 787 | "#### Introduction:\n", 788 | "Welcome to CodesWithPankaj.com! In this tutorial, we'll explore the concept of type conversion in Python. Type conversion, also known as type casting, is the process of converting one data type into another. Python provides built-in functions for this purpose, allowing you to work seamlessly with different types of data. Let's dive into the world of Python type conversion!\n", 789 | "\n", 790 | "#### Implicit vs. Explicit Type Conversion:\n", 791 | "\n", 792 | "1. **Implicit Type Conversion (Coercion):**\n", 793 | " - Python automatically converts one data type to another.\n", 794 | " - It occurs during operations between different data types.\n", 795 | "\n", 796 | " ```python\n", 797 | " # Implicit Type Conversion\n", 798 | " int_number = 10\n", 799 | " float_number = 3.5\n", 800 | " \n", 801 | " result = int_number + float_number # int_number is implicitly converted to float\n", 802 | " ```\n", 803 | "\n", 804 | "2. **Explicit Type Conversion (Type Casting):**\n", 805 | " - You explicitly convert a data type using predefined functions.\n", 806 | " - Common functions: `int()`, `float()`, `str()`, etc.\n", 807 | "\n", 808 | " ```python\n", 809 | " # Explicit Type Conversion\n", 810 | " float_number = 3.5\n", 811 | " \n", 812 | " # Convert float to int\n", 813 | " int_number = int(float_number)\n", 814 | " ```\n", 815 | "\n", 816 | "#### Common Type Conversion Functions:\n", 817 | "\n", 818 | "1. **`int()`:** Converts a value to an integer.\n", 819 | "\n", 820 | " ```python\n", 821 | " float_number = 3.8\n", 822 | " int_number = int(float_number) # Result: 3\n", 823 | " ```\n", 824 | "\n", 825 | "2. **`float()`:** Converts a value to a floating-point number.\n", 826 | "\n", 827 | " ```python\n", 828 | " int_number = 5\n", 829 | " float_number = float(int_number) # Result: 5.0\n", 830 | " ```\n", 831 | "\n", 832 | "3. **`str()`:** Converts a value to a string.\n", 833 | "\n", 834 | " ```python\n", 835 | " numeric_value = 42\n", 836 | " string_value = str(numeric_value) # Result: \"42\"\n", 837 | " ```\n", 838 | "\n", 839 | "4. **`list()`, `tuple()`, `set()`:** Converts a sequence to a list, tuple, or set.\n", 840 | "\n", 841 | " ```python\n", 842 | " my_tuple = (1, 2, 3)\n", 843 | " list_version = list(my_tuple) # Result: [1, 2, 3]\n", 844 | " ```\n", 845 | "\n", 846 | "5. **`bool()`:** Converts a value to a boolean.\n", 847 | "\n", 848 | " ```python\n", 849 | " numeric_value = 0\n", 850 | " boolean_value = bool(numeric_value) # Result: False\n", 851 | " ```\n", 852 | "\n", 853 | "#### Example Code:\n", 854 | "\n", 855 | "Now, let's create a Python script that demonstrates both implicit and explicit type conversion:\n", 856 | "\n", 857 | "```python\n", 858 | "# codeswithpankaj_type_conversion.py\n", 859 | "\n", 860 | "# Implicit Type Conversion\n", 861 | "int_number = 10\n", 862 | "float_number = 3.5\n", 863 | "\n", 864 | "result = int_number + float_number # int_number is implicitly converted to float\n", 865 | "print(\"Implicit Conversion Result:\", result)\n", 866 | "\n", 867 | "# Explicit Type Conversion\n", 868 | "float_number = 3.8\n", 869 | "\n", 870 | "# Convert float to int\n", 871 | "int_number = int(float_number)\n", 872 | "print(\"Explicit Conversion Result (float to int):\", int_number)\n", 873 | "\n", 874 | "# Convert int to str\n", 875 | "numeric_value = 42\n", 876 | "string_value = str(numeric_value)\n", 877 | "print(\"Explicit Conversion Result (int to str):\", string_value)\n", 878 | "\n", 879 | "# Convert list to set\n", 880 | "my_list = [1, 2, 3, 3, 4]\n", 881 | "set_version = set(my_list)\n", 882 | "print(\"Explicit Conversion Result (list to set):\", set_version)\n", 883 | "```\n", 884 | "\n", 885 | "#### Conclusion:\n", 886 | "\n", 887 | "Understanding type conversion in Python is crucial for working with diverse data types. Whether it's automatic (implicit) or manually triggered (explicit), type conversion allows you to handle data more flexibly in your programs.\n", 888 | "\n", 889 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 890 | ], 891 | "metadata": { 892 | "id": "HopOWg2QAz74" 893 | } 894 | }, 895 | { 896 | "cell_type": "markdown", 897 | "source": [ 898 | "# Python `for` Loop - In-Depth Explanation\n", 899 | "\n", 900 | "#### Introduction:\n", 901 | "Welcome to an in-depth exploration of the `for` loop in Python at CodesWithPankaj.com! The `for` loop is a powerful and versatile construct that allows you to iterate over elements in a sequence, making it an essential tool in your programming arsenal. Let's break down the intricacies of the `for` loop.\n", 902 | "\n", 903 | "#### Basic Syntax:\n", 904 | "The basic syntax of the `for` loop is as follows:\n", 905 | "\n", 906 | "```python\n", 907 | "for variable in iterable:\n", 908 | " # Code to be executed for each iteration\n", 909 | "```\n", 910 | "\n", 911 | "- `variable`: Represents the current element in the iteration.\n", 912 | "- `iterable`: Refers to an iterable object like a list, tuple, string, or any other object that can be iterated over.\n", 913 | "\n", 914 | "#### Example 1: Iterating Over a List\n", 915 | "\n", 916 | "```python\n", 917 | "# Example 1: Iterating Over a List\n", 918 | "fruits = [\"apple\", \"banana\", \"orange\"]\n", 919 | "\n", 920 | "for fruit in fruits:\n", 921 | " print(f\"I love {fruit}s!\")\n", 922 | "```\n", 923 | "\n", 924 | "**Output:**\n", 925 | "```\n", 926 | "I love apples!\n", 927 | "I love bananas!\n", 928 | "I love oranges!\n", 929 | "```\n", 930 | "\n", 931 | "#### Example 2: Iterating Over a String\n", 932 | "\n", 933 | "```python\n", 934 | "# Example 2: Iterating Over a String\n", 935 | "word = \"Python\"\n", 936 | "\n", 937 | "for char in word:\n", 938 | " print(char)\n", 939 | "```\n", 940 | "\n", 941 | "**Output:**\n", 942 | "```\n", 943 | "P\n", 944 | "y\n", 945 | "t\n", 946 | "h\n", 947 | "o\n", 948 | "n\n", 949 | "```\n", 950 | "\n", 951 | "#### Example 3: Using `range()` Function\n", 952 | "\n", 953 | "```python\n", 954 | "# Example 3: Using range() Function\n", 955 | "for number in range(1, 6):\n", 956 | " print(number)\n", 957 | "```\n", 958 | "\n", 959 | "**Output:**\n", 960 | "```\n", 961 | "1\n", 962 | "2\n", 963 | "3\n", 964 | "4\n", 965 | "5\n", 966 | "```\n", 967 | "\n", 968 | "#### `range()` Function Explained:\n", 969 | "\n", 970 | "The `range(start, stop, step)` function generates a sequence of numbers from `start` to `stop - 1`, incrementing by `step`. If `step` is omitted, it defaults to 1.\n", 971 | "\n", 972 | "#### Example 4: Iterating Over a Dictionary\n", 973 | "\n", 974 | "```python\n", 975 | "# Example 4: Iterating Over a Dictionary\n", 976 | "student_grades = {\"Alice\": 90, \"Bob\": 80, \"Charlie\": 95}\n", 977 | "\n", 978 | "for name, grade in student_grades.items():\n", 979 | " print(f\"{name}'s grade is {grade}\")\n", 980 | "```\n", 981 | "\n", 982 | "**Output:**\n", 983 | "```\n", 984 | "Alice's grade is 90\n", 985 | "Bob's grade is 80\n", 986 | "Charlie's grade is 95\n", 987 | "```\n", 988 | "\n", 989 | "#### `items()` Method Explained:\n", 990 | "\n", 991 | "The `items()` method is used with dictionaries to return a view of key-value pairs as tuples.\n", 992 | "\n", 993 | "#### `break` and `continue` Statements:\n", 994 | "\n", 995 | "- The `break` statement exits the loop prematurely.\n", 996 | "- The `continue` statement skips the rest of the code inside the loop for the current iteration.\n", 997 | "\n", 998 | "#### Example 5: Using `break` and `continue`\n", 999 | "\n", 1000 | "```python\n", 1001 | "# Example 5: Using break and continue\n", 1002 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 1003 | "\n", 1004 | "for num in numbers:\n", 1005 | " if num == 5:\n", 1006 | " break # exit the loop when num is 5\n", 1007 | " elif num % 2 == 0:\n", 1008 | " continue # skip the rest of the code for even numbers\n", 1009 | " print(num)\n", 1010 | "```\n", 1011 | "\n", 1012 | "**Output:**\n", 1013 | "```\n", 1014 | "1\n", 1015 | "3\n", 1016 | "```\n", 1017 | "\n", 1018 | "#### `else` Clause in a `for` Loop:\n", 1019 | "\n", 1020 | "The `else` clause in a `for` loop is executed when the loop exhausts its iterable (reaches the end).\n", 1021 | "\n", 1022 | "#### Example 6: `else` Clause in a `for` Loop\n", 1023 | "\n", 1024 | "```python\n", 1025 | "# Example 6: else Clause in a for Loop\n", 1026 | "for i in range(5):\n", 1027 | " print(i)\n", 1028 | "else:\n", 1029 | " print(\"Loop completed successfully!\")\n", 1030 | "```\n", 1031 | "\n", 1032 | "**Output:**\n", 1033 | "```\n", 1034 | "0\n", 1035 | "1\n", 1036 | "2\n", 1037 | "3\n", 1038 | "4\n", 1039 | "Loop completed successfully!\n", 1040 | "```\n", 1041 | "\n", 1042 | "#### Conclusion:\n", 1043 | "\n", 1044 | "The `for` loop is a versatile construct in Python, providing a clean and efficient way to iterate over elements in various data structures. Armed with the knowledge of `for` loop syntax, iterable objects, and associated statements, you can handle a wide range of tasks in your Python programs.\n", 1045 | "\n", 1046 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 1047 | ], 1048 | "metadata": { 1049 | "id": "d7WuDU-3A5tI" 1050 | } 1051 | }, 1052 | { 1053 | "cell_type": "markdown", 1054 | "source": [ 1055 | "# Python while Loop\n", 1056 | "\n", 1057 | "#### Introduction:\n", 1058 | "Welcome to an in-depth exploration of the `while` loop in Python at CodesWithPankaj.com! The `while` loop is a powerful construct that allows you to repeatedly execute a block of code as long as a specified condition is true. Let's delve into the details of the `while` loop.\n", 1059 | "\n", 1060 | "#### Basic Syntax:\n", 1061 | "The basic syntax of the `while` loop is as follows:\n", 1062 | "\n", 1063 | "```python\n", 1064 | "while condition:\n", 1065 | " # Code to be executed as long as the condition is true\n", 1066 | "```\n", 1067 | "\n", 1068 | "- `condition`: Represents the expression to be evaluated. The loop continues as long as this condition is true.\n", 1069 | "\n", 1070 | "#### Example 1: Simple `while` Loop\n", 1071 | "\n", 1072 | "```python\n", 1073 | "# Example 1: Simple while Loop\n", 1074 | "counter = 1\n", 1075 | "\n", 1076 | "while counter <= 5:\n", 1077 | " print(f\"Count: {counter}\")\n", 1078 | " counter += 1\n", 1079 | "```\n", 1080 | "\n", 1081 | "**Output:**\n", 1082 | "```\n", 1083 | "Count: 1\n", 1084 | "Count: 2\n", 1085 | "Count: 3\n", 1086 | "Count: 4\n", 1087 | "Count: 5\n", 1088 | "```\n", 1089 | "\n", 1090 | "#### Example 2: `break` and `continue` in a `while` Loop\n", 1091 | "\n", 1092 | "```python\n", 1093 | "# Example 2: break and continue in a while Loop\n", 1094 | "number = 1\n", 1095 | "\n", 1096 | "while number <= 10:\n", 1097 | " if number == 5:\n", 1098 | " break # exit the loop when number is 5\n", 1099 | " elif number % 2 == 0:\n", 1100 | " number += 1\n", 1101 | " continue # skip the rest of the code for even numbers\n", 1102 | " print(number)\n", 1103 | " number += 1\n", 1104 | "```\n", 1105 | "\n", 1106 | "**Output:**\n", 1107 | "```\n", 1108 | "1\n", 1109 | "3\n", 1110 | "```\n", 1111 | "\n", 1112 | "#### `break` and `continue` Statements in a `while` Loop:\n", 1113 | "\n", 1114 | "- The `break` statement exits the loop prematurely.\n", 1115 | "- The `continue` statement skips the rest of the code inside the loop for the current iteration.\n", 1116 | "\n", 1117 | "#### Example 3: `else` Clause in a `while` Loop\n", 1118 | "\n", 1119 | "The `else` clause in a `while` loop is executed when the loop condition becomes false.\n", 1120 | "\n", 1121 | "```python\n", 1122 | "# Example 3: else Clause in a while Loop\n", 1123 | "counter = 1\n", 1124 | "\n", 1125 | "while counter <= 5:\n", 1126 | " print(f\"Count: {counter}\")\n", 1127 | " counter += 1\n", 1128 | "else:\n", 1129 | " print(\"Loop completed successfully!\")\n", 1130 | "```\n", 1131 | "\n", 1132 | "**Output:**\n", 1133 | "```\n", 1134 | "Count: 1\n", 1135 | "Count: 2\n", 1136 | "Count: 3\n", 1137 | "Count: 4\n", 1138 | "Count: 5\n", 1139 | "Loop completed successfully!\n", 1140 | "```\n", 1141 | "\n", 1142 | "#### Infinite `while` Loop:\n", 1143 | "\n", 1144 | "Be cautious when using `while` loops to avoid creating infinite loops. An infinite loop runs indefinitely unless there is a mechanism to break out of it.\n", 1145 | "\n", 1146 | "#### Example 4: Infinite `while` Loop\n", 1147 | "\n", 1148 | "```python\n", 1149 | "# Example 4: Infinite while Loop\n", 1150 | "while True:\n", 1151 | " user_input = input(\"Enter 'exit' to break the loop: \")\n", 1152 | " if user_input.lower() == 'exit':\n", 1153 | " break\n", 1154 | "```\n", 1155 | "\n", 1156 | "#### Nested `while` Loops:\n", 1157 | "\n", 1158 | "Just like `for` loops, `while` loops can be nested to handle more complex scenarios.\n", 1159 | "\n", 1160 | "#### Example 5: Nested `while` Loop\n", 1161 | "\n", 1162 | "```python\n", 1163 | "# Example 5: Nested while Loop\n", 1164 | "outer_counter = 1\n", 1165 | "\n", 1166 | "while outer_counter <= 3:\n", 1167 | " inner_counter = 1\n", 1168 | " while inner_counter <= 2:\n", 1169 | " print(f\"Outer: {outer_counter}, Inner: {inner_counter}\")\n", 1170 | " inner_counter += 1\n", 1171 | " outer_counter += 1\n", 1172 | "```\n", 1173 | "\n", 1174 | "**Output:**\n", 1175 | "```\n", 1176 | "Outer: 1, Inner: 1\n", 1177 | "Outer: 1, Inner: 2\n", 1178 | "Outer: 2, Inner: 1\n", 1179 | "Outer: 2, Inner: 2\n", 1180 | "Outer: 3, Inner: 1\n", 1181 | "Outer: 3, Inner: 2\n", 1182 | "```\n", 1183 | "\n", 1184 | "#### Conclusion:\n", 1185 | "\n", 1186 | "The `while` loop is a dynamic construct in Python, allowing you to repeatedly execute code as long as a specified condition remains true. With a solid understanding of the `while` loop syntax, the use of `break` and `continue` statements, and the inclusion of an `else` clause, you can handle various looping scenarios in your Python programs.\n", 1187 | "\n", 1188 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 1189 | ], 1190 | "metadata": { 1191 | "id": "vWD1CW2EA_ZX" 1192 | } 1193 | }, 1194 | { 1195 | "cell_type": "markdown", 1196 | "source": [ 1197 | "# Python break and continue\n", 1198 | "\n", 1199 | "#### Introduction:\n", 1200 | "Welcome to an exploration of the `break` and `continue` statements in Python at CodesWithPankaj.com! These statements provide control over the execution flow within loops. Understanding how to use `break` and `continue` is crucial for efficient loop management. Let's dive into the details.\n", 1201 | "\n", 1202 | "#### `break` Statement:\n", 1203 | "The `break` statement is used to exit a loop prematurely, regardless of whether the loop condition is still true.\n", 1204 | "\n", 1205 | "#### Example 1: Using `break` in a `for` Loop\n", 1206 | "\n", 1207 | "```python\n", 1208 | "# Example 1: Using break in a for Loop\n", 1209 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 1210 | "\n", 1211 | "for num in numbers:\n", 1212 | " if num == 5:\n", 1213 | " break # exit the loop when num is 5\n", 1214 | " print(num)\n", 1215 | "```\n", 1216 | "\n", 1217 | "**Output:**\n", 1218 | "```\n", 1219 | "1\n", 1220 | "2\n", 1221 | "3\n", 1222 | "4\n", 1223 | "```\n", 1224 | "\n", 1225 | "#### Example 2: Using `break` in a `while` Loop\n", 1226 | "\n", 1227 | "```python\n", 1228 | "# Example 2: Using break in a while Loop\n", 1229 | "counter = 1\n", 1230 | "\n", 1231 | "while counter <= 5:\n", 1232 | " print(f\"Count: {counter}\")\n", 1233 | " if counter == 3:\n", 1234 | " break # exit the loop when counter is 3\n", 1235 | " counter += 1\n", 1236 | "```\n", 1237 | "\n", 1238 | "**Output:**\n", 1239 | "```\n", 1240 | "Count: 1\n", 1241 | "Count: 2\n", 1242 | "Count: 3\n", 1243 | "```\n", 1244 | "\n", 1245 | "#### `continue` Statement:\n", 1246 | "The `continue` statement is used to skip the rest of the code inside a loop for the current iteration and move on to the next iteration.\n", 1247 | "\n", 1248 | "#### Example 3: Using `continue` in a `for` Loop\n", 1249 | "\n", 1250 | "```python\n", 1251 | "# Example 3: Using continue in a for Loop\n", 1252 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 1253 | "\n", 1254 | "for num in numbers:\n", 1255 | " if num % 2 == 0:\n", 1256 | " continue # skip the rest of the code for even numbers\n", 1257 | " print(num)\n", 1258 | "```\n", 1259 | "\n", 1260 | "**Output:**\n", 1261 | "```\n", 1262 | "1\n", 1263 | "3\n", 1264 | "5\n", 1265 | "7\n", 1266 | "9\n", 1267 | "```\n", 1268 | "\n", 1269 | "#### Example 4: Using `continue` in a `while` Loop\n", 1270 | "\n", 1271 | "```python\n", 1272 | "# Example 4: Using continue in a while Loop\n", 1273 | "counter = 1\n", 1274 | "\n", 1275 | "while counter <= 5:\n", 1276 | " if counter == 3:\n", 1277 | " counter += 1\n", 1278 | " continue # skip the rest of the code for counter equal to 3\n", 1279 | " print(f\"Count: {counter}\")\n", 1280 | " counter += 1\n", 1281 | "```\n", 1282 | "\n", 1283 | "**Output:**\n", 1284 | "```\n", 1285 | "Count: 1\n", 1286 | "Count: 2\n", 1287 | "Count: 4\n", 1288 | "Count: 5\n", 1289 | "```\n", 1290 | "\n", 1291 | "#### Conclusion:\n", 1292 | "\n", 1293 | "The `break` and `continue` statements are powerful tools for controlling the flow of loops in Python. While `break` allows you to exit a loop prematurely, `continue` lets you skip the remaining code for the current iteration and move on to the next one. These statements contribute to writing cleaner and more efficient code.\n", 1294 | "\n", 1295 | "For more coding tips and tutorials, visit [CodesWithPankaj.com](https://codeswithpankaj.com). Happy coding!" 1296 | ], 1297 | "metadata": { 1298 | "id": "05sc7O2rBFNY" 1299 | } 1300 | } 1301 | ] 1302 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Pankaj Chouhan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Complete-Python-Bootcamp 2 | The Complete Python Bootcamp From Zero to Hero in Python Learn Python like a Professional Start from the basics and go all the way to creating your own applications and games 3 | --------------------------------------------------------------------------------