Introduction
This comprehensive Python course begins with core programming concepts such as variables, data types, control flow (if-else, loops), and progresses through object-oriented programming, file input/output, and ends with advanced topics including modern Python features and AI-powered projects.
Core Programming Fundamentals
- Variables & Data Types: Understanding integers, floats, strings, booleans, and None.
- Control Flow: Use of if, elif, else clauses; logical and relational operators.
- Loops: While and for loops with examples like printing multiplication tables.
- Functions: Defining, calling, passing arguments, returning values, default parameters, and recursion (factorial and sums).
Data Structures & Their Manipulation
- Lists & Tuples: Creation, indexing, slicing, mutability differences, methods like append, insert, count.
- Dictionaries: Key-value pairs, methods like update, get, keys, and values, mutable nature.
- Sets: Unordered collections with unique elements, set operations such as union, intersection, subset.
- String Methods and Manipulations: Slicing, concatenation, methods like .startswith(), .find(), .replace(), escape sequences.
File Handling
- Reading, writing, and appending text files using the open function and the context manager (
withstatement). - Working with file methods like read(), readline(), readlines(), and managing file pointers.
- Handling exceptions and errors during file operations.
Object-Oriented Programming
- Classes and Objects: Structure, attributes (class vs instance), methods, and constructors (
__init__). - Encapsulation & Abstraction: Accessing instance variables via methods and properties.
- Inheritance: Single, multiple, multilevel inheritance, use of
super()method. - Static and Class Methods: Using decorators to define methods not bound to object or class instance.
- Advanced OOP Features: Property decorators for getters and setters, operator overloading,
__str__and__len__methods.
Modern Python Features
- Walrus Operator: Assign expressions within larger expressions to shorten code.
- Type Hints: Adding type annotations for variables, functions for clarity and better code checking.
- Match-Case Statements: Python's switch-case-like syntax introduced in 3.10 for cleaner multi-conditional logic.
- Exception Handling Enhancements: Use of
try-except-else-finallyblocks and raising custom exceptions. - Virtual Environments: Creating isolated Python environments and managing package dependencies with
pip freeze/requirements.txt.
Functional Programming Tools
- Map, Filter, and Reduce: Applying functions to lists for transforming or filtering data sequences.
- List Comprehensions: Elegant syntax for creating modified lists from iterables.
Practical Projects and Demos
- Jarvis Virtual Assistant: Voice-controlled assistant with speech recognition, text-to-speech, web interaction, and news fetching via API.
- Guess the Number: Interactive game implementing Python basics and control flow.
- WhatsApp Auto-Responder Bot: Automating message responses by scraping chat with PyAutoGUI and integrating AI replies via OpenAI API.
Best Practices for Learning and Development
- Emphasis on hands-on coding with practice problem sets after each chapter.
- Using AI tools like ChatGPT effectively for coding assistance.
- Encouragement to document learning with notebooks, source codes, and cheat sheets.
- Job search advice including LinkedIn optimization and strategic application sending.
Conclusion
This course bridges beginner to advanced levels through a blend of theoretical concepts, practical exercises, and real-world projects focusing on modern Python capabilities and market-relevant skills. It fosters a strong foundation and inspires learners to explore specialized fields such as AI, web development, and data science using Python. For a deeper dive into AI concepts integrated in this course, see the Comprehensive Artificial Intelligence Course: AI, ML, Deep Learning & NLP. To strengthen foundational programming skills relevant here, consider the Comprehensive Overview of Algorithms and Data Structures Course. For learners interested in expanding beyond Python to full-stack development, the Comprehensive CS50 Course Review: From Basics to Web Development offers valuable perspectives. Additionally, mastering data manipulation techniques detailed in the Comprehensive Guide to Python Pandas: Data Inspection, Cleaning, and Transformation can significantly complement your Python data handling skills.
This is Sallu Bhai's son, Ballu. Ballu didn't get a break in Bollywood. That's why he learned Python from the internet in 2 days.
He thought that by learning Python in 2 days, he'll get a 90 LPA job. He didn't get a 90 LPA job.
All he got was frustration. Why do I get an error every time? I don't want to do the recording.
Harry, you? What happened? Why is there so much noise? I've been trying to learn Python since then.
Nothing is happening. One error, then another error. How will I learn Python?
I'm following the proper road map. It'll be done. It's not that difficult. These things look good in theory.
Road map, project. I'm going to Bigg Boss' house. I don't want to do the recording.
I think I'll have to bring a new Python course. In which, latest features, how to use AI,
and how to get jobs. I'll learn Python from this aspect. Complete Python course with handwritten notes.
And how to use AI to learn Python. All this is ready with knowledge. Not only this,
I made this course keeping the job aspect in mind. If you learn Python through this course, you'll definitely get a job.
And I'll also tell you how to find a job and where. I've divided this course into many chapters. And for your practice,
I've made many practice sets for you. Not only this, we're going to make amazing projects.
Not light projects. Amazing, amazing AI projects. There's only one prerequisite of this course.
And that is your time. If you bring your time, I'll teach you how to do machine learning with complete Python.
Artificial intelligence programs. Data science. Web development.
General scripting. And this list goes on and on. I'll teach you how to do all this.
So, let's not waste time. Let's go to the computer screen. And let's get started.
According to Stack Overflow, Python is the most loved and easiest language. To do this course,
you don't need to know any language. Even if your Python is the first language, you can do this course easily and well.
In fact, the purpose of this course is to teach you your first programming language. So, I'd like to open my Python handbook here
and talk about what is programming. I'll give you this ultimate Python handbook along with cheat sheet,
along with notes, which I've written by hand. There are many more things for you,
like source code. There are many more things which I'll tell you in the coming time.
But let's focus on what is programming. Just like we use Hindi and English to communicate with each other,
we use programming language like Python to communicate with the computer. If we want to say something to the computer,
like, sum two numbers, we can't say, like Amitabh Bachchan,
like, Mr. Computer, sum two numbers. No, we can't say that. We have to write a proper program.
And we'll write that program through a programming language. Just like you talk to a Chinese person
in Chinese, you talk to a French person in French, whatever language you understand,
you talk to them in that. Similarly, you talk to the computer in programming languages like Python.
C, C++, Java, Rust, Ruby, Golang, JavaScript, these are all programming languages. But in this video,
we're going to learn Python. Why did I choose Python as your first language
if you're a beginner? Because Python is simple, it's easy to understand,
and it feels like you're reading simple English. Yes, it feels like you're reading plain and simple English.
And I'm not saying this. These are the people who have learned Python.
And this is the general opinion in the industry that Python is so easy that it feels like you're reading
plain and simple English. Now, after watching this whole course, you'll think this course is too big.
In fact, this course is too small. This course is not big because I have learned all the Python
along with projects, along with my experience, which I have gained
after struggling so much. I have learned so many things that I am going to teach you in a video.
So yes, this video is small, boss. This is the pseudocode nature of Python, that is, the easy-to-understand nature
that it feels like you're reading English. This nature makes it understandable among beginners.
For example, look at this program. This is a program to sum up five numbers, which I have written in just ten lines of code.
Then look at this program. This is a program to equally distribute the expense of a trip among three friends.
Suppose three friends are going on a trip to equally bear everyone's expenses, then this program will equally split
the total expense among the three. Then this third program is amazing, which is Jarvis.
Whatever you say, he will do. Jarvis opens YouTube. You will give voice commands
and he will obey the voice commands. I will tell you once again that this course is absolutely free
and I am not going to sell you anything in this course. So you guys watch it completely and you will learn Python.
You don't have to buy anything. I am not going to sell you anything. Let's talk about the features of Python.
It is easy to understand, that is, the development time is very short. If you want to write a script
which is an assistant and you talk to people, basically your computer,
it is very easy to write such scripts in Python. Artificial intelligence, machine learning, it is very easy to write all these things in Python
because it has dedicated libraries. It is free, it is open source. By the way, we will talk about libraries
in the coming time, but it is free. You don't have to pay for it.
It is open source. It is high-level language. High-level language means
that you talk to it from above and it translates your code in low-level instruction and executes it in the machine.
The hardware is our computer, like our CPU, RAM, GPU. All this comes in our hardware
and the software is the instructions which are executed on that hardware. Our Python script is also a kind of software
which is going to be run on our hardware and will instruct our hardware what to do with it.
For example, if you want to sum two numbers, then through software, that is, through a Python program,
you will tell that I want to sum two numbers and then you can easily sum two numbers. Basically, we give code to the Python interpreter
which is written in the Python script and it converts that Python script into the machine language.
The Python interpreter takes the Python script from us and gives the machine code to the computer which the computer easily understands.
You cannot talk directly to the hardware. You will have to talk through software. Python programming language
is a way to make this kind of software. I hope that makes sense. Even if it doesn't,
if you are a beginner, it will make sense in the future. It's fun to work with
plus it's portable. That is, if you have written a code in Linux, Windows,
even if it is written on Mac, then your code is going to run. However, I will tell you more about this in the future.
But Python is portable and it's fun to work with. Let's not waste time
and install Python. To install Python, I will simply come to Google
and write Python install. And as soon as I write Python install, I will get a page with the download Python title.
Which will be the first page. And you will get a button which says download Python 3.12.3.
I will click on it. And the Python setup has been downloaded. This setup of 25 MB has been downloaded very quickly.
And after this, I will open it. And as soon as I open it, I will see a window like this.
Here it is very important for you. And I want to emphasize this that you guys check this
at python.exe to path. It's really very important that you guys follow this step.
After this, click on install now. And it will start installing. It will take some time
depending upon how fast your system is. My computer is very fast. That's why it's getting very fast.
But you got the point. It will also happen in your computer. Even if it is slow, it will happen.
It will take time. It will take time. It will be correct.
It will happen. And setup was successful. I am very excited.
I will close this. And before we start coding, we will install one more piece of software.
And this time we will write Download VS Code. As soon as we write Download VS Code,
the first link we will get will be of Download VS Code. We will open this.
As soon as we open this, here you will be given options for Windows, Linux, and Mac.
I am in Windows. That's why I will download Windows. If you are in Mac, Linux,
or in any other machine, download for that. If you are in Apple Silicon,
download for Silicon. If you are in Apple's Intel laptop, download for Intel.
But for now, I will download for Windows. It has started downloading.
The internet connection is very fast. I pay a lot of money for my internet. I have collected that.
I have downloaded it as soon as possible. We will open this. And we will read the License Agreement.
I will read this License Agreement fully. This License applies to Visual Studio and other damages.
And yes, I agree to this agreement. I will click on Next. Make sure that you have checked
all these check boxes. We will click on Next. We will click on Install.
And it's going to install now. And you can see here that it has been installed.
Now I will click on Finish. And my Visual Studio code will be launched. I will change a quick setting for you.
You have to search for mouse wheel here. And you have to check this setting. This is very important.
Then you have to click on Extensions and download the Python extension. You have to search for Python here.
Click on Install. And as soon as you click on Install, it will be installed.
Basically, it becomes easy to code in Python programming language if we use it.
So yeah, it was that easy. Just install it like a game. I am very excited
to write my first Python program. And we are going to do the same in the next chapter.
So yeah, see you in the next chapter. Before moving ahead, I want to tell you
a very interesting story. Python was created by Guido van Rossum in 1991.
He had created a new programming language for which he was looking for a name. And he couldn't think of a name.
He was watching a British comedy series named Monty's Flying Circus. He wanted a short, unique,
and a little mysterious name. And he named it Monty Python's Flying Circus.
Guido van Rossum is a genius who first worked in Google and then he joined Dropbox.
And now he works in Microsoft. If you are looking for a job, Python programming language
is perfect for you. I am not saying this Python course is the latest.
I will include all the things that have come in Python in the recent years.
And some features have been added in the latest version. So we are going to cover all those things.
Now I am very excited because we are going to write our first Python program.
And our first chapter is Module, Comments, and Pip. What are these three things?
First of all, I will show you by writing the first program. We will make a folder from here
so that things are organized. See, I also have to give you the source code.
So from that perspective, I will make a new folder for you whose name I am going to keep
Chapter 1. I will go inside Chapter 1. I would like to tell you
that if you have installed exactly like I have installed VS Code,
then you can right click and select the option Open with Code
so that VS Code will open in this folder. That is, whatever files will be in this folder,
like if I made a file here, I will make a file here. Let's say I make a main.py
or I make a first.py. I think first.py is a better name. I made first.py.
.py is an extension. You can click here and make a new file in VS Code.
What is VS Code? VS Code is an editor for us to write code.
Can we write in Notepad? Yes, we can write. But it won't be that efficient.
Okay, you should understand this thing. The second thing is I wrote first.py here.
.py means it is a Python script. Now you see, as soon as I wrote first.py here,
first.py came here too. So opening this folder in VS Code means the file I am making here
I will see that file here too. That means this folder is open. Now whatever I type here,
it will be saved in this file. If you have enabled autosave in the file, then you don't need to press ctrl s.
Whatever you write, it will be saved. So make sure
that you have turned on autosave. Just like me. Now what I will do here,
I will write print. And I will put a double quote here. And you see,
as soon as you put a double quote, automatically this double quote will also close.
And if you write hello world, then here
hello world will be printed when you run this program. Now what do you have to do
to run this program? You click on the terminal here. What did I do here?
As you can see, I am bringing my mouse arrow here. So it is showing me like this.
I can take it up like this. And by clicking on the terminal here, I will write python.
And after that I will write first.py. That means I am telling python
to run the first.py script. I will hit enter and you see
hello world is written here. What does it mean? It means
that this script is running and I got to see its output. What does output mean?
When we run a program, it has an output. In this case,
that output is hello world. As you can see. So congratulations,
you have written your first python program. You guys in the comment section below,
do write congratulations by liking this video. Like the video
and in the comment section below, congratulations to all of us learners.
Now what I will do here, I will tell you what is a module.
What is a module? And what is a pip? We wrote our first python program.
We understood what .py is. .py is an extension. When we make your python program,
then we write the name of that file as .py. Like you must have seen
movies.mp4. Game setup is .exe. In the same way,
we use python file .py extension. Now let's talk about what is a module
and what is a pip? Module is a file containing code
written by somebody else. What does it mean? It means that
usually this code is written by somebody else. And we can use it
in our program in the same way. Now what does it mean?
Suppose I tell you guys that you have to find the harmonic mean
of five numbers. You have to find the arithmetic mean.
If you have to write its program, then you will write it
from scratch. You will code the whole logic one by one.
But if you automatically get a module which you can install
with the help of pip, then you can use somebody else's code
in your program. How does it work? I will show you guys.
With the help of pip, you can install any module.
Now there are many modules. For example, there is a module called flask
which helps you in making web apps. There is a framework called Django
which also comes as a package, as a module
which you install by typing pip install django. There are many more
modules in python which we can install by typing pip install
and then using its name we can install it. So I will show you guys
a few modules which I will install. You have to come to the terminal
and simply type pip install flask. And as soon as I type
pip install flask, my flask will be installed. You can see
successfully installed flask. And after this I can import it
by typing import flask. And to give you guys an example of a module,
I will show you a very interesting package. Let me show you, whose name is pyjokes. You can install it by typing pip install pyjokes.
So I will come here in my terminal and I will type pip install pyjokes. I have created a new file by clicking here with the name module.py. Now you can see here that successfully installed pyjokes.
That is, pyjokes has been successfully installed. Now what I will do here, I will import pyjokes. And you see, as soon as I write pyj, it automatically suggested me pyjokes.
And this is the reason why we use VS Code. Because we get all the features that we may not get by typing in notepad or any other editor. Now I will write here joke is equal to pyjokes.
And I will write here get joke. And then I will write print joke. I will tell you all this step by step.
But now I just want to show you what is the package. So look, it has given me a joke. What's the object-oriented way to become wealthy?
It's inheritance. Let's see if it gives a new joke by running again. Yes, it's giving a new joke.
How many programmers does it take to kill a cockroach? Two, one holds, the other installs windows on it. So this is a windows joke.
But I am a windows fan by the way. So this is the module. Look, I didn't write this code to write jokes.
I imported pyjokes. And automatically I can print a random joke on my computer. So I didn't write the code.
I used someone else's code. But I used someone else's code and printed the joke. That means I achieved the ultimate task.
Which ultimately matters. The documentation of pyjokes is here. You can do everything here.
But that was just an example. We will see many more things later. So pyjoke was a module.
We installed it with the help of pip. Pip is the package manager for python. You can use pip to install a module on your system.
Like pip install flask, pip install pyjoke. So we have two types of modules. One built-in module and one external module.
Now we will see how python can be used as a calculator. I will tell you that if you open the terminal of your computer. Which you can open by right-clicking on the terminal in windows.
And if you are in mac or linux, you can open the terminal. From spotlight search or by going directly to search. Now what I will do here, because I am on windows.
I opened the terminal from here. Once your terminal is open, you write python. And as soon as you write python.
It will tell you the version and date of python. When this version was built. And after that you can write here.
Let's say I write 5 plus 6. And it is showing me 11. Wow, this is amazing.
Let's try multiplication. 4 multiplied by 2. Wow, 8.
You are doing multiplication very well. You are doing everything right. Let's try something else.
Let's give it a little trick. Not like this. Let's give it a trick.
Will it be able to do it? Maybe it will be able to do it, maybe not. And it is able to do it.
So even if we give it a tricky calculation. Still it is able to do it. Which means that we can use python as a calculator.
So to use python as a calculator. We simply write python. Enter it in the terminal.
And it opens RAPL. RAPL stands for Read Evaluate Print Loop. Now what is this Read Evaluate Print Loop?
Read Evaluate Print Loop means. It will read and evaluate. For example, I entered 6 plus 5.
So it read and evaluated it. It said it is 11. And printed it.
And went back to the loop. It will read, evaluate and print. Then it will go to the loop.
For example, if I write here 3. Multiplied by 6. So it read it.
Evaluated it. Printed it. And then repeat.
Now it is saying again. So Read Evaluate Print Loop. It is called RAPL.
So if someone asks you what is RAPL? So this is RAPL in python. So if you want to write python.
You can fire python like this. In the terminal and write. Will my print hello world work?
It should work. Yes it is working. So whenever we want to execute anything through python.
We can write it in RAPL. We can write print. After that we wrote hello world.
Entered it. If I make a syntax error here. If I write anything.
Which is not valid in python. So will it work? No, it is giving me an error.
So it will give an error. If it is valid, then it will work. If it is not valid, then it won't work.
So you have to keep this in mind. So we saw what is RAPL. Read Evaluate Print Loop.
Now let's talk about comments. If you come here. I wrote import pyjokes.
This was our program. With which we printed the joke. If I run it and show you.
How many programmers does it take to change a light bulb? None. That's a hardware problem.
So this is a joke. A programmer can't fix a light bulb. Because it is a hardware problem.
So go and call an electrician. So if I write print. Like I wrote hello world.
Printing jokes. If I run it again. So you will see.
Printing jokes. If you play a windows cd backwards. You will hear.
Satanic chanting. If you play it forwards. It installs windows.
So this is a windows joke. If you understood. Now I want this line.
To remain in my program. But not execute. I want a way to ignore it.
So what I can do. By clicking on this line. Control forward slash.
So this comment will be out. By pressing ctrl forward slash. VS code put a pound symbol.
Pound symbol is. Symbol for. Comments.
To write a single line comment. You have to start with pound. Whichever line starts with pound.
Python will automatically ignore it. You should know this. Now why do we use comment.
Sometimes we have to put comments. In our program. Like I want to write.
This prints a random joke. Now if I write it like this. I want to tell you.
That the line below. Is printing a random joke. I want to indicate this.
For fellow programmers. I want to write this line. Now if I run it like this.
Invalid syntax. But I want to keep this line. So the way to keep this line is.
Single line comment. By pressing ctrl forward slash. It will be uncomment.
By pressing ctrl forward slash again. It will be comment. VS code is showing it in green color.
Because VS code wants to tell you. Python interpreter will ignore this line. So if you run it.
This line will be considered. As part of this program. It will be ignored.
So simply a joke will be printed here. And the rest of the comment lines. Will be ignored.
So if you want. It to be uncomment again. By pressing ctrl forward slash.
You can toggle it. So this was single line comment. Sometimes we have.
Very long comment. And we need multiline comment. So this is the way to write.
Multiline comment. Multiple lines will be ignored. Single line comment will be ignored.
Multiline comment is the way. To write triple double quotes. And it will be ignored.
For example. If I come back to VS code. If I write triple double quotes.
And I write something like this. So thanks. That was my program.
If I run it. So thanks that was my program. It is not printed.
It was ignored. Because it was a multiline comment. So it was ignored.
So this is multiline comment. This is single line comment. Now I will tell you something amazing.
Since we have. Good IDEs and text editors. Since then we use.
Only single line comment. Most of the programmers. Use single line comment.
Suppose you have multiple lines. There are many lines. One more line.
And suppose I write. Yet another line. And we have many more lines.
What happens is. If I want to comment all of them. I will select.
Control forward slash. And it will comment individually. It is difficult to do it manually.
That's why multiline comments were made. But with the help of IDE. With the help of text editors.
We can select lines. Control forward slash. Comment or uncomment.
If I want to comment only these three lines. Control forward slash. Comment uncomment.
Good. Now we will comment it out. Otherwise there will be an error.
If I don't comment it out. There will be an error. What is this?
That's why I will comment it out. If I comment it out. There will be no error.
Everything is working fine. This is Microsoft attack. Why did Microsoft name their search engine Bing.
Because it's not Google. Now what we will do. We have seen the comment.
We will come back to this handbook. Handbook is very important. I will tell you all the links.
How you can download the handbook. How you will get cheat sheets. Just stay in this course.
And it is my responsibility to teach you Python. We have seen two types of comments. We write single line comments like this.
We put a pound symbol and it is ignored. After this we have seen multi-line comments. Triple double quote or triple single quote.
You can also use triple single quote. You can also use triple double quote. To write multi-line comments.
So this was about our comments. Now we will go to chapter 1 practice set. Let me tell you.
In practice sets. I will make you talk after every chapter. In these practice sets there will be problems.
Which I will make you solve. I will give you the solution. So that you can practice.
And you can understand the concepts. So let's go to chapter 1 practice set. So let's go to chapter 1 practice set.
So brothers and sisters. I have become a big fan of organizing things. So I will make a new folder in the name of chapter 1 PS.
I will close our existing folder. I will close it. I will choose the option of open with code.
If you are not getting the option of open with code. It means that at the time of installation. The checkboxes that I told you to stick.
You didn't do that. And if you want to fix it. Then install VS code again.
By sticking those checkboxes. Without installing, you run the setup again. Stick those checkboxes.
Your open with code option will come. Now I will click on plus here. After clicking on plus, I will make a new file.
Problem1.py I will hit enter. And this is our problem 1.
Now let's see what was our problem 1. Now see here. If your select interpreter is coming here.
Then you select the interpreter. I will select the 3.12 which I have just installed. So this has become my default interpreter.
If you can see below. It's a very good thing. What is the first question of chapter 1?
Write Twinkle Twinkle Little Star Poem in Python. Write a program. Let's google it first.
Twinkle Twinkle Little Star Poem has come here. After that, we will scroll down here.
Where is the poem? I have made the whole page. And you guys copy paste it like this.
The question is very simple. Write a program to print Twinkle Twinkle Little Star Poem in Python.
How will you do it? It's very simple. You will use print.
If you do it in double quotes. Then it will not work. So what you have to do?
You have to put triple single quotes. Put triple single quotes like this. And put triple single quotes again.
What did I do? I didn't tell you this. So I told you through this practice set.
Either you print all the lines one by one. Print Twinkle Twinkle Little Star Print How I Wonder What You Are
Print Up Above The World So High Print Like A Diamond In The Sky Instead of doing all this,
You can print multi lines with triple single quotes or triple double quotes. We talked about comments.
We said that comments can be done with triple single quotes and triple double quotes.
But now I want to tell you that you can use triple single quotes and triple double quotes
to print multi lines. Or if I keep it more professionally. Then I will say that
you can also make multi line strings. So that was the take away. Let's run it and yes
our poem will be printed. Wow! Amazing! Poem is printed. Problem is solved. Question number 2.
Use Ripple and print the table of 5 using it. You have to solve all the problems. Try to solve it yourself.
And if you say that I don't have time. Now JCB will be installed outside. And when JCB will start digging,
everyone will stand up to see. It will look like there is no time. If I ask you to ask questions,
you have to try all the questions yourself. Use Ripple and print the table of 5 using it. So how will you do it using Ripple?
First of all, I will open the terminal here. What is Ripple? What did you say? What is Ripple?
I told you in chapter 1. Write Python like this. And as soon as you write Python,
it will come to you like this. Now what do you have to do here? You have to write 5 into 1.
It's 5. 5 into 2. 5 into 3. 5 into 4.
5 into 5. 5 into 6. Why are you doing it manually?
Yes, I know that it can be done more efficiently. And I will tell you in the coming chapters. But now we have printed the table of 5.
Using Ripple. Using Python. So this way you can print the table of 5.
Question number 2 is solved. Question number 3. Install an external module and use it to perform
an operation of your interest. Which external module do you want to install? We saw that there are two types of modules.
Built-in and external. We have to install the external module. And then after installing the external module,
we have to use it. So to solve question number 3, I am going to use a very interesting module.
whose name is PyTTX. Which is used for Python text-to-speech.
So I have installed pip. I will install PyTTSX3, I will simply bring my terminal here, so I will install PyTTSX3, I will bring my terminal, I will press ctrl V, I will hit enter and you see it has been installed here and it is very easy to use it, simply copy this code and whatever I type, it will speak, okay, it's good, let's run it
I will speak this text, wow, so here we can't speak Hindi, but again you got the point, okay, so we can write in English, we will write anything in English, I will write Hey I am good, so it will play Hey I am good Now you can use any module, this was my choice of module, which I installed an external module, then I used it to perform an action, you can install Pygame You can basically use anything and there are a lot of useful modules in Python, and as we move forward, you will know how to use the modules, now the next question says
Write a Python program to print the contents of a directory using the OS module, now yes, we will do this with chat gpt, now you will say why are we doing it with chat gpt, you are teaching chat gpt here So I give you the justification for this, we are using chat gpt because if we want to survive in this AI era, then we should know how to use AI, and for that I will tell chat gpt to give me the program to print the content of a directory from the OS module So let's go to chat gpt now, so here you can see that chat gpt is open and what I will do here is that I will ask a question from it and why not copy and paste it directly, so I will do ctrl c here and ctrl v here and it will tell me this
In fact, it is searching online, but I don't want it to search, now look here, I will copy this code and I will paste it by doing problem 4, so I will come to this code now, I have made problem 4.py here and I will paste it, now it is saying that give me the path of your directory here, so why not give only slash here and as soon as I run it, you can see that it has listed all the files and folders in my C drive Okay, if I tell it here that tell me what is in the slash new folder and f is small here, then it will tell me what is in the slash new folder, there is audio, there is card 1, there is card 2, amazing, so we can list files and directories in a specific path and you can see that the code that AI has generated for me is how well commented and accurate, you can see the use of comments, AI is also doing it, it is telling us what is the use of which line, so amazing Throughout the course, I am going to use AI because we are going to solve a bigger problem, but I will request you that you don't use AI too much, I just want to tell you that by using AI, you can make a very big program with a little knowledge and I want you to use it, but until we reach chapter 4 or 5, I will request you not to use AI until you get the concepts of the whole python
5th question says, label the program written in problem 4 with comments, AI has already labeled it with comments, so you know what the solution of this problem is, usually the code we get online does not label with comments, but here AI has automatically labeled with comments, so you can delete these comments and write again with comments, you can say to AI that give me the program without comments, just for your learning, you can say give me this program without comments, and you can see that it has given without comments, I will copy and paste it, then I will write comments, so first of all I will write here, select the directory whose content you want to list, I have commented it with ctrl forward slash, I will capitalize it, then after this, use the OS module to list the directory content, then after this, I don't know what is for at this point, I used AI, but if I just do print content here, then it will work, now how is it doing it, leave all this, we have only run the online program, now if it doesn't have it, then I will slash it, there is no folder named directory path on my computer, but you can see here that it has listed all the content,
this is amazing, I love it, and here I will write print the contents of the directory, so this is the program which is commented, plus lists the content of your directory, so that was practice set 1, now we will go to chapter 2, where we will learn about variable and data type, I am very excited, let's go Alright guys, so chapter 2 is our variable and data type, and I will say that your real python starts from here, what is variable, what is data type, I will tell you everything, and I will make your fundamental so strong in python that you will never have any problem, so variable is the name given to a memory location in a program, what does that mean, you may not have understood by reading this statement, but I will also come here, where I have organized for you all, I will give you all this, handbook, handwritten notes, cheat sheet, project source code, I will give you everything, I will click on open with code, so that our folder opens in VS code, click on plus, and here I will make a python program named variables.py, so I made a python program named variables.py, what is variable, if you write here a is equal to 1, b is equal to 2,
and if I print a plus b, then what will happen, a is equal to 1, b is equal to 2, and if I print a plus b, then 3 will be printed, so if I run this program, then its output will be 3, as you can see, so what is a here, a is a variable, what is b, b is also a variable, you can consider a variable as a container, what is a container, like in your kitchen there is lentil, rice, flour, there are many other things in the kitchen, so in your kitchen there is a box, you can store things in the box, so if I bring this analogy here, so here this is a box, in which I put 1, so a is a container, in which I put 1, 1 is a number, so in a comes 1, so whenever I say a, what will it mean, it means give me 1, now let's say there is a red box, there is a blue box, there is sugar in the red box, and there is lentil in the blue box, if I say give me a red box, then what does it mean, it means give me sugar, so whether you call it a red box or sugar, it is the same thing, in the same way, when I said a is equal to 1, then 1 is stored in a, what will happen is that in your RAM, your random access memory, in that, on the memory location named a, 1 is stored, so we will not go into more details, we will just consider the variable as a container and say that 1 has come in that container, in the same way, b is equal to 2 means that 2 has come in b,
let's come back to the handbook, a is equal to 30 means that 30 has come in a, a is equal to Harry means that Harry has come in a, why is there a double quote, double quote means string, and if I write h-a-r-r-y like this, then it will give an error, but I wrote it in a double quote, so I marked it like a string, and it is important to mark it like a string because if there is another variable named Harry, then the Python interpreter will know that I am not talking about it, I am talking about Harry as a string, in the same way, I wrote c is equal to 71.22, so a floating point number has come here, here a is the name of the variable and 1 is a constant value, b is the name of the variable, 2 is a constant value, here we can also call b as an identifier, identifier means that the name used to identify, if your name is Rohan, then your Rohan is an identifier and the value of Rohan is that you are, you understand something like this, identifier means the name by which the name is called, if you say this is a cat, then the cat is an identifier, which identifies, you know that there is a cat image in your mind, so cat is an identifier, in the same way, identifiers are used to denote which variable is being talked about, if I write here c is equal to 7, and if I write here name is equal to double quote, so I am Harry, double quote means that I am talking about a string,
if I don't put a double quote, then this Harry will look for another identifier, which is not there, and that's why he is giving me an error, saying that Harry is not defined, so what does double quote mean, double quote means that a string is being talked about, name is equal to double quote Harry, as soon as I did it, it will understand that I am talking about Harry as a string, then whenever I print name, then Harry will be printed, so this was identifier, now let's talk about data types, if I talk about Harry, it is a floating point number, which has a decimal, and if I talk about Harry, it is a string, which we also write in double quote, so there are three different types of data types, primarily if I talk about integers, floating point numbers, strings, booleans, and none, these are the main data types, what comes in integers, 1,2,3,4 numbers, what comes in floating point numbers, 7.22, 8.74, 7.221, all these are floating point numbers, then in strings, whatever you write in double quote, it becomes a string, so what I will do here, I will make a file named data types.py, data types.py, and let's do one thing, let's number them, so that you know, which program was made first, which was made after that, because I don't just trust you to teach, I want you to get the source code as well, so here if I write a is equal to 1, then I will write a is an integer,
then if I write b is equal to 5.22, then I will write b is a floating point number, then if I write c is equal to Harry, then I will write c is a string, then if I write d is equal to true, which can also be false, and capital F will be false, capital T will be true, true, false, this is the boolean data type in Python, sometimes we have to store only this much, that is it true or false, yes or no, then we use boolean variables, true, false, these are boolean variables, now you see here I write d is a boolean variable, so this is our boolean variable, now if I write here e is equal to none, what is this, e is a none type variable, now why are we storing none, why, what is the need, sometimes we have to mark that there is nothing in this variable, nothing, to mark nothing, we use none, now some of you will say that to mark nothing, we can also use false, no, false yes or no, when we have to identify anything in yes or no, then we use boolean variables,
but if I have to denote that there is nothing in a particular variable, then I will use none type variable, I hope that is clear, now we will talk about rules for defining variable names, now you tell me one thing, your name can be Rohan, Akash, Izan, Sameer, but have you seen a person whose name is 722xy underscore kp at the rate, it doesn't happen, if you have seen it, then it will be a sample, I have never seen it, and it doesn't happen, now don't comment that I have seen it, if you have seen it, then do it, but I tell you that there is a rule to define the name, there is a rule that his name can be this, Srishti can be this girl's name, this girl's name is underscore at the rate bb at the rate, there is no copyright symbol, it doesn't happen, in the same way, there is a rule in Python programming language, you can keep the name of this variable a, you can keep b, you can keep c, you can keep d, you can keep e, but there are some rules, what you can't keep, I will tell you now, so I will make a file here with the name of 03 underscore rules underscore variables dot py, and I will show you that a is equal to 23 will work, aaa is equal to 435 will work, harry is equal to 34 will work, sameer is equal to 45 will work,
underscore sameer is equal to 34 will also work, but at the rate sameer is equal to 56 will not work, you can see that a red color has been given to us by the pylons, that brother, see this will not work, how will we know what will work and what will not work, we read the rules to give the name of the variable, if we understand those rules, then we will find out whether the name of the variable is valid or invalid, come on, let's read the rules, let's come to the handbook, and you see, a variable name can contain alphabets, digits and underscores, see, in the name of the variable, there can be alphabets which are from a to z, there can be digits which are from 0 to 9, there can also be underscores, underscores are symbols by pressing shift and dash, you will see in your keyboard, this one is key, see, you can see this one, this one is key, see, I have to show you underscores too, so there can be alphabets, there can be digits, there can also be underscores, but the variable name can start only from the alphabet or underscore, that is, you can use any character from a to z to start, you can also use underscore to start, you cannot use numbers to start, it will not start from 0, 0 Sameer is not a valid variable name, 1 Sameer is also not there, 1 Srishti is also not there, 1 Divya is also not there, 3 Katrina is also not there, okay, so according to this, you will have to follow this rule, you will have to follow this rule, the third rule says that a variable name can't start with a digit,
it can only start with an alphabet or underscore, it can't start with a digit, it can't be a white space in the name of a variable, this is our fourth rule, our fourth rule says that there can be no white space in the name of a variable, nor can it be a special character, we have talked about underscore, it can be that, it can be alphabets, it can be digits, it can be underscores, so if you understand these four rules to define a variable, then you will tell that the variable is allowed or not, now you see here, if I come back, then underscore Sameer is valid, why was it not valid, because it started with add the rate, okay, what is this, valid variable name, it is not there, it has a symbol of add the rate, a special character has come, if it was underscore, then it would have worked, add the rate will not work, okay, so great, you should understand this thing, okay, so these were the rules for defining variables, this is invalid, this is also invalid, I will add the rate here
So this is invalid due to add the rate symbol This is invalid because it has add the rate There can't be add the rate in the name of variable
Now you will say why such rules were made in python programming language Such rules were made in python programming language because There are some things that python interpreter uses internally
This add the rate, we will see when we will read object oriented programming Where it is used, where is this add the rate used What is decorator, I will tell you everything
And then you will know why such rules were made These rules were made so that you can enjoy python programming So that you don't get confused
That's why these rules were made This name is valid, this name is invalid You can use this, you can't use this, that's it
To talk like this That's why these rules were made for you Now let's come back to operators in python
What are operators? If you have studied a little maths If you have studied maths of 5th and 6th
Then you will know There are plus minus times divide arithmetic operators Everyone knows this
But in programming there are assignment operators Is equal to assignment operator When I will write a is equal to 1
Is equal to an assignment operator Which is saying to put 1 in a It is saying that I will call this memory location with name a
In which 1 store is there a is a container in which 1 store is there So equal to is an assignment operator
Plus equal to is also an assignment operator Minus equal to is also an assignment operator Then we have comparison operators
Which are used to compare two things Like if I say 1 equals to equals to 1 Is 1 equals to equals to 1
If I have written 1 equals to equals to 1 If this is true then it will return true Otherwise it will return false
We will see how it works Then we have logical operators AND, OR, NOT
How do they work? We will also see this So we will see these operators one by one
So first of all we will see arithmetic operators When we write 7 plus 4 is equal to 11 Then 7 and 4 are called operands
And the plus you see is called operator And 11 after equal to is called result So I will come here
04 underscore operators dot py I will make a file And we will start with arithmetic operators
What are arithmetic operators? If I say a is equal to 34 And I say b is equal to 4
And I write print Or I write c is equal to a plus b And then I print c
Print function is there Which is helpful in printing in python programming If I run this program
It is 38 If I go to my 7 plus 4 example You will see 11
As you can see 7 and 4 is 11 So this was arithmetic operator
I will make it concise So that you can understand it easily After this we will talk about assignment operators
When I write a is equal to Suppose I write 4 minus 2 So equal to is assignment operator
So I write assignment operators What is it saying? Assign 4 minus 2 in a
If I write a plus equal to I write 3 Suppose I write b is equal to 6
I want to make it easy And I say b plus equal to 3 So I am saying
Increment the value of b by 3 And then assign it to b Means increase b value by 3
And then make b equal to that value So what was the value of b? It was 6
3 will be added to b Means 3 will be added to 6 6, 3, 9
9 will be printed So let's print both of them Here I do print a
And here I do print b So if I do print b here So what will happen?
Now see once 2 came here Once 9 came What is 4 minus 2?
It is 2 And what is 6 and 3? It is 9
So what was added to the existing value of b? 3 was added to the existing value of b Because I wrote b plus equal to 3
What if I wrote b minus is equal to 3? If I say b minus is equal to 3 And I comment this line
And I say b is equal to 6 And after that I say b minus is equal to 3 So I am saying
Subtract 3 from b So what will be 6 minus 3? It will be 3
So b value will be 3 As you can see So I adjust this
I decrement the increment And decrement the value of b by 3 And then assign it to b
In the same way If I make it star 6 will become 18
If I divide it 6 by 3 will become 2 So you see 2.0 came here
But I will minus it again Because I have written decrement here But you got the point
You got the point What are assignment operators? We will talk about comparison operators
And about comparison operators I will tell you a very interesting fact That their return value is always a boolean
Means if I write a equals to And I write 7 greater than 3 So either it will be true or false
So it will be true or false in a Let me explain You won't understand comparison operators
Until I show it in code So I write here Comparison operators
And here you can see Suppose I write here I use something else
Maybe I use d Here I will write d is equal to And I wrote 5 is less than 4
And I print d So I am saying 5 is less than 4 in d
I used a comparison operator Which is less than Is 5 less than 4 true or false?
It is not true What are you saying? So 5 is not less than 4
So what will happen? False will come instead of d So I will run it and show you
You see here false came But if I flip it If I make 5 greater than 4
Then what will happen? What will come instead of d? True will come
So comparison operators always Either return true or false Comparison operators always
Return boolean So great This is comparison operators
Comparison operators are not just greater than They are greater than equal to Either greater than or equal to
If I will make 5 greater than or equal to 5 It will return true And if I will make 5 greater than 5
Is it greater than 5? No it is not greater than 5 It is equal
And it will be false Greater than equal to means Greater than OR equal to
Either greater than OR equal to You can see 5 greater than equal to
I wrote 5 here So it returned true Even less than equal to
Will return true Greater than OR equal to Either less than OR equal to
It is equal to So it returned true So these are our comparison operators
In comparison operators Not equal to also comes I will show you that too
If I say 5 is not equal to 7 So it is true It is true
So it returned true If I say 5 is not equal to 5 5 is equal to 5 actually
It is false So this thing is false That's why
The value of d will be false I hope you got the point Now if I say
Double equals to Which means Is it equal to 5?
Is it true or false? It is true It is equal to 5
So d What will happen? d will be true
Now many people Who are beginners in programming They say
Double equal to and single equal to They have a lot of confusion Because all their life
Equal to means Is it equal to this? They have always learned this
But suddenly When they are introduced in programming They are told
Double equal to Means Comparison operator
And equal to assignment operator So somewhere Their mind starts to blow
But I tell you When you have to check If two values are equal
Or not Then use double equal to Always
Remember this And when you have to do assignment Means
If you have to put a value in a variable Then you use single equal to I hope you got the point
These things are very simple And straightforward Right?
So let's move ahead I hope I could explain to you You learn python from somewhere
That's why I took the opportunity To teach you python
And It's my luck Right?
So let's move ahead Now see here We have seen comparison operators
Let's see logical operators What are logical operators? If I show you logical operators
I write here Logical operators See if I write here
E is equal to And I write here One
And then I write Or Five
Or I say True or false Right?
Either true Or false Right?
So what will happen? What will be the value of E? E's value will be
True Now you will say What is going on?
I don't understand You will say What is this?
For once you will see You will say What is true or false?
So see Where do logical operators Come from?
If you have read Switching circuits in class 12 Which you are taught
In the chapter of semiconductor If you haven't read Then I will tell you
What is this? So if I tell you Either you are a human
Or an animal Either you are a human Or an animal
Right? So is this true or false? This is true
Either you are a human Or an animal You are a human
So if I say You are a human And you are an animal
Or I say You are a human And you are a fish
Is this true? No, this is not true You are not a fish
You are not a fish So For AND
When both things are true Then it will return true For OR
If one of the two Is true Then it will return true
If I say If I say If I say
If I say If I say If I say
If I say If I say If I say
Either this or this Means one of the two Should be true
So it will return true So these are the logical operators So if I
Make its truth table And show you Actually the truth table
Is called So I made this program For you
See how easy it will become To understand for you So you will see
Its output Telling True or false is true
True or true is true False or true True or true is true
But false or false is False So what happens is
If one of the two is true It will return true Otherwise
If both are false It will return false If I make this truth table
For AND This is called Truth table
By the way This is the fancy name Of this thing
Which two operands What value is going to come This is called
Truth table So if I write here Truth table of
Truth Truth Table
Of OR This is basically
Called truth table Which two values What value is going to come
Simple So don't get confused At all
And here AND And I put AND
In single quote So that you can easily Understand
You will know This is OR Truth table
And here What I will simply do I will replace OR
With AND You see By pressing ALT
How I am bringing Multiple cursors Yes
This is called Multicursor functionality I will tell you
What is this So what happens Once
If I Press ALT In my keyboard
And wherever I click There will be one
Cursor And whatever I type
Will come in that cursor This is called Multicursor functionality
Of VS Code If you are in VS Code You can use
Multicursor functionality I hope you liked This feature
If you are a beginner If you know This feature
For the first time You must have liked it You must have enjoyed
So let's print This truth table So see
We printed OR's truth table TRUE OR FALSE is TRUE
TRUE OR TRUE is this Now here TRUE AND FALSE is FALSE
TRUE AND TRUE is TRUE Means both things Should be true
AND means Both things should be true So see when TRUE AND TRUE
Is TRUE Then everywhere else Is FALSE
As simple as that Now we will talk About NOT
About NOT NOT Operates on
Only one operand Means TRUE is FALSE
FALSE is TRUE Now if you Have seen this course
So far I will request you That you like this video
In the comment section below This python course Is helpful
If you find this course Helpful So if I write here
PRINT NOT OF FALSE Then you will see TRUE
So I will show you You will see TRUE And if I say here
PRINT NOT OF TRUE Then you will see FALSE
So this basically Operates on Boolean operators
Operates on Single operand Make TRUE FALSE
Make FALSE TRUE That is called NOT Operator
Write in the comment Someone Who makes TRUE FALSE
And FALSE TRUE That is called NOT Operator
Someone Who makes TRUE FALSE And FALSE TRUE
That is called NOT Operator If you have time
Write in the comment Someone who wins Even after losing
Is called a winner And someone who Makes TRUE FALSE
And FALSE TRUE That is called NOT Operator
Write below Okay We will all learn
From this And we all will be Thankful
I will come back In python handbook We have understood
All the operators Now we will talk About type function
And type casting For which I Will tell you
What is type casting And what is type I have already told you
Integer String None
Boolean These are all types If I want to know
What is the type of any variable How will I do that We will use
This type of code I want to copy it By pressing
Control C Like this And after copying
I will paste it In my program And basically
I will write it Line by line And I will print
Type of A If I write T is equal to
Type of A And I can Print
T And I will show you As soon as I run it
So you see Class int is written here Because this is
Integer Okay If I make it
31.2 So I This time
I have to write Class int This time
I will write Class float Why
Because 31.2 Has decimal And it is
Floating point number Because it is Floating point number
That's why I am writing 31.2
Then it will become A string I will show you
So you see Class str is being printed Look at the output
Class str is being printed What is the meaning Of this
This means That by using Type function
You can print Any type of Variable
Yes Any type of Variable
This is a very good thing That you can Print any type of
Variable The most interesting Thing is
You can change Any type of Variable
In any type of Variable On the condition
That it is valid What is the meaning Of this
If I say That this Is 31.2
Something like this I will write it like this Okay
I can write it As per my wish So you tell me
What will be its type You tell me What will be its type
In the comment section With timestamp Tell me
What will be its type And if you wrote String
Then you are A genius Because we have come
Too far And you figured it out Which is amazing
So kudos to you Because it is in Double quote
So if I run it Then you see Class str is being
Printed here Class str is being Printed here
If I run it again Class str is being Printed
So class str is being Printed It is a very good thing
But I want it As a floating point Number
So can I Change its type Can I say
Something like B is equal to A
But the type Should be Float
Is there any Programmatic way Or I will say
Float As a function And a
So float is a function int is also a function, str is also a function which takes any kind of variable and converts it to float
provided this conversion is legal now if you try to convert harry to float, you will see an error I will show you that too
and to do experiments like this, do use repl but now we are writing b is equal to float a and then we put pound symbol here, which means all this will be ignored
yes, we can write single line comments after any statement in case you don't know we can write like this, this is valid
if you don't know, that should be fine, you are learning you are learning and you have learned, that's a good thing comments are also written like this
to describe this line, write comments like this it's a good practice okay, great, very good
now see what I am saying here we gave it 31.2 and we said 31.2 in float
what will be the return? return will be 31.2 as an integer so first of all, let me do one thing, let me comment this line
and let me show you, if a is equal to 31.2, then what is the type of a? str, okay, very good and if I say b is equal to float a, then what will be the type of b?
this time the type of b has been floated, did you see? this time the type of b has been floated, which is amazing did you understand what this means?
this means that by using type function we can change the type of any variable in this case, we converted floating point number from string to a
which was a string, there was 31.2 in the string but there was a string, so we use type function to find the type
and float, str, int by using these functions we can do type conversion
so here if we do str 31, then 31 will be converted to string if we do int 32, which was a string, then it will be converted to integer we will call this string to integer conversion
if we write float 32, then integer to float conversion will be done so I hope you understood this thing and this is really very helpful
when we will make programs, then we will know the importance of this so these were the functions which convert one data type to another data type
if the conversion is valid now let's talk about input function, which is an amazing function I will tell you how input function works
for this I will make a file, yes I will make a dedicated file with the name 06 underscore input dot py suppose I want users to input numbers
and both of them become sum I can say a is equal to input and I can say enter number 1
this is a very tricky thing and you will enjoy it, listen carefully I will ask you a question, you have to tell me
what is the problem in this program enter number 2 so if I will write a is equal to input enter number 1
then what will happen? number 1 will come inside a if I will write b is equal to input enter number 2
then number 2 will come in b I will print and I will write number a is
after office what will I do? I am pressing alt shift and down arrow key to replicate a line
in case you don't know, it is coming down the key I am pressing is coming down so I have duplicated this line
and I am saying number b is and I have used alt and click I will do a here and b here
means in VS code you can write any way you want, just write quickly because the more efficient you will be
the more value you will get, in total and that's a fact what is the value of a and b?
we will give that, so enter number 1 say 34, enter number 2 67, entered
number a is 34, number b is 67 if I will put colon here then readability will increase, let's run this number again
34 56 and you see below number a is 34, number b is
56, now I will sum both of them I will write print and I will write sum is
and here I will write a plus b and if I gave 1 to a and 2 to b then 3 will come
right? right or wrong? gave 1 to a gave 2 to b, then what will come?
see what came 12 came what happened?
means how did 12 come? and trust me guys, when I saw such a thing for the first time in my life
my mind was blown, I said how can 12 come, sum of 1 and 2 I will cut short the talks
and I will tell you why sum of 1 and 2 came 12
actually this is what happens if you want to guess, tell me in the comment section let me tell
the input of value a is taken in the form of string value of b is also taken in the form of string
so let me show you repl I use repl a lot when I have to write such small programs
if I write harry plus coder
then what will happen? both the strings will get concatenated when we sum two strings pass it from plus operator
then both the strings get concatenated and here you see I did harry plus coder, so it became harry coder
and if I write something like this 1 plus 2
note that this is a string 1 is also a string, 2 is also a string I will hit enter, see what happened? 12 came
what happened? what happened with harry coder? strings got concatenated, why? because these two are strings
but if I write a number, then what will happen? it will become sum so now you understood why 1 and 2 came
if I take any other number, I just want to show you so that you don't get confused if I take 23 and 67, they will also get concatenated
2, 3, 6, 7 it is not like this, it is happening with 1 and 2 so I want this number
to be sum, add up not concatenate so how will I do it?
I will have to convert these two into integers so what I will do? I will say whatever input it is giving me, return it and make it an int
press end and I closed it, I said a is equal to int of input
whatever input is coming in the form of string whatever user is giving me input make it an integer
now see, if I put two numbers let's take example of 1 and 2 oops, here I have to type 1
and here I have to type 3 if I have to type 3, then sum of 1 and 3 is 4 if I do 1 and 2, then 3 will come
if I do 1 and 2, then 3 will come as you can see did you see that?
if I sum 1 and 2, then 3 came how cool is that? amazing
so this is the thing you have to perform type conversions as and when required
and input function is an amazing function straightforward way to take input from the user
an amazing way to take input from the user and that wraps up chapter 2
we had a lot of fun in chapter 2 and now we will see the practice set of chapter 2 in which we will practice the concepts we have studied
let's move on to chapter 2 practice set so guys, our first question is write a python program to add two numbers
so I will make a folder named chapter 2 ps and I will shift right click on this folder and open vscode
here I will make the first program 01 underscore problem 1 dot py and simply I will say
a is equal to 1, b is equal to 5 print a plus b easy peasy lemon squeezy
whatever the two numbers are, they will be added but it is important to make this program because a lot of people will only watch the video
they will not make the program I want all of you to make programs so that you guys do coding in actual
and learn so we are going to question number 2 problem 2 dot py
and here what is problem 2 telling us write a python program to find remainder when the number is divided by z
now this thing is a little tricky it has an arithmetic operator so let's say we have a number
34, we have a number let us say 5 now I want to know
what is the remainder so what I will do here, I will write print and I will write
when a is divided by b is
what will be the remainder it will be a percent b, this is called modulo
operator in case you don't know and this returns the remainder when a is divided by b
easy peasy lemon squeezy once again guys and you see here 4 will come because 4 will be the remainder
and by default the print adds a space when you put a comma you can print multiple things
using print function in case you don't already know and in case you haven't already noticed
I hope you have noticed when I run it, see when a is divided by b is 4
if I make it 37 then what will be the remainder 5 x 7 is 35 and 2 will be the remainder
so will it show me 2, yes simple easy peasy lemon squeezy you just need to know the operator
and in case you are not able to solve the practice set then don't get disappointed because these practice sets are designed to teach you
ok I have kept some learnings in practice set just so that you know
now you see here use comparison operator to find out but before that check the type of variable assigned using input function
this is also very easy, so what I will do this is also very easy I will do problem3.py
and I will write a is equal to input enter the value of a
and I will write here print type of a let's run it
so I entered the same value class str so I can see the type of the same value
I hope that you guys understood it so if I run it like this I wrote 6 class str
because it is string type, always input type is string whether I write harry
or I run it again or I write a number or I write a point
it is always a string so you should know this and you can convert it by type conversion
that is another thing I hope that you got the point now let's go to the next question
use comparison operator to find out whether a given variable a is greater than b or not
let's make a program there are two numbers a and b and we have to find out whether a is greater than b
or not, so problem let's take the value of a I will put int input
so that input will give me number in string and int will type cast it so I will enter
number 1 after this what will happen b is equal to
int input and enter number 2
I did this, you will enter both numbers and after that we will print and write
a is greater than b is a and after that
we will write whether it is a true statement or false statement so we will type is and here we will write
a is greater than b is it true or false, so here we have to take chances of a and
take chances of b, so first I will take 34 then I will take 80 false, a is greater than b is
false, so if I take another number in which a is greater than b, so I took a as 45
and took b as 2, so see what happens here a is greater than b is true and I will tell you a feature of VS code
in this practice set, I want to teach you as much as I can if you drag it and take it like this
then you can make it a tab and here keep doing your work and keep looking at its output so if you run it like this then automatically
you will come in this tab and here you will get your output, so I hope you liked this trick
so here I will terminate it because already this process was going on, I didn't put the numbers so if I run it again then it will come down again
45, 45 this is also false because I have used greater than we just have to find out
whether a given variable a is greater than b or not, whether a is greater than b or not, that's all we have to find out
we don't have to talk about equal to ok, write a python program to find an average of
two numbers entered by the user, very similar program, very similar program ok, very similar program
so 05 underscore underscore problem 6 or
5.py ok, so what I will do I will leverage this code which I have already written, I have written it, it is my code
so I can steal it, in fact stealing is not even the word, I can use my own code ok, and I can use chat gpt
chat gpt is also running here if I will do complex task then easy task I will do with chat gpt, this is called
using AI to your advantage, ok great, int input enter number 1, enter number 2, now what we have to do
we have to find the average, so here what I will do, I will write the average of these
two numbers is and what will be the average we have I will open bracket, a plus b
and divided by 2, it is important to put bracket so that your calculation will be accurate always put bracket and do calculation
never think that python will do board mask python will automatically do it for you
if you are thinking in your mind first multiply then divide put bracket, simple thing
put bracket, whatever will be in bracket will be first so a plus b will be evaluated first then divided by 2, I will run
and see here, I took 45 and after this I took 45 and 55
ok, so see its average is 50 ok, simple write a python program to find
average of two numbers entered by the user we have done it, now what we have to do square of a number entered by the user
and this is even simpler but I will show you so that you don't think that I didn't show you
problem 6.py and here I will say enter the number
here I will say enter the number let's do one thing let's copy and paste
we are not talking about number 2 I will say enter your number enter your number
and the average square of the number square of the number is
and here do a into a do a into a otherwise I will tell you one more way
you can do a to the power 2 by the way you have seen in books, a tolu 2 is written but it is not valid in python
if you press shift and press 6 then this kind of tolu will come and if you write a tolu 2
then this a square is denoted like this in many books but it is not valid in python
to write a square in python note that if you write a tolu 2 then it will not be a square
so I will do a star star 2 then it will be a square so if I run it and show you
so you see here I wrote 4 and here square of the number is 16
if I would have done a into a then what would have happened then also the same would have happened but what happens with a star star
that a to the power 3, a to the power 30 40, how much ever big power I can raise, this is the benefit of a
so let's run this program and if I write 7 here means both of them will give me 49.49
this is also valid, this is also valid what is not valid, tullu is not valid okay
tullu is not valid in python many people make mistakes I am telling you because many people make mistakes
here I will write not valid incorrect for finding square of a number in python
this is incorrect this is not how you square a number in python okay
I hope your practice set was solved very well you solved it it was fun
if you find it easy in the future we are going to raise the bar like anything I am telling you
in the coming time we are going to take things to a very high level but I want all the beginners to hold their hands and take me up
because of which I am going a little slow but remember what I said we are going to raise the bar like anything
and you are going to get all this source code so we have done chapter 1 we have done chapter 1 practice set
we have done chapter 2 we have done chapter 2 practice set I will give you this handbook, I will also give you the cheat sheet
I will give you the source code along with that source code of the project
you are going to get everything I will meet you in chapter 3 Chapter 3
we are talking about strings I had already told you that whatever you put in the double quote
it becomes a string in the python programming language what is a string? it's a sequence of characters
enclosed in quotes let's understand it closely not like this
closely we will understand it well so I will make a folder here in the name of chapter 3
because I like to keep things organized I will click on shift right I will click on open with code
this folder will open in vs code I will make a file by typing 01 intro to strings.py and I will tell you
what is a string? this is a string I wrote name is equal to harry
this is a string okay, this is a string okay, very good
is there any other way to make a string in python? yes you can put it in single quote
that's the same thing you can put it in double quote that's the same thing
you can put it in triple quote if you want to make a multi-line string remember that twinkle twinkle little star example
yes exactly you can put your characters in triple quote
and your string will be made so what is a string? it is a data type in python
it is a sequence of characters and we can primarily write a string in three ways mainly we can write a string in three ways
a, b and c this way we made three strings all three represent the same type of string
now what is string slicing? a string in python can be sliced for getting a part of the string first of all, I would like to tell you
that a string is immutable what does it mean? once a string is made
you can't change that string it means if I want to change one character of this particular string
I can't now don't get confused because I am going to explain this very well
just a heads up I want to tell you string is immutable
that means you can't change an existing string that means if this is a string then you can't put p instead of r
you will have to make a new string with that in mind let's move ahead
and I will tell you how we can slice a string if I write name2 is equal to
or write name short is equal to that means small name is equal to name and I want to get some characters of name
so what happens is there is a concept called index in python in fact, in any programming language
and the counting in programming languages starts from zero so this is my zeroth character
this is my first character this is second this is third
this is fourth okay we can also count in reverse
this is character minus one this is minus two this is minus three
when we count in reverse we start with one when we count in normal
then we start with zero with that in mind let's come back
a string in python can be sliced for getting part of these strings assume this is your string
this is zero this is one this is Two
this is Three this is Four if we do inverse counting
we start from minus one minus one minus two
minus three minus four minus five
this string's length is five because it has five characters one
two three four
five it has five characters that's why
its length is five to get the length of any string we can do something like this
we can say len of name and if I do len of name
then the length of the string will be printed but now I want to show you that how we can get
a short string of a particular string now see what happens here this concept is very important
the index in a string starts from zero to length minus one in python in order to slice a string
we use the following syntax now what you will do slice string is equal to
existing string then you will write index start then you will write index end
what is the meaning of this your mind will be blown you will say
what is this this is very difficult I can't do it
but it will be done I am there you see
come here in VS code if I write here start from zero
then you write colon zero one
two three if I write here colon three
then it means that start from index zero excluding all the way till three
excluding three okay understand
it will go from zero to two so you will have to remember this thing that whenever I write name zero colon three
then it will go from zero to two zero will be included one will be included
two will be included three will not be included that means h will be included
which is zero a will be included which is one
r will be included which is two but the other r will not be included
so h, a, r will be its output if I print name short
okay so I will print name short so here you can see
that h, a, r came okay so this is how indexing works
in strings this is basic indexing you should know
if I just want a character suppose I say character one is equal to
and I write name one something like this I can move this line by pressing alt
just so that you know I press alt I do one thing
that I toggle screencast mode I can move it by pressing alt if I press the alt key
in my keyboard so I will keep pressing alt and I will use arrow keys
so I can move this particular line so I am moving this particular line and if I write print here
and I write character one so you see that zero, one
here its output will be a so the a will be printed why a was printed
because character one is a what is character one from here we counted from zero
we did zero, one first character counting will be from zero
if you do it from the beginning if you do it from ending then counting will be from minus one
remember this I will come back to the same diagram zero, one, two, three, four
minus one, minus two, minus three minus four, minus five okay
so see here we wrote sl is equal to name index start colon index end
index start is included index end is not included it is written here
first index is included last index is not included sl zero colon three
returns h, a, r characters from zero to three sl one colon three
returns a, r characters from one to three one will be included
three will not be included zero colon three means it will go from zero to two
excluding three zero will be included one will be included
two will be included one colon three means it will go from one to two
three will not be included last one will not be included and counting will start from zero
so it is giving a, r because one has corresponding a
and two has corresponding r three will not be included so you should know this
now I will tell you negative slicing which is very easy
and you will also know you will be surprised that it was so easy
it was negative slicing so to explain negative slicing I will come to my terminal
and I will write python here yes you heard that right we will run repl
ok and here I do one thing I also make a program
zero two underscore negative underscore slicing dot py ok
suppose I have a string name is equal to harry and after this I simply
wrote print name and after this I wrote from zero to five
zero one two three four it will be complete so I will do zero colon three
so here har will come zero one or two three is not included
so far we have seen but if I tell you that man
let me tell you one thing that you will not encounter in your python programming journey
negative slicing usually usually you will not encounter
but in some cases you can get some on the basis of code
or it can come in your interview how does negative slicing work then you have to do something like this
see what does the interviewer ask you from minus four
and if we do it till minus one then what will come ok
now you will run you will say what will come
what is minus four what you do the easiest way to do this
what happens you convert minus four corresponding positive index
means you write on paper from here minus one start marking
like I have done here minus one minus two minus three minus four
minus five what is minus four one is positive
and what is minus one four is positive ok
so what I will do that I will come back here and instead of minus four
I will write one ok I will write name I will write one here
and instead of minus one what will I write what is instead of minus one
instead of minus one there is four so I will write four
so see the output of both will be the same ARR ARR
so you negative indices
in corresponding positive indices convert it
no one will tell you I am telling no one told me
when I was in my python programming journey people used to say
count from here count from there minus four means
do this from here hey brother is it negative anywhere
make it positive first after that you do whatever you want
ok now I will tell you one more thing sometimes what happens
you are asked that if you do something like this that is, nothing is written here
and four is written here so what will happen if nothing is written here
it means zero is written if not written here then it means
length minus one is written I will tell you easily while digesting
while doing ok while digesting
so see I will run it so here colon four is written
means zero colon four is written and if something was written like this if something was written like this
one colon blank blank means length blank means what
blank means length should I write one colon or should I write one colon five
it is the same thing ok it is the same thing
now see arry arry so I will write here
is same as and I will copy it here and write
one colon five ok is same as
one colon five and what is this this is same as
the one above if you leave the first blank it is same as
zero colon four zero colon four ok
four so if you see someone doing this
then first of all correct his actions means you will say
brother you are saying colon four to me I will correct it first
what you have written I will write it in plain language I will write it here
so in plain language you write zero colon four if someone tells you
tell me what will be its output print name minus three colon minus two
you tell him keep it cool what will you tell him
you will tell him you keep it cool I will convert it into
corresponding positive indices because you are a negative man you are giving me negative
means don't say like this to the interviewer ok means
I am telling you convert minus into plus ok
the interviewer will trick you you just have to do do it in your mind
what I am saying do it in positive see what will happen with practice
you will do it in your mind but practice first you can tell him
brother I want the paper or you always have in 99% of the cases
you have a computer in your hand otherwise paper is there you can always solve the question
if you are putting MCQ etc then you start marking from here minus one minus two minus three
minus four minus five zero one two three four and after that
convert negative into positive that's about it it will be done after that
ok I hope you understood I know you understood
negative indices in most of the cases not in practical programs
you will get to see ok and in my programs
means if I am working with someone he uses negative indices so first of all
I maybe him if he is my close friend
then I can give him a laugh ok which I keep giving
in joke it also means to improve
and if there is someone else then I will tell him
brother why are you using negative indices but you should know see I know
see to convert negative into positive and the interviewer will ask it's his job to ask
now see here there is a concept of skip value what is this skip value
see what happens like say this word is equal to amazing ok word is equal to amazing
what you do if someone tells you 1 colon 6 colon 2
so what will you do from 1 to 6 first of all go from 1 to 6
0 1 2 3 4 5 6 ok 6 will not be included so what will be the output of 1 colon 6
this will come now what you do in this that
take the first one ok jump two steps ahead
like after m a and z then jump two steps ahead 1 2
and after this you can't jump two steps ahead I will show you in REPL suppose I have a is equal to
0 1 2 3 4 5 6 7 8 9 I have this string now I say
a 1 colon 7 colon 3 what will be the output of this
so if I write here a 1 colon 7 colon 3 then what will happen
first of all we will resolve 1 colon 7 which will be from 1 to 6
means 1 2 3 4 means if I start marking from here then all these indices will come up to 6
so 1 2 3 4 5 6 this much portion a 1 colon 7 will give us ok
so first of all we resolve a 1 colon 7 after this we have given skip value of 3
so first 1 will come then 1 2 3 4 will come then 1 2 3 will not come
so its answer will be 1 4 and you can see that its answer is 1 4
this skip value you start from here 1 2 3
1 2 3 like this keep taking it ok
if I give you an example of a big string so I will give you g h i j k l m n o p q r s t
u v w x y z ok assume I took this and I will make it a is equal to or b is equal to
now assume we do the same thing here 1 colon assume we do 9 colon 4
ok do this so first of all what we will do how did this come
its output first of all let's see bf is its output why its output is bf so first of all we will resolve b 1 colon 9
once we have this then what we will do we will take 4 number we will write b first
after b see 1 2 3 let me zoom in it will be fun let me zoom in
see 1 2 3 4 what will come after b see b is here
1 2 3 4 what will come after b f after b f
so see b will come first then after b 1 2 3 4 f will come then 1 2
3 can't go on 4 so bf will be its output I hope you understood its clear you must have got clarity
see I zoomed in its little bit wrong but you got the point
first resolve this much then whatever number is written here jump that much
like here b will come first then after this 1 2 3 4 f will come 1 2 3 4
go as far as you can I hope you got the point and I hope this was very simple for you
everything is written in notes Now let's talk about some more advanced slicing techniques which I have already told you that if nothing is written here then
put zero in it, if nothing is written here then what to put? to put its length, 1, 2, 3, 4, 5, 6, 7 was the length so here it is 7, if it is empty here then 7
if it is here then 0, okay? I hope you got the point, okay? Now there are some functions of string by using which we can manipulate strings
or we can get some information about them the first and most important function is len so what I will do here is 0, 3, underscore
and I will write str functions.py, the first and most important function is len, okay? which tells you the length of the string
so if I have this string and I will print len of name, then what will happen? that you will get the length of this string which is 5
now see here 5 is printed, means the length of this string is 5, okay? then after this there is another method called ends with, it tells you that is this string
ending with rry so here the string which is written, that variable will come like this like in this case if my name is there, then name will come, so what I will do here
I will print, I will write name.endswith and you see how good VS code is that it is recommending you, below it is suggesting things
ends with written rry, so true will be shown here so this thing will give me the truth because this string harry is ending with rry
is rry ending with a? no rry is not ending with a, so it is false rry is ending with a, rry is not ending with a
so you should know this thing that what is ends with? like ends with, starts with starts with
and here ha if I write ha, will it start with true? now you tell me in the comment below if I write small h, will it give false or true
you are right, it will give false because capital H and small h are different characters this function is case sensitive, you should know this thing
so I wrote name.startswithha I wrote name.endswithrry, there are many methods of string like I tell you, if I write print
and I write name.capitalize so what will happen? it will return capitalized string means if my string is small h arry
so what it will do? it will capitalize it will capitalize if I write h arry r, it will not do anything to r
it will capitalize only h means it will capitalize first character and if you give something else after space, it will not do anything
so many people are confused that does it capitalize every word? there are many methods of string and we will use chat gpt
you should know this and I will tell you how we will use chat gpt we will open a new chat in chat gpt
and we will write give me most used string functions in python
you know I wrote wrong spelling of functions this is beauty it told me about len, str, typecasting
if you lower dot, it will come in lower case if you upper dot, it will come in upper case if you capitalize dot, it will be capitalized
if you title dot, it will do first character of each word means like I told you if I write harry r, it will capitalize a
so like this you can explore many methods do you need to remember them? no, I have put all the methods in python handbook
which will be useful for you so here we will do string.find
this function finds a word and returns the index of first occurrence of that word in the string so what will it do?
it comes on word case index 5 for the white space I hope you got it
now we will replace old word with new word it will replace first occurrence of old word so string.replace will replace all occurrences
let me show you I will write python a is equal to
harry is a good boy now if I write a.replace replace good with bad
it will be bad bad boy it will replace all occurrences so you should know dot replace function
now we will talk about escape sequence characters what are escape sequence characters? escape sequence characters are special type of characters
by the way, before I tell you about escape sequence characters I want you to write two methods in the comment section
you can use chart gpt I am in favor of using chart gpt people think chart gpt is bad
so I am a person who is different from traditional classroom teachers
they say don't use chart gpt because they think you will cheat in exam I am here to take you to industry that's why I am telling you to use chart gpt
and write two methods in the comment section think how much we will learn including myself
so everyone write two methods in the comment section now let's come back to escape sequence characters
if you want to print new line in a string 0 4 underscore escape underscore sequence dot p y
I have written a is equal to harry is a good boy but not a bad boy
let's say I am a noob and I don't know anything I will try to run it
what is this? this is right you can't do like this
you can't you can't you can't do like this
I want a new line after boy you can use escape sequence character use backslash
this is your backslash this is backslash I will show you
this is backslash backslash n is a special character which is manipulated in new line
we have some escape sequence characters which have special meaning backslash n means new line
let me run it if I put a new line after bad will there be a new line?
harry is a good boy but not a bad boy and a new line and space
escape sequence characters are amazing are there more escape sequence characters? yes, there is a backslash t
which gives a space I have escape sequence characters and you should know that
escape sequence characters have special meaning for example, if I want to put a double quote suppose I want to put boy in double quote
python will get confused python will ask where is your double quote python will not know
python will say this is your double quote if you want to print double quote
but if I want double quote I will write backslash double quote backslash double quote will be manipulated as double quote
python will read it as a double quote and if I run it but not a bad boy
double quote this thing is amazing harry is a good boy but not a bad boy
I love the way python is designed if I talk about single quote I can write single quote like this
if I made this string with single quote I have written single quote like this if I run it, single quote is there
if I make this string with single quote same problem will come how will I know if this is single quote
or single quote same thing, single quote will come from backslash single quote so this was our escape sequence characters
I want to request you go to chat gpt and bring some more escape sequence characters
like I showed you single quote backslash t is tab, backslash n is newline backslash backslash is literal backslash
if you have to use backslash literally I want you to go to chat gpt and tell me some more escape sequence characters
in fact more than enough escape sequence characters but go and explore yourself this will increase your productivity
by using AI but before I move to chapter 3 practice set I want to request you to like this video
and write in the comment section this python course is helpful and I will give you a heart
if you write like this, I will know you have come this far and you have completed this course so please like and leave a comment
I will feel good so guys, I am enjoying a lot let's set chapter 3 practice
and make some ambience I will make a folder for chapter 3 practice set and I will shift right click that folder
I will make a new file first question is write a python program to display a user entered name
followed by good afternoon we have to ask user to tell his name so we will input
enter your name we asked user and that name came in name variable now we will print and say
good afternoon and after this we will write name now I will tell you a new way to write name
which is a new method in python, python 3.6 onwards you write here f you have marked string as f
and you can use variables in this string if you have written f here and you have written good afternoon
and if you have written name here then name variable value will come here and good afternoon will be printed
but the condition is that you have marked it as f people used to use format function and sometimes they used to do like this
which they had to do they used to do concatenation like this wrote name, then wrote plus
then put double quote you have written one string so you have to write plus
you don't have to do this now I will run and I will write my name so you see good afternoon harry
if you want you can put comma yes you have to write f if you are using f string because if you don't write f then it will not know that you want f string
and it will be printed like this I have seen this kind of errors in applications and I understand that someone has tried to make f string
and variable name comes now we will put f we are not that kind of developer
we will write very clean code let's move to next question write a program which will fill name and date in this letter
how will we do first we will make a file and here
problem2.py dear name you are selected
and then date I will say print and I will say letter.replace
I will say do this replace the name replace it with name
which is harry this whole string will return and I will
replace this dot replace now you will say what is this
I will explain it again and here we will write date 24 september
24 september 2050 we will run it
dear harry you are selected if you don't want these spaces dear harry you are selected
24 september 2050 what happened first I said
replace name with harry now this whole string then I said replace date with this
so that string means if I don't write this and print it
so date is written like this and placeholder of date but what I did
now I can do chaining dot replace function you can do chaining
I hope you got the point I hope you understood how to do chaining
next question write a program to detect double space in a string how will you detect double space in a string
this is an interesting problem problem3.py and what we have to do
double name is equal to is a good double space
boy so I have to detect this double space so I will write
name.find find method gives us index index of what
of any substring I took a substring with double space as soon as I run it, it will give me index
it gave me minus 1 which means there is no double space if I put double space
I removed double space if I put double space it gave me 15
which means your string has double space
so this is a program to detect double space which tells me if there is double space or not
if it is minus 1, it means there is no double space and if it is not minus 1 it means there is double space
let me tell you find function returns
index of this substring in this parent string in this parent string
what does it mean it means if I write here
goo it will tell me in this string
0,1,2,3,4,5,6,7,8,9,10 11 index if I run it, its output will be 11
so it will say your goo is on 11th index in the same way, double space
is on which index and if it is not it returns minus 1
this is the rule of find method so the method of string is find which returns minus 1 if it didn't get occurrence
and it returns index if it got substring so in this case, our substring
is double space and if there is double space, it means if it is minus 1, it means there is no double space
if it is not minus 1, it means there is double space so this is a double space detector I hope you understood
make your own programs now it is saying replace the double space from problem 3 with single spaces
again, once again an interesting problem I will copy problem 3 and write
problem 4 problem4.py and you see here
harry is a good boy and here what I have to do I have to replace
so I will replace double space with single space and this is a function which will
replace double space with single space double space is gone double space is single space
easy peasy lemon squeezy it was a very easy problem I hope you understood
we used a simple method once again I want to tell you strings are immutable
if you used method on string, then original string will remain same if I print name after doing this
it will remain same so name string will not change and you will say where did this come from
this is very important so I brought this from middle name string has not changed
it has been printed as a new string let me repeat name string has not changed
it has been printed as a new string so this function makes a new string and prints it it does not change your original string
this is called immutability of strings strings are immutable I will write this, I want to emphasize
strings are immutable strings are immutable which means
that you cannot change them by executing or running functions
on them we will see the list which changes if we run a function on them
if you run a function named dot then original list will change remember this
I hope I am able to explain all the concepts so we have done fourth question now we will do fifth question
which is write a program to format the following letter using escape sequence character
now this letter is not formatted properly, we have to use escape sequence character to format it
so problem 5.py and here letter is equal to I will make it small l
because strings are usually started with small letter not with capital, classes are started with capital
which we will study later don't worry about it so how will I format it
if I print it like this then it will look like this it is not looking good, I will run it again
it is not looking good dear harry, this python course is nice thanks, I want a space
after comma, new line sorry, I will add new line from backslash n
after this I want a new line backslash n and I want
backslash t and capital t so I have used some escape sequence
characters and now see how well it is formatted dear harry, this python course is nice thanks, so I have given an indentation
and this python course is nice very good, so here
I have given this space with backslash t with backslash n I have given new line and here also with backslash n
I have given new line and that wraps our chapter 3 practice set I hope you are enjoying
do tell me in the comment section that you are enjoying and I will give a heart to all your comments
you will get a notification that I have given a heart to your comments because if you tell me that this course is helpful
I am going to give you a heart now we will move towards chapter 4 let's go towards chapter 4 with full energy
so guys I am so happy that we have reached chapter 4 so I will close the chapter 3
and shift right click and open the chapter 4 vscode instance
I will click on open with code and I will create a new file and I will name it
01-list.py what is a list and what is a tuple python
so in any programming language if you want to store many similar elements
then you use array you use list so in python programming
this type of collection is called list and we will use list to store things
so list in the simplest form is container to store a set of values of any data type
so you see here friends is equal to apple, akash, rohan, 7 or false so I will write friends here
and here will come apple and after that will come orange and after that
I wrote 5 345.06 false
akash and rohan so you see
this is a list and there are different types of data types you see
apple, orange, 5, 345.06 false, akash and rohan
now if I write here friends and after that 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Now it's upside down, starting from 11, then upside down, 6 to 62, 34 and 1 So if you want to reverse your list, then you can use the reverse method, okay? Great, now what we will do here, we will see the insert, what is insert?
Look, we have seen append, what does append do? It inserts at the end, that is, it joins the list at the end If I have to get stuck somewhere, if I have to get stuck on a particular index, then how will I do that? For example, let's say I want to add a new item, let's say I add an item here
And I want this item's name to be 33333 and I want it at index 3, okay? So you will see here that it is telling me which index you want to insert and what you want to insert Look, it is written here that first tell me the index, then tell me what you want to insert
So you get a hint from this in VS Code So let's say I want to insert 33333, 0, 1, 2, 3 will be its index, so it will be inserted after 62 So look, it is inserted after 62, okay?
So we basically said here that insert 33333, how many 3s have I written? 2, 2, 4, 2, 6, okay 6 Insert 33333, such that its index in the list is 3, okay?
This is what I have said, when I am saying l1.insert 3,333333 I am basically saying that insert it in the list such that its index is 3 If I make it 2, what will happen?
It will be inserted after 0, 1, 2 i.e. 34 It means that its index should be 2 in this list, insert it in this way So where will it come?
Obviously, it will come after 34, as you have seen here, okay? So this is our insert method Then comes the pop method
What does pop do? It will delete the element at a particular index, okay? And it will also return its value
It will delete a particular index element from your list And it will return that element Now we are not seeing its return value
What does return value mean? Return value means that if I print l1.sort, then what will come? If I print l1.reverse, then what will come?
It will be called return value of l1.reverse In the case of list, because it is changing my original list I have nothing to do with return value
Are you understanding this? Because it is changing my original list So I am just running the function and taking a leave
I am saying okay, I have run the function on the list My original list has changed My work is only 35
So now you see here, I will comment it out and show you the pop method So if I say l1.pop and write 3 What is on 3 index? 0, 1, 2, 3
This 2 will pop out Now see, when I print, it will not be 2 It is not 2, see, after 62, 6 is coming
What happened to 2? It popped out 2 popped out And you can see here, if I print it
See what happens, whenever we run a function, it will work Like in this case, what is l1.pop doing? It is changing l1
And it will return something Means if I print it, what will come? If I do a is equal to l1.pop, I will show you this thing
So when I run l1.pop and print it So it will print the value at index 3 Which is 2 in this case
Which is 2 in this case It will print that So you saw here, when I did print l1.pop 3
So the 2 was printed here Because the value on the 3rd index was 2 The value that popped was 2
2 values were erased from the list It was printed I could have written it like this too
Like if I write here, value is equal to So basically what I said Take the return value in a container named value
Means take it in a variable named value It is called a variable, not a container I am telling you container to explain
Value is equal to l1.pop 3 And print value This will also print the same thing
See, it will print 2 only So I hope these things are very easy for you I want to tell you another way to run a python program
If you want to run a python program Then you write python By giving space, whatever code you want to run
Suppose I wrote 02 here And make sure that your terminal is open in that folder In which folder your program is
For example, I have a program in chapter 4 folder I have this program in chapter 4 folder Now what do you do?
Write 02 and give tab See, only one file is starting from 02 Listen to me carefully
Only one file is starting from 02 So if I give tab after 02 If I press tab key
It will auto-complete for me If I hit enter, the program will run So if you want to write the full name of a file in your terminal
And it is a very complicated name Like in this case, it is a very long name It will take some time to type 02
So what do you do? Write 02 and press tab It will auto-complete
But one thing you have to ensure If something else is starting from 02 Then you have to write till there
Until it becomes unique Like if there is a 02 list There is a 02 tuple
So you have to write 02 L and press tab Then it will be able to understand Which file you want to open
Anyway, this is the knowledge of the terminal I come back to the knowledge of python So if you want to print the return value
You can do something like this L1.sort, L1.reverse I don't care about the return value
Because my work is done If I want, I can print its return value But I won't
Because L1.sort will sort my list L1.reverse will reverse my list My work will be done
That's it Was our string changing in the case of string? It was not
And this is the difference That string is immutable List is mutable
So we are able to change the list Because the list is mutable Means the function is running on it
We are able to change the original list But the string is immutable Means it returns a new string
But it can't change your existing string Then comes our remove method Which removes a particular value from the list
So try the remove method yourself I want to give you as a challenge Try the remove method yourself
One method which I want you to try yourself Are there other methods? Are there other methods of list?
Yes, there are other methods And I want you to comment at least one method of list You can ask chatgpt
Tell me some more methods of list It will give you a comprehensive list I want to say one thing
The things I have put in the handbook Those are the most important methods You follow them
Whatever chatgpt is giving you Take it with a pinch of salt Means don't take it so seriously
As seriously as you will take this handbook Because I have put a lot of effort in this handbook I have put a lot of effort in this
And I will give you the handwritten version of this handbook You will get that too So yeah, let's talk about tuples
What is tuple? Tuple is an immutable data type in python Which is like a list
What is the difference between list and tuple? Tuple is immutable List is mutable
As I have already told you The string is immutable You can't change the string
You can change the list But somewhere we need such a data type Which is a collection
And along with that it is immutable So this type of data type is our tuple So let's talk about tuple
How will tuple be made? So first of all we will make a file 03 underscore tuple dot py
So if I write here a is equal to And here in the bracket 1, 2, 5, 6 And if I print and show you
Type of a So a is a tuple As you can see
You can see class tuple here So this is a tuple Why is this tuple?
Because you have made a bracket like this Now if you want to make an empty tuple So you can make it like this
You want to make an empty tuple You make it like this This is your empty tuple
So you remember this thing This is your empty tuple If you want to make such a tuple
Which is empty So how will you make it? Will you make it like this?
No, you won't make it like this I will show you what will happen if you make it like this If you make it like this
Then python will understand That you have written a number Which you have enclosed in the bracket
And you can see This class integer is telling here It's not even python's fault
So you will say I want to make such a tuple Which has only one element
How should I make it? So what you have to do You have to give a comma after 1
Then python will understand Okay, you are not talking about integer You are talking about such a tuple
Which has only one element So as soon as you run it You will get the class tuple
Printed here So here we have made such a tuple Which has only one element
But usually we deal with such a tuple Which has multiple elements And if there are different data type elements
Then there should be no problem So if I write false here If I write Rohan here
If I write Shivam here And if I run it Then you see
Class tuple You can see here Okay
So can I change the elements of tuple? You will comment this below And after putting a timestamp
You will comment and tell me Can I do it or not? If I do A0 is equal to 45
Or do 453 What will happen? Tell me in the comment section below
Just tell me this much Will there be a change or not? And I am glad
You people commented If you commented You have commented absolutely right
Can't change Because The point of tuple is
That you can't change it You can make a new tuple All those things will be done
Tuple You can't change Means
I said A is equal to So it became a stone line Just like a string
You can't even change the string If you said A is equal to
In double quote Harry So it means
A is equal to In double quote Harry will remain
You can't change Its third character Nor can you change
Its fifth character So you made A is equal to This tuple
You made it It is a stone line Okay
It became a stone line So tuple can't be changed This is the main point
Okay Now you see We have taken a tuple here
And we will talk about some methods Of tuple And there are many other methods of tuple
About which we will talk So first of all I will tell you about some methods of tuple
And what will I do here Right now I will print it
And I told you If you do print A This will happen
If you do print type of A This will happen So all these things
I have already told you Okay That class tuple will come
Now we will see Methods of tuple So
04 underscore tuple Underscore Methods
Dot py Okay So I will do one thing
Let's take this Let's reuse Let's reuse
Our This tuple And if I say here
A dot So you see That
If I do A dot count 45
Okay And I write No is equal to
A dot count 45 Print
No Now how will you know That it is changing existing
See You have to remember Which one is mutable
Which one is immutable String is immutable That's why
It will not change existing string Always a new string Will return
And In the case of list Existing list will change
But Here you see When I am saying
No is equal to A dot count 45
See A cannot change By running A dot count
Because A is a tuple Which is an immutable data type That's why
If I run it You see It has told me
That 45 Comes in your tuple Only once
Okay And if I put another 45 It will return 2
Okay Do use chat gpt I say it again and again
And I will keep saying Give me Some
Tuple Methods Using chat gpt
Is very important For you See
It has told me That Count is one
Index is one Okay I have included both
I have also included Index You see
A lot of things Have been told How to make
Concatenated tuple You always see New tuple is being made
Existing tuple is not changing They have also done Concatenation
New tuple is being made Okay So I will do one thing
Here My tuple is equal to A my tuple dot index 3
What does the index method do? Returns the index of The first occurrence
Of a specified value In the tuple If it is not there
In your tuple Then it will raise Value error
Like if I say here I is equal to A dot index
Tell me index Of 45 I will try it
Which index will it tell me? See It will return 011
That's it It will not search further Because it has got 45
Once it has got 45 It will not search further After that
Okay I will show you See
It has returned 1 01 If I do 3424
3424 3424 3424
3424 3424 If I run
It is telling Your 3424 is on 3rd index
0123 Your 3424 is on 3rd index
It is telling Hope you have understood This thing
That 3424 Is on your Index
Sorry 3424 Is on your 3rd index
It is on index 3 Index means Counting from 0
Index means Counting from 0 0123
And we will count From 0 only Because this is
Programming I am not talking Only about python
Later if you see C language Rust
C++ If you see Languages like this
Then your counting Will start from 0 You have to note
The tuple chapter And here The things
That chat gpt Has told you It is very good
It has told How to do Concatenation
You can make a new Tuple by doing Plus concatenation
Then repetition Tuples can be repeated Using star
Now it is telling If you want to repeat You have to make
A tuple Which has repeated Values
Like there is a tuple Now you want This tuple to repeat
3 times You do repeated Is equal to
My tuple Into 3 Membership
You can check If an item exists In a tuple
Using the in keyword This is also very good You can check
If there is Any element In your tuple
If you say Print 2 in my tuple Output will be
True 4 in my tuple Output will be
False How cool is that So use chat gpt
My friends Use chat gpt In your college
If your teacher Is telling you Or your professor
Is telling you Which I have heard That people say
That chat gpt If you don't open Someone else will open
And will go ahead of you Okay So here
You have to use A lot of AI You have to use
A lot At the time of learning Keep a little less
Use of AI So that you can learn Later you have to
Use AI My tuple is equal to You can take out
Length of my tuple You can take out Length
I include it here So if I Print
Length of a So its length See here
It has told 8 You will count 1 2 3 4 5 6 7 8
Okay Then slicing Exactly the same concept
Of slicing Which I told you Just remember one thing
That Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice When you do slicing, a new tuple will be written.
Unpacking, tuple can be unpacked into individual variables. You can do unpacking of tuple. Like if you have a tuple, if you have a comma b comma c is equal to my tuple,
then in a, b and c, 1, 2 and 3 respectively will be assigned. I hope you understood this thing. It's great, it's fun, it's fun, it's fun, it's a lot of fun.
Okay, come back and now we will move towards the practice set of chapter number 4. I am very motivated. Let's go to the practice set.
So guys, practice set 4. The first question says, write a program to store 7 fruits in a list entered by the user. So there is a list.
In it, you have to ask the user the name of 7 fruits and enter it. Did you understand? If you don't understand, I'll explain it.
So what we will do here, we will make a folder by the name of chapter 4 ps. I will close the old folder. I will open this folder by pressing shift right.
And I will make a new file, 01 underscore problem 1 dot py. So what do we have? First of all, what do we have to do?
We have to make a list. And they give a good name. It's not like that.
They give a good name. Fruits is equal to, I have made a blank list here. What will I do after this?
I will say f1 is equal to fruit1 is equal to input. So what will the user do? Enter fruit name.
Okay. What will I do after this? I will write fruits.append f1.
Okay. I will append f1. Then what will I do?
I will do the same thing with f2, f3, f4. Okay. So I will do the same thing with f2.
I will do the same thing with f3. I will do the same thing with f4. How many fruits?
How many to do? Where did the question go? We have to make 7 fruits.
So we have to make it up to f7. Yes, it is a cumbersome program. But I will tell you how you can make it better.
4, 5, 6, 7. One more. Just one more.
So here it is after 4. 5. By the way, I don't even need to change its name.
And I will tell you why. 5, 6. And here comes 7.
And by pressing alt, I will toggle the screencast mode for you first. So that you can see what I am doing.
I selected this. I pressed alt on my keyboard. I pressed up arrow.
And I can move it. You should know how to do things like this. And finally we will print our fruits.
Let's go. And you see here. Enter fruit name.
Banana. Banana. Then after that.
Another fruit's name. Grapes. Then grapes.
Then after that, I don't know any other fruit's name. Tomato. Potato.
Which is not a fruit. I don't know. You can write in the comments.
I don't even know if it's not a fruit. I selected it. I could have selected pineapple or watermelon.
But 1, 2, 3, 4, 5, 6, 7. 7 out of 7. Fruit's list is made here.
So you can run this program as per your convenience. Try it yourself. It will be fun for you.
Let's move to the next question. Write a program to accept marks of 6 students. And display them in a sorted manner.
So you have to accept marks of 6 students. It is exactly the same program. It is exactly the same program.
01. Sorry. 02 underscore problem.
2 dot py. And where these fruits are written. Now I will show you one thing.
You can double click. You can select this. Fruits.
Right click. Change all occurrences. Do you see this?
Learn this. It is very useful. Change all occurrences is visible.
Click on it. And instead of fruits. You write marks.
Okay. Here this fruit. Change all occurrences.
You can also do it by selecting. Change all occurrences. And you can also select by double clicking.
So basically you have to select this. Right click. Change all occurrences.
Now you see. As soon as you did change all occurrences. Here you got multiple cursors.
Press backspace. And you guys write here. Enter marks here.
Okay. Write something like this. Okay.
You wrote. Enter marks here. Okay.
So what will happen? That your first mark, second, third, fourth. We have to take marks of 6 students.
Okay. We have to take marks of 6 students. 1, 2, 3, 4, 5, 6.
And here you see. After that we will print marks. So I run it.
Enter marks here. 23, 1, 2, 45. Then someone got 65.
Then these are my marks. 100. And these are not printed in sorted manner.
So what we have to do? After appending in marks. We have to sort marks.
So what we have to do? We have to write marks in sorted manner. So now if I write it right.
Sorry. If I run it here. And I put here.
Whatever marks are there. If I put. So see it came in sorted manner.
Okay. It did not come in increasing order yet. Why didn't it come?
Why didn't it come in sorted manner? And our type is hidden behind it. The type of this.
What is its type? Its type is string. So because its type is string.
This has happened. So what I have to do? I have to replace input with int input.
But you see. Even after being a string. Sort function worked on it.
It sorted it alphabetically. First comes 2. It wrote strings starting from 2.
Then it wrote strings starting from 3. Then it wrote strings starting from 6. If you have seen in Excel.
If you have seen in MS Excel. Sort function. Or even if you have not seen.
It has sorted alphabetically. Basically. So what we will do?
We need integer marks. So the input bracket we have written. We will change it.
To int input. So I will do change all occurrences. And here instead of input.
I will write input in int and bracket. And again I will open the bracket. Okay.
And I just have to open one bracket. Because the rest of the brackets. I have to put it at the end.
Okay. So I will delete it by pressing the delete key here. So I wrote int.
Then I wrote input. Then I will press the end key. See I pressed the end key.
I will close it by pressing the end key. A little tricky. I have used multiple cursors.
But you should know. How to do it. I selected it.
I did change all occurrences. Then whatever keys I pressed. It came on your computer screen.
You can also do it one by one. If you are a beginner. You are not able to do it.
No problem. You can do it one by one. But I like to do it fast.
Okay. So I will run it here. I have run it here.
And see it is saying. Enter mark share. Let's run it again.
I have run it here. And you see. Enter mark share.
34 came. One came 78. One came 23.
One came 79. Mine came 100. Then I have a friend.
His came 98. And see it is sorted. In increasing order.
So he gave me here. Sorted list of integers. Earlier you saw.
The list that was being printed. Around that. A single quote was coming.
Why was that single quote coming? That single quote. Was coming because.
Because it was a string. Okay. So our problem 2.
Is also solved. Let's come to problem 3. Check that a type cannot be changed.
In python. This is a little mistyped here. Check that.
Tuple type cannot be changed. In python. So let's do problem no.
3. Come on. Solve it.
Okay. Let's solve it. Okay.
Let's solve it. Let's solve it. Come on.
Solve. 03 underscore. Problem.
3. py. And here I will make a tuple.
And I am going to name it. A. And I am going to write here.
34. And in this. Let's add Harry too.
So I will try to change. A2. And I will say.
Do A2. Larry. So what will he do?
There will be an error. You see. Tuple object cannot.
Does not support. Item assignment. This means.
Understand. This means. That your assignment.
You can't do that. In tuple. Because tuple.
Is immutable. You can't change that. Okay.
Simple. Come on. Towards the next question.
Which is saying. Write a program to sum a list. With four numbers.
So we have a list. There are four numbers in it. And what will we do to it?
4. py. So I have.
L is equal to a list. Which has four numbers. And here.
These four numbers. I have to sum them. So I have.
L is equal to a list. Which has four numbers. And here.
These four numbers. I have to sum them. So what will I do?
To sum them. I. L.
I will print. Sum of L. So sum.
Is a function. Which sums. Now what is this sum?
Sum is a built-in function. And here. What can you do?
You can make it hold a list. And it will sum you. So we have.
Some numbers. We have summed them. So I make these numbers.
A single digit number. So that you can even check. So 5 and 1 is 6.
And 6 and 6 is 12. And 12 is 12. And 12 is 12.
And 12 is 12. And 12 is 12. And 12 is 12.
And 6 and 6 is 12. So is it 12? Yes.
It is 12. Which means. That our program.
Has become. Perfect. Okay.
Come to the next question. Write a program. To count the number of zeros.
In the following tuple. So let's copy the following tuple. First of all.
So I will copy this tuple. I will make a new file. I will name it.
05. Underscore. Problem.
5. Py. Okay.
A is equal to this. What will I do? I will say.
N is equal to. A. Count.
What do we have to do? We have to count 0. And print n.
Although it is seen. That there are 3 zeros. But if there were 30,000.
Then maybe. You would not have been able to count. So here.
I did a. Count 0. So here 3 came.
So we had to count zeros. If I had to count something else. Then I could have done that too.
And yeah. That wraps. Our chapter 4 practice set.
I hope. That you understood. Tuples.
And lists. Now I will teach you one thing. Before ending this video.
What will I do? That. I will show you.
That we solved this question. Okay. Write.
Check that a type cannot be. Let's take this one. Let's take the first one.
Okay. Let's take this one. Let's take this one.
This is the question. Okay. And I will start a new chat.
I will say chat gpt. I have. Submitted.
This answer. To this question. Suggest.
Me. I have. Submitted.
This answer. To this question. Suggest.
Me. I have. Submitted.
This answer. To this question. Suggest.
Me. What I did. Right.
Or. Wrong. Now I will hold my answer.
So here was my answer. I guess this was problem number 2. Yes.
This was problem number 2. So I am telling chat gpt. See.
I solved it in this way. Tell me. So he is telling.
Your code correctly accepts marks for 6 students. And displays them in a sorted manner. Here are some suggestions and improvements.
Correct use of list and append. You use a list to store. See.
I did this right. Now he is saying. What are the suggestions of your improvement.
And he told me something. That. I have not read for loop.
So I did not make you do it. But. You got the point.
Chat gpt. Does not just solve the question. He also has the courage to tell you.
Where you are right. Where wrong. And what could have been improved.
That's why chat gpt. Include in your flow. Include in the learning flow.
Do it in the professional flow. Include in the learning flow too. Come on.
This learning is very good. I hope you liked it. If you liked it.
Then. Like it. Share it.
Share it. Share it. Share it.
Share it. Share it. Share it.
Share it. Caitrion is very good. Now I am going to go to chapter 5.
Because that Very important. So guys what is chapter 5. It's a amazing chapter.
That's because. We are going to study about sets and dictionaries. So.
Now what I will do. I will come to this PDF. Which I have made for you.
Dictionary and set. And our existing folder. In the chapter 4 practice set.
I will close it. What are Dictionary and sets. This is also a minority learning.
But. If one is selected. This is also a collection.
This is also used to store data. In python. But.
How is it different from list and tuple. I tell you. The dictionary in python programming.
They are key value pairs. Okay. Like.
If you want to store the names of some students. And their marks. So you can map their name.
With their marks. And like this. Many.
Names and marks. You can store the pairs. What does that mean.
And how to do that. In python programming. Come on.
I will tell you. What does that mean. And how to do that.
In python programming. Come on. Let's learn.
So. What will I do. I will come here.
I will make a folder. Of chapter number 5. Chapter number 5.
And after making a folder of chapter 5. I will open it in VS code. So the first file that is going to be made.
That is. Dictionaries. Dig.py.
So what will I do. If I write here. Let marks.
Oops. Marks. Is not there.
What will I do. I will open this. I will open this.
So. What will I do. I will make a folder.
Marks. Marks. Is equal to.
And I write something like this. And I write Harry. I will say.
Harry's marks are. 100 out of 100. Then.
Shubham's marks are. Suppose. 56.
Rohan's marks are. Which is not a good boy. He could only get 23.
So 23. And now what will I do. If I print.
Type of marks. First I will print the marks. Then I will print the type of marks.
Fine. If I print these two. Then you will see.
And along with that. Class dict has been printed. Which means.
This is its type. Dictionary type. And this.
Dictionary type. Python has recognized it. Python is telling.
Brother. This is a dictionary. The sole use of a dictionary.
This is that. You can play in key value pairs. In short.
The thing is. If you want. Key.
Value. Be associated with each other. And that's it.
You can play in. Key value pairs. And that's it.
You can play in. Key value pairs. And that's it.
You can play in. Key value pairs. Now.
What is the benefit of doing this? I will tell you a little inside. That.
Why is the need of dictionary in python programming? Basically. What happens is.
This lookup. That is. If I have 1000 marks.
1000 students. Then this lookup. Is very fast.
That is. If I. Find marks of harry.
Then. Big O. Of one time.
I will be told. Here. That.
100. Fine. You understand this.
Harry's marks. 100. Shubham's marks.
56. So. If I.
Try to write this thing. In the list. See.
I am telling you inside. You will feel a little uncomfortable. But.
I will tell you. Because it is important to know. If I store like this.
Harry's marks. 100. Means.
I can make a list of list. I can make a list inside the list. List of list.
Is allowed in python. So. I can make this.
But. If we do this. Then.
We directly. Take the name of harry. And not take the 100.
So. and if you write complex logic then it will be computationally expensive anyways these are high level talks so I will delete them
but in total you should understand that dictionary data type is very important in python programming and it is very important for us to understand it coming back to the notes
if you have written key, value, harry code, marks 100, list then you can print its values like this similarly if you write a list then you will get 1 to 9
because list key corresponding is 1 to 9 I hope that makes sense properties of python dictionaries
let's talk about some properties first of all it is unordered second it is mutable
third it is indexed what does it mean? it means first of all order doesn't matter in this
second thing it is mutable what does mutable mean? mutable means we can change
list was mutable string was immutable tuple was immutable
dictionary came in the game this is mutable means you can change existing dictionary you can add things in dictionary
you can change corresponding values of existing keys and along with that it can't have duplicate keys
so this is a very good thing it is indexed means it is indexed
means if you say Rohan then you will get 23 immediately it won't look up one by one
it is not like marks will check is this Rohan? is this Rohan? no, it will be indexed
and corresponding 23 of Rohan will be printed so let's see some methods of dictionary so what we will do is
we will keep our dictionary and after that we will see dict methods so I will write methods here
.py and here I will keep our marks dictionary like this so can key value pairs
be like this? can it be 0, Harry? yes it can be here you can
use string you can use integers now I want to tell you some methods of dictionary
the first method of dictionary is .items so if I print here and I say marks.items
so marks.items what it does is it gives all these items of dictionary
it gives a list of key value pairs and key value pairs which we get are in the form of tuples
now I will tell you so if I print here marks.items so you can see
I got dict items now I can iterate it using for loop which we will read later
next method is .keys if I do a.keys here
if I do a.marks.keys here in this way so I will get marks.keys
you can see I got marks.keys now I will do one thing just so that you have this code
so I will write it like this so what is the meaning of keys? Harry, Shubham, Rohan and 0
which is on your left hand side and on your right hand side 156, 23 are values
and we can print them by doing marks.values so if I do marks.values
so dictionary values are printed so I hope you understood what is marks.values and
what is marks.keys now what we can do is we can update our dictionary
for example if I write here if I write here
marks.update and I will say that my Harry
his marks should be 99 cut one of his marks no one should give 100
so this happens in CBSE if you know then tell me in the comment one mark is cut
99 is given not 100 so you can see here marks.update harry colon 99
so my original dictionary will be updated and this will happen because my dictionary is mutable
so you will get the proof of mutability here if I print marks
and I will comment this values print my original dictionary has Harry 99
so what you do is you write marks.update and pass a new dictionary and in that dictionary
the keys which are matching they get updated and the keys which are not like Renu
I will say Renu is more intelligent than Harry his marks are 100 so Renu will be added
you can see so the key value pairs which are not there will be added where they are not there in marks
if I write marks.update I have written marks.update I have passed a dictionary
so the dictionary which I have passed in that the key value pairs which are there in my marks
they will be updated like Harry 100 so it will be updated and the ones which are not there
like Renu was not there that will be added means the dictionary will be updated
according to this the key value pairs which are there will be updated
it is simple so this was our update method which is a very simple and straight forward method
now I will tell you the get method what is the meaning of get method if I write a.get name then what it will do
it will return the value of the specified keys and value is returned so I will show you here
if I come here and write print and I write marks.get
if I write shivika and I write shivika and if I write
harry then what will happen then I will get the marks of harry which is 100
I have commented this 99 so it is showing 100 now you will say that marks.get
harry and this marks.get harry what is the difference
and this comes in the interview so listen carefully what I am telling you if you say to me
marks.get harry and marks.get harry means you said that
this is marks.get harry this is marks.get harry if you say to me that they are same
then I will tell you that they are not same why they are not same because if I run it now then both of them gave me 100
it is same but if I write harry2 then this line will give me none
but this line will give me error and if you use square bracket then you will get error
it does not exist in the dictionary and if you use dot get method then you will get none
so I will run it and show you you will get key error but here you will get none
I will write here prints none and what will it do
returns an error it will return an error as simple as that
so I hope you understood so these were some methods of our dictionary now I will tell you to go to docs
but better than docs I will tell you to go to chat gpt and go to chat gpt and ask
give me some dictionary methods
and if you write python here then it will be a good prompt give me some python dictionary methods and see here
dig.clear is a very good method it will empty the dictionary another method is dig.copy
it will make a shallow copy of the dictionary you can use some keys to make a new dictionary
in which what will happen the keys will be taken from here and the value will be zero so these are some methods
that you can use, pop is there pop item is there, set default is there update is there, but the examples
I have told you here those are the most important methods rest you can always use chat gpt
and google, like pop method of dictionary is very famous, you must try it out
I will tell you which methods you must try you must know pop method
where is pop method you must know pop method then you must know pop item
so you can see pop and pop item rest I have told you all I hope you understood
and now we will talk about sets what is set set is a collection of well defined objects
did you remember if you are a science student then you must remember set is a collection of well defined objects
and here I will teach you some mathematics you will remember mathematics
and you will remember mathematics because it is based on mathematics it is based on mathematics
if you have seen chapter number 1 in your class 11 I am talking to a science student
commerce students also study maths so you will know that this set
how it works I will tell you from the beginning what I will do here
03 underscore sets dot py if I write here s is equal to
so what will happen it will become an empty dictionary if I do like this
it will become a set so if you want to make an empty dictionary I will write here
d is equal to if you do like this so what will happen
it will become an empty dictionary so I will write here empty dictionary
so this is the way to make an empty dictionary so let me explain you so if you write here python
and you write here d is equal to and then you write
print type of d so this class dict
so how to make an empty set because set is written like this we know that there are key value pairs in the dictionary
there are key value pairs in the dictionary but in the sets you just put values
and both sets and dictionaries are made using curly bracket
so now you will ask me that ok what you said was very good
15 or 32 will make a set with values 15 or 32 and dictionary is written like this
but how to make an empty set because set and dictionary are enclosed with curly brackets
so to make an empty set I will put here e, you can put any name
it should be a valid identifier e is equal to set so if I come back to apple
and this time I will zoom otherwise you will say that why this person doesn't zoom python and then
if I write here s is equal to set and if I print type of s so this class set is giving me
so to make an empty set this is not used this makes an empty dictionary
this is used to make an empty set set don't use s
is equal to a as it will create an empty dictionary
so this is asked in the interview and many people make this mistake and reject the interview
that you don't even know how to make an empty set you want me to hire you understand
so don't become one of them now see what is set why is set used
why is set made in python no element can be repeated in set if you wrote 15, 32 in set
if you wrote 5, 5, 5 twice and if you print s
so you see here let me run this so here what is
our set it will take only once although 5 is written here 4 times
but it will take only once because elements can't be repeated in set so whenever we have a use case
in which elements can't be repeated we use set I hope that is clear
so 15, 32, 54, 5, 5, 5 we wrote here but we got 32, 1, 5 and 54
now you see the order here it didn't maintain so note this thing
if you want order then you have to use list but if you just want
that my elements can't be repeated then you can do something like this I hope that makes sense
now the way I told you about set I told you about list, double, dictionary, sets
I will tell you about 04 underscore set underscore methods dot py
let's go so we will use this set other data type elements can come
like I can write here after 5 can I do this? yes I can
so if I do print s and type of s and run it you see my set is printed
and class set is telling me which means it is valid I can do this, very good
now I will tell you about set methods in set methods most important method is add
repetition is not allowed in sets so if you are using add in sets then what I will do
here I will simply write s dot add suppose I want to add 566, I will print again
s and type of s so it will remain set and 566 is also added
and class set is showing me by the way you can make it as a tab just so that you don't know
if you don't know then let me tell you you can see it as a tab so you saw that the methods of set
in s dot add 566 if I type s then 566 is added
now I will come to chat gpt and I will say give me some most important
set methods in python let's make it set methods in python and it will give me set methods
so here you see we have seen add, after this there is a clear which clears the set and there is a copy
rest difference and difference update I will tell you there is a discard
after this there is an intersection so we will study about all these before that what we will do
we will study the properties of set first of all they are unordered you can't
expect order from set it's not like you gave 1,2,3,4 so you are thinking it will be stored like 1,2,3,4
we have already seen this example our 32 was being printed later now you see here
there is no way to change items in sets once you added in set it's not like you do something else
you can remove that item and add a new one but changing existing item doesn't make sense
because set is a collection of well defined objects which are not being repeated sets cannot
contain duplicate values sets can't contain duplicate values I have already told you
and sets are unindexed means you can't access elements of set by index
it's not like you can count 0,1,2,3,4 now let's talk about operations
on sets, what all operations you can do on sets first of all, you can take out the length of a set you can also take out the length of a dictionary
in case I didn't tell you in dictionary that you can take out the length of a dictionary so let me tell you that if you here
I will write it as D, I don't want to confuse you if I write here 0 colon 1 and 1 colon Harry basically I am making a key value pair
I will close it with curly bracket and if I length of D, you will get 2 here because it has two key value pairs so length works in dictionary and length works in set
length is a function which works in our list, works in tuple, works in dictionary and works in set it will return 4
because length of set is 4, it is returning 4 because it has 4 values we can remove any element and our set will be updated, if we run s.remove, let me show you
if I write here s.remove and I write here 1 then 1 will be removed from my set and if I duplicate it, just so that you know how I duplicated it
I will toggle the screencast mode if you press alt shift down arrow key it will be duplicated, by pressing alt you can take it down with arrow key
like this, these are the shortcuts of VS code, just so that you know if we run it, you can see what we did we removed 1, that's why 1 is gone from our set
after we ran s.remove after running s.remove, 1 is gone I hope you understood this
now I will come back and show you s.pop what pop does is, it removes a random element from the set and I won't say that you should use pop on your set
because it is removing a random element from the set, which is not good in my opinion what clear will do, it will clear your set
and your set will be cleared if you run s.clear now I will tell you about union and intersection if I talk about union in sets, then union means
take those values which are in both sets and I will make a new file for this and if you are a math student, then you must be knowing this
set union intersection dot py suppose I have a set called s1 and its values are 1, 45 and 6 and I have a set called s2, in which values are 7, 8 and 1
and let's take one more, let's take 78 if I say print and say s1 dot union and I say s2, I say s1 dot union s2
then union means take its values and take its values with no value repeated whatsoever so what will happen, 1, 45, 6, 7, 8, 1 was there and 78
so now see here, all these values are there value can't be repeated in this set, this was our rule from the beginning so that rule will also be followed, so what will our union do
union means take the values of these two sets now there is one thing called intersection in sets intersection means, take the common values in these two
so what are the common values in both, there is only one so what will happen in the value of intersection, there will be only one there will be a set in which there is only one
if there would have been 78 here also, if there would have been 78 here also then what would have been its union, union would have remained the same because its values and its values will also be included in union
intersection means, make a set in which there are overlapping values which is 1 and 78 and union and intersection should come to everyone
so at least these two should come to you there are many more operations on sets, but I will say that these should come to you so 1, 6, 7, 8, 45, 78 and 178 intersection came
so this is union and this is intersection I hope you understood this thing so if I run it again and show you, then this is union and this is intersection
rest you do the experiment yourself, if you have any doubt that what will be its output how to do this, how to do that run RAPL in your computer, right click and click on the terminal
and click on the terminal and write python and run RAPL means no one told me that this is free, means sometimes we feel that who should do it run RAPL and see for yourself
you will learn more from that if you ask someone that what will happen if you do this in union do it and see for yourself, I hope that makes sense
there are many more operations in our sets but I will tell you only union and intersection if you are not a math student and you don't want to be associated with math
you are learning python programming only for web development or for AI so for now you can learn only union and intersection before going, I want to explain this diagram to you
suppose the first circle has area A and this circle has area B and this is R1 and this is R3 means this area is R1 and this area is R3
to explain this better, I will take a snip and explain it to you so basically if you see here I will take yellow color
this area is R1 and if I take another color so this area is R3
and R2 is the overlapping area so if I take this color Cn so this is R2
and what is A? A is the whole circle and B is the whole circle I don't want you to get bored by listening to mathematics if you haven't done much mathematics
but this diagram is very important and explains things very well R1 is this much area
so if I talk about R2, it is A intersection this is the symbol of intersection intersection means the values which are in A and B
and this overlapping area is called R2 and this intersection is called B if I talk about R1, R2, R3
then it is A union B means the values which are in both the sets means the combined values of both the sets
after this, A triangle B R1 plus R3 is A triangle B so if we talk about R1 and R3
then it is A triangle B then R1 is A minus B A is the whole area
and B is the minus area so it is R1 and R3 is B minus A
I hope you understood this so in total this is how it works
and if I show you in RAPL and let's say I wrote Python here let me zoom in
by the way, I am going a little more into mathematics but I want to explain this to you in case it is useful to you
so what I will do 1,23,45 and I will write S2
let us say 6,1,3,23 and then
54 if I say S1 minus S2 then it will give me a set
the values which are in S1 will be minused by the values which are in S2 so 1 will be deducted
and 23 will be deducted, so only 45 will be left why only 45 is left? because the value which is 45
is here but not here, so it is S1 minus S2 now the way we saw
that we can minus in set we can also do these operations in set I have already told you about union
but if you come here then you see there is a difference method, there is an intersection we saw union, this subset
this superset, there are two methods what does subset mean? let's say I have a set, now I don't want to go into set theory
because this is a Python tutorial but I will tell you about this subset for example, what will be the subset of this?
1 will be its subset 1,45 will also be its subset subset means
any set will be a subset when its elements are already in this set
so will 6,78 be a subset of this set? yes will 1,2,3 be a subset of this set?
no, because there is neither 2 nor 3 will 7,8 be a subset of this set? yes
why will it be a subset? because there is neither 7 nor 8 so this is a subset
and this will be a superset of that subset so if you think this is too much information
then you can use these two methods I will tell you to use s1.issubset and set1
so if there is any subset then it will tell you or else it will return false
now see, our set1 is 1,2,3 so this is a subset of 1,2,3
that's why .issubset is returning true so I will tell you to spend some time on this
if you want to go to set if you want to learn related to set
then I will tell you to spend some more time on set so that was about chapter no.5
I am so happy that we completed chapter no.5 now we will see practice set of chapter no.5
it is very important practice set is very close to my heart so we will solve this
and then we will move to chapter no.6 I am very excited so let's go to chapter no.5
practice set alright guys, first question of chapter no.5 practice set
so I have closed my existing VS code and I will open with code on chapter 5 PS
and you can see here I will write 01 underscore one dot py
so I have written problem1.py here first question says write a program to create a dictionary we have to make a dictionary
of Hindi words with values as their English translation so you have to tell
values as English translation like I will tell you so what we have to do
words is equal to like I will write help and I will write here help
then I will write chair chair
then I will write cat I will write here cat then what I will do
word is equal to input enter the
word you want meaning of
here a word will come and after that you have to print words
word now see what I have done here this word is a variable
and whatever you will input it will come inside the word and when you will do words
then what will happen if you have written help then words will come and if you have written something else
then it will come and what I will do here enter the word
so you have seen how easy it was to solve this problem first of all we made a dictionary
after making this dictionary we used input we said inside the word
tell us the word you want to know then we put this variable here
words word and it will be printed if I will write a word which is not in our dictionary then what will happen
then you see here this key error is giving me so here is a condition and what is that condition
that word should be in your dictionary alright let's move on to the next question our next question says
write a program to input 8 numbers from the user and display all the unique numbers
once so you have to input 8 numbers and this is a simple classic problem
of sets I have told you the use case of whole set so what I will do here
I will write n is equal to input and I will write enter let's capitalize this
number 1 and how many times you have to do this you have to do this
8 times so what I will do I will use same variable and here
I will take 8 times input and after taking 8 times input what I will do I will write here
first of all we will make an empty set and we know that set in fact an empty set is made in this way
we know this and after this what we will do s dot add and n but no
we have to do int n or we have to do int input because we have to make set of integers and this was the trickiness of this problem
that you have to make integer and then put it in set so yes
and here I will not do number 1 why not I will do like this enter number and I will
duplicate it 8 times 7 times 1 2 3 4 5 6 7 so here I pressed on keyboard
alt shift and down arrow I will show you one more time
alt shift down arrow 7 times and finally what we will do we will print set
so you see here when I will run this program 3 5 3
2 1 1 2 2 and see it gave me 1 2 3 5
because unique numbers were to be displayed did you see how easy it was
and the question number 2 I have framed it in such a way that you get the use case of set
that you will know how to display numbers uniquely can we have a set with 18 as an integer
and 18 as a string as value in it is it possible let's try
so what we will do is 0 3 sometimes to do some things you guys
should run the program like let's say I make a set set.add
18 s.add s.add 18 as a string
let's print I want all of you to comment below and tell me what will be the output
will it be 18 once or twice do put a time stamp so that I know what problem solution you have given
so let me run it and show you I have run it and yes you see 18 as a string and 18 as an integer
both are in your set because these are two different values one is integer and one is
string so yes we can do this so what is the answer yes we can have a set with 18 as an
integer and 18 as a string value in it now see here it is saying
what will be the length of the following set you added 20 then added 20.0 then you added 20 as a string
the answer is simple these are three different values length of s will be
23 so here let me copy the code question number 4
question number 4 where did you go vs code question number 404
underscore problem 4.py and we will print s
sorry length of s let's print s length of s will come
what is this we thought 3 will come but why 2
and this is a very important interview question in python let me explain it to you
it's my duty and it's very important so I will write python here and I will write 1 equals to equals to 1.0
you will think that 1 equals to equals to 1.0 will return false in python programming but actually it returns true
because when both numbers are same python floating point number and integer number shows true
and you can see here it's true now if I take this thing to chat gpt and I ask chat gpt to explain
explain this behavior so what will happen what did I write behavior
now see here chat gpt explained so use chat gpt use AI
see here it is telling see comparison operator it will say that
comparison operator checks values if both values are same then data type doesn't matter to python
so here you can see that 1 equals to equals to 1.0 evaluates to true
because while comparing floating point number and integer python compares their values
and determines that if they are numerically equal then true
so this is python's property and because of that what happened
that the length of this set what will be the length of this set it will be 2
what an interesting question right what an interesting question so if you do len of s
so what will happen it will be 2 very interesting question
and that's why I have put it comes a lot in interviews so you guys
ask this to your friends who think that python knows ask this question
maybe they will get it problem number 5 says s is equal to
if you made it like this then what is the type of s many people will say it is set
no bro this is dictionary if it is s then it doesn't mean it is set are you getting it
understand the ways to confuse s is equal to the interviewer has given you
with an empty curly bracket he is giving you that hope that if you have written the name of the variable as s
then it will be set no bro if I call this
if I call this as iphone 15 pro max then it won't be iphone 15 pro max it will be this AC's remote
so it is the same thing I hope you understood the things what will be the answer to this
I have already told you the answer to this s is equal to do this
if you do type of s then it will be dictionary alright let's move on
to the question number 6 and I will solve question number 5 here quickly
05 underscore problem 5 dot py s is equal to do this
print type of s and it will be a dict because it is dictionary
now what we will do is we will make a place for problem 6 problem 6 dot py
what is problem 6 saying make an empty dictionary allow 4 friends to enter their favorite language
as value and use key as their names like harry python alright
shubham c++ rohan assembly language like this alright
assume that their names are unique alright their names are unique
so what we have to do we have to write a program in which what we will do
first of all we will make an empty dictionary empty dictionary is made alright
then what I will do name is equal to I will do input and I will write
enter friends name alright then
lang is equal to I will do input and I will say enter language name
enter language name and after that what I will do I will add in dictionary
what I will do d dot how to add
I will write d dot update name colon lang and you see here how many times I will do this
I will do this 4 times and why I will do this I will do 1 2 3 times more
means total 4 times why I am doing this 4 times because I have 4 friends
I have to do this for 4 friends you can see here now if I run this program
then you see after running this program assume I am writing here harry
and then I am writing python actually at the end here I should have printed d
then only you will see how my final dictionary is looking so I will type quickly
here I will write harry then I will write python then I will write rohan
then I will write assembly then I will write shubham then I will write c
then I will write divya then I will write cpp and you see here
our dictionary is made name is in key language is in value
harry python rohan assembly shubham c divya cpp easy peasy lemon squeezy now see question number 7
if the names of two friends are same means in this case if the names of two friends are same
then what will happen then nothing will happen just that
our update method will keep updating like if I write here rohan c
then the language entered later will be considered as rohan's language I will show you
like if I write here shubham python harry python
suppose I wrote c for rohan and then I wrote js for rohan so only js will come for rohan
because rohan's key got updated till here when I entered this input till there rohan's language was c
but after that when other rohan entered js then it got updated because I used update method of dictionary
I hope you understood this I hope you understood this when I wrote d.update
then my existing dictionary got updated I hope you understood this next question says
if the languages of two friends are same then what will happen in the program see if the key is same
then it will get updated like here we saw shubham, harry and rohan were our keys
because rohan entered twice and it got updated in the second time once it got set it was c
and then it got updated to js but if the value is same like here shubham and harry are both python
the value can be same because in dictionary identifier is key
like it's identifier is shubham if I want python then I will write
d.shubham then I will get python d.harry then I will get python d.rohan then I will get js
so the value can be same but the key which is our identifier should not be same
or else you will face some problems, basically it will get updated like we saw already
I hope you got the point question number 9 says can you change the values inside a list which is contained
in a set, so it's a very interesting problem, so let's do this and try to update
and can we update or not, we will get to know so this is problem number 9
so here I will make 07 underscore problem7.py
and I will write here the values entered
later will be updated and here in 08 I will write
problem8.py and here I will write nothing will happen
nothing will happen the values the values can be same
I have written here now the 9th problem code I have copied on clipboard
so I will write problem 9.py and I will paste it here
and what we will do we will try to change it how to change it
I can't do it now tell me what do you want me to do should I do this
0,1,2,3,4 but I told you there is no indexing in this set but suppose some idiot
try to do 0,1,2,3,4 and after that he says to make 0
is equal to 9 python will say what are you doing what did you eat, what did you drink
what did you blow, so this will say python you can see here S40 is equal to
9 is written here he is saying an hashable type list he is saying that first of all
list can't be a hashable set because list is not hashable second thing is
even if it is you are not allowed to do it so there are two reasons
we can't update first thing is we can't include list in a set
second thing is if we could do it we can't change value by indexing
do you understand so if I hold this chart gpt and say to chart gpt
give me more wisdom around this I will give him the whole question no you cannot change the value inside a list contained in a set
infact you can't even have a list as an element in a set because sets in python require all the elements
to be immutable and hashable do you understand this all the elements in your set
should be immutable and hashable lists are mutable I have told you what is mutable
it can be changed and not hashable so they cannot be added to a set
now he is saying whatever you want to do he has given more wisdom
that's why I am saying chart gpt or whatever AI is use AI to your advantage
use it to learn fast if you don't understand anything ask chart gpt to give you wisdom
he will give you wisdom do you understand I hope you understood
I hope you understood everything in the coming time
we will do more powerful projects but for now let's move to chapter 6 see you there
guys chapter 6 is about conditional expressions now what is conditional expression see what happens sometimes
we do our work according to certain conditions for example let's say user entered 7
maybe we want to show him that you entered an odd number and if someone entered 6
then we want to show him this is an even number so to make such programs
we use conditional expression I will explain with some analogy I will explain with my temper
I have made chapter 6 folder I will shift right click and open with code and this folder will open
so the first program is conditionals.py what is conditionals
conditionals is if, else, elif in python why do we use it, how do we use it
see what happens sometimes you work according to certain conditions today is sunday, let's order food from outside
we can't order food from outside everyday infact it's not healthy so let's do it on sunday
let's eat ice cream on sunday maybe we won't eat ice cream everyday but let's eat ice cream today
so this is a condition that today is sunday let's eat ice cream today is my friend's birthday, so I will give him a gift of 10,000
no one gives a gift of 10,000 but again, if someone is giving a gift of 10,000 then on birthday
it doesn't mean that he will give it everyday it's a condition that it's your birthday that's why I am giving you a gift
otherwise your face is not so good that I will give you a gift everyday so here sometimes we want to play pubg on our phone
if the day is sunday let's play pubg on sunday, we study the rest of the day like this
why do we order ice cream online if the day is sunny if the day is sunny then we will say that it's too hot
the temperature is 40-50 degrees let's eat ice cream sometimes we can go hiking
if our parents allow us or mom says that there is no need to go hiking then we won't go
this is the type of scene this happens in python programming also that we want to execute some instructions
only when a particular condition is met
one more time in python programming we must be able to execute instructions on a certain condition
being met and this is what the conditionals are for
till here we understood now what we will do is we will understand this in code
suppose I said user enter your age enter your age
and obviously the input is string output
so we have to type cast it to make it integer so I put a is equal to int input
now I can say if if means if the age is
greater than 18 I will put colon this is the syntax of python
understand things here I put colon colon means
you have entered now as soon as I press enter my cursor didn't come here
it came here and this space is called indentation in python
one more time when I wrote if greater than 18 I put colon
which is the syntax of if as soon as I hit enter it will automatically give me this space
this space is put in python to indent indent means you have entered
inside the if I wrote print you are above the age
of consent and after this hit enter and come back
means you were inside the if this space means you are inside the if to come out of if
you have to come here and else you do print
I will copy this I will copy this you are below the age of consent
ok, now let's understand this program I will toggle the screencast mode for you
if age greater than 18 then we are inside the if you are above the age of consent
and if I want to write another line inside this if suppose I want to write good for you then I can write that
but I have to be inside this if means I have to give this space space means you are inside the if
you must have seen in English that it is done like this that we are inside this heading
so indentation is given you must have seen this thing in books that there is a heading
then whatever is written below is written with a space and when the second heading starts
then we come back here indentation is used in python and by using this indentation
we can come inside the if inside the else and outside the else
now here this if is connected to this else if your age is greater than 18 then this will be printed
if less than 18 or equal to 18 then this will be printed so I will make it greater than equal to
just so that greater than equal to let's run this program now someone will ask me to enter your age
do you know? I entered 56, you are above the age of consent, the spelling of consent is wrong, consent or good for you ok, you are above the age of consent, good for you, so if I run it again and write my age as 5, you are below the age of consent
ok, and here I correct the spelling of consent again, and if I write 0 here then what will happen? if I write 0 then what will happen? so you see you are below the age of consent so what will happen is that the program's control, once we get only one value, the program will enter this if
it will say that whatever it has entered is greater than 18, if yes then print this means execute whatever is inside if and ignore the rest else after this our control will come here directly, control means the program will come here directly
let's say I write end of program, let me run it and explain the flow, let's say I wrote 56 here now you see what happens if I write 56, you are above the age of consent, good for you means if this condition is true then the program's control comes inside this
and it executes the instructions inside this and ignores else because when the condition of if is true then else does not work else works only when the condition of if is false, so because the condition of if is true here so it will run and after running the program will jump outside the else means here
that's why end of program was printed, I hope I explained this to you, this is if else now I want to tell you about elif, sometimes what happens is that we do things on the basis of multiple conditions let me tell you the use case of this, first of all I will check whether 1 is greater than or equal to 18 or not
if it is greater than or equal to 18 then I will print this, but think if someone writes his age as minus 78 then also he is saying the same, I want if someone gives negative age then he will get a heads up and the program will tell him that what happened to you, why are you writing negative age, negative age does not happen
means have you ever seen minus 8 years old, now don't write in the comment that I have seen, you must not have seen now you see here that if I want to give a head up to my user, it sounds weird but I want to put that why are you putting negative age in my program, why are you playing with my program
then I will write elif, I will say elif a is less than 0 means if a is negative and I will put colon I will hit enter and I will write here that you are entering an invalid age and now let's understand this program, this if, elif and else are connected to each other
first thing you have to understand that if you wrote if then elif and then else then these three are connected to each other and these three work together, how do they work together first the condition of if will be checked, once the condition of if is checked then what will happen
if it is true then this will be printed and these two will be ignored but if this condition is not true listen carefully, if the condition of if is not true then what will happen that then your this will not work because the condition in your if is not true then it will check the condition of the next elif
if this is also not true then after this if there is any elif then it will check the condition of that suppose I want to put an elif here and I want that if a equals to equals to 0 means if a is equal to 0 you are entering 0 which is not a valid age, so here I want that if my condition is true then this will be printed
otherwise if someone put a negative age then I will tell him that don't put invalid negative age I wrote invalid negative age then if someone put 0 then I want that you are entering 0 which is a valid age and after that if any condition is not matched then this will work, so these four work together
so this is called if elif else ladder, I will write here if elif else ladder so we will do one thing here, we will make a program called 02 underscore if elif else ladder dot py and I want to keep it readable, if elif else ladder and I want that once we finish this course
then you will have everything properly and in conditionals I want to put only if or else here I want to put only if or else, this is our ladder, this is called if else and this is called if elif else ladder this is our if else, if else statement, I want to document things properly so that once we finish this course
then you will have a set of programs like a resource, int input enter your age, once your age enters let me run this program and show you and then I will tell you how it is running so here enter your age is asked, suppose I put 78, as soon as I put 78 then the condition will match
that is, the condition inside if will match, 78 is greater than 18 and because it is greater than 18 so these two lines inside it will be executed and because they will be executed so this elif, this elif and this else will be ignored, very good, now because this is executed
and this is ignored so after running this the program will come to the end of program because this print end of program is not inside any if, elif or else, look carefully this line is inside if because there is space after if, this line is inside elif because there is space
I will call it indent, not space, I will call it indent, its name is indent elif is written here, this line is inside this elif because indent is written here indent means this space, this line is inside else because indent is written here
very good, we understood, this line is outside because indent is not written here this line is stuck here, now you see here that you entered age, if condition matched the masala inside if was executed which was two lines, both the lines inside if were executed
and after this end of program came, what came? end of program, very good, it was fun now what I will do, I will run this program again and I will put minus 78 now when I put minus 78, this condition did not match because minus 78 is greater than 18
and it is not equal here, so it will check this condition in elif this condition is true because minus 78 is less than zero so this condition is met here and because this condition is met
that's why this is printed and once if, elif, elif, elif, all the elifs and else if any one condition is matched, then the rest is ignored so because this condition is matched, the rest of the elif, else will be ignored
and our control will come here directly and you see here that you are entering an invalid negative age, end of program, ok, end of program came here now you see that I will put zero and show you here and if I put zero here
then you see here it is saying you are entering zero which is not a valid age that why are you putting zero, ok, so what happened, this condition here it did not try to match, this condition did not try to match
this one matched because it was zero, that's why it printed this and directly came out of this ladder and printed this, ok so only one thing will be executed from the ladder, ok
now see here, I will run it and show you and if I put some number here and which is 4, ok, so this condition did not match, this condition did not match this condition did not match, you are below the age of concern came
end of program came, I hope you understood this thing let's come to the notes, I have written exactly the same thing in the notes that if else and elif statements are a multi-way decision taken by our program
due to certain conditions in our code, ok, so this is our code I have told you that what happens if there is an elif then here I have given a code example of if else
now you have to do a quick quiz write a program to print yes when the age entered by the user is greater than or equal to 18, what you have to do is
that you have to write a program to print yes when the age entered by the user is greater than or equal to 18 very simple program, in fact, I have written a similar program here
so you may not need to write this program because I have already written it, it will be copy-pasted but write a program which will print yes if the age entered by the user is greater than or equal to 18
otherwise you can print no, ok so you have to do this, ok do this and tell me in the comment section by writing a timestamp
that I have finished the quick quiz I want you to tell me in the comment section below that I have finished the quick quiz now let's talk about relational operators
what are these relational operators relational operators are used to evaluate conditions inside the if statement some examples are equals, greater than equals or less than equals
I have already told you about relational operators that equal to, equal to, greater than equal to these are also called comparison operators
comparison operators and relational operators are the same thing so you should know this, I have already told you we have also talked about logical operators
and we have discussed about AND, OR, NOT we said that AND is true when both are true means if both are true then AND will return true
means if I do true AND true then it will return true otherwise it will return false true AND false will also be false, false AND true will also be false
false AND false will also be false but in OR there is no such case, OR needs one operand true to be true means true OR false will also be true
false OR true will also be true true OR true will also be true but false OR false will be false and what does NOT do? it inverts
true to false and false to true, we have talked about these three now I have explained you about the Elif Clause that how many conditions you can have
you can check one by one so I have already told you about the Elif Clause and Elif is not necessary for you to write
you can write only if else means you can write such a program where there is only an if and an else and you can also write such a program where there is an if
and an else I will give you one example which confuses a lot of people and after that we will wrap up this chapter
that example is of 03 underscore multiple if statements
example of multiple if statements I will write here if age is greater than 45 and I am going to write the same program
I am going to write the same program here and then I will explain it to you and here if age is greater than
we wrote 18 here we wrote if we wrote Elif here we wrote Elif let's say I write something like this
I put two if here I write here if a percent 2 is equal to equal to 0 means if a is divided by 2 and the remainder is 0
then print a is even and after that I did this so how will this be executed
I will explain this to you the first thing you should do is which if, which else and which Elif
determine this first if you see this program then this if after this there is no else and after this there is no Elif
so I will name this if statement 1 if statement no.1
and I will name this if statement no.2 so if statement no.1
and if statement no.2 so if statement no.1 is this and if statement no.2 is this and you can see here that this will run independently
and this will run independently I hope you understood this a is greater than or equal to 18
these three will run independently and I will do one thing I will write here end of
if statement no.1 and I will write here end of if statement no.2 here I wrote end of if statement no.1
and I will write here end of if statement no.2 so what happened here is this statement will run independently and this statement will run independently
so I hope you understood this if there were more if else statements the first thing we would have to do is
how many if statements will work together we will segment them basically if I enter 18
or if I enter 28 so what will happen in 28 first this statement will run
and this will say a is even and after that this whole ladder will run in this what will happen a is greater than 18
this will run, this will ignore, this will ignore and then end of program will run you can see this so you will have to understand these complex if else statements
you can run this program again because you will understand only if you run it again there could have been more multiple if statements
if I would have put one more if statement after this then it would have run independently if I would have put one if statement between these two
then it would have got that if statement so you have to determine which if else will work together
because some if statements are independent like here if is independent if doesn't need else, if can be alone
else can't be alone but if I just write else here, else print see here it is showing me an error
suppose if I do like this, then it is saying whose else is this? whose else is this? it won't work if I run this it is saying this is an invalid syntax, whose else is this?
so else can't be alone elif can't be alone elif can't be alone
if I do elif a greater than 5 then elif can't be alone ok
so I hope you understood this thing so here our if statement is working with this if statement
and after this this end of program is working on its own so I hope you understood this thing
so I will come back to my notes and you can see here
there can be any number of elif statements e will be small here in elif e is small
and the python programming is case sensitive last else is executed only if all the conditions inside elif fail
means the last else is executed only if in this case, if I show you
this else will be executed only if this, this and these three conditions can't be executed
ok, I hope this thing is clear to you and this ends our chapter no. 6 we are moving ahead at a very good pace
I am really very happy about how good things we are doing it is fun here
I am getting enthusiasm I will meet you in chapter 6 practice set music
so guys for chapter 6 practice set I will make a new folder I will close this
and here for chapter 6 practice set I will make a new folder chapter 6
ps and here what we are going to do we will do the first question of chapter 6
so I will open this in vscode by right clicking and I will solve the first problem which is write a program to find
the greatest of four numbers entered by the user so what we will do here
we will solve this question very easily problem 1.py and here we have four numbers
so I will say a is equal to int input and what I will do here
I will write enter number 1 and here I will
toggle screen cast mode and show you how I am duplicating
and I will do one thing a1, a2, a3, a4 and I will do 2, 3 and 4
we have four numbers a1, a2, a3, a4 we have to find which one is the biggest
so I will put an if statement and I will say if a1 is greater than a2
and I will write and and a1 is greater than a3
and I will write a1 is greater than a4
which is the greatest number I will print number is
a1 and I will print a1 so I will say
a1 is the greatest number but if this thing is with a2 means
a2 is a2 is greater than a1
a3 and a4 so what we will do we will print a2
so our a2 will be the greatest number but if our if and I will write
because I have to do one thing I have to print a2 so if a1 is greater than a2
then it will be a3 a1, a2 and from a4
then our a3 will be the greatest otherwise our a4 will be the greatest
I want to show you one thing even if you put only LFLF then also it is a valid program
so if I put a4 here and a4 is greater than a3 means a4 is greater than
a1, a2 and a3 then a4 will be the greatest so let's test this program
and then you will understand how I wrote it run it and after running suppose I took one
two, three and four and greatest number is a4 if I run it again
and if I enter more numbers even if I enter negative numbers and if I do this
then it is telling me greatest number is a3 2, 3, 4, 3 so you can see it has told me which number is the greatest
and which is not let's understand it again first of all I took a1, a2, a3 and a4
as an input from the user using input function because I know that output of input function is a string
that's why I type cast it from string to int all four numbers are in integer form
then I saw that if a1 is greater than all numbers then a1 is the greatest
if a2 is greater than all numbers a1, a3 and a4 then a2 is the greatest
if a3 is greater than all numbers then a3 is the greatest if a4 is greater than all numbers
then a4 is the greatest simple as that so our problem no.1 is solved
let's move to the second problem passed or failed if it requires total 40% and at least
33% in each subject to pass and we have to assume three subjects and user will tell the marks
so what is that we have to take input of student's marks in three subjects
and after taking that input we have to see that all subjects should have 33% i.e. in every subject
33% should be there out of 100 and if we calculate total percentage
if you don't know how to calculate percentage see I will tell you very easily very easily
because my purpose here is to explain python to you and if small maths are coming in between
then explaining that is also my work so what I will do is marks1
int of input this int of input you can make a knot
you can make a knot that you have to put int whenever you have to take a number enter marks1
and same thing we will do with marks2 and 3 so here I will put marks2
I am using multi cursor functionality of VS code I will press alt wherever I will click, cursor will come
see I have done here, now if I will do 3 I can do 3 on both sides, I could have put 50 cursors very good
now marks1, marks2, marks3 have come to us, first of all what we will do check for total percentage
which should be 40% so what we will do, this is marks out of 100 we are assuming this is marks out of 100
so what we will do we will say total underscore percentage is equal to
always name of big variable which is descriptive, you should use that I will not keep name of this variable as T
I will keep total underscore percentage and whenever we talk about our complex programs there we always
name of our variables even if they are big, but we have to keep descriptive name of our variables
descriptive means after reading it, we can understand total percentage
so you should know this now what we will do, marks1 plus marks2
plus marks3 now I will show you a trick if I will write m3
so it will automatically suggest marks3 here VS code is telling me that I have suggested marks3 because
you have written m3 and marks3 m and 3 are matching see m and 3 are highlighted
no one tells you this, I am explaining you with nitty gritty if you want to write marks3, write m3
if you have a big variable, total percentage write tp and enter it, it will automatically suggest I will show you, let's say I have
total percentage, I will write tp and enter it so write name of big variable and use ID like VS code
it will definitely help you now total percentage will be divided by 300 see all my marks
I have summed them see total marks 300 out of 300
you understand this if you have 300 marks so individually if your marks1, marks2
and marks3 are coming, let's say 70 so all three will be summed and divided by 300 now I will say
if total percentage is greater than equal to 40
I will write print you are pass ok
otherwise I will write else I will print
you failed try again next year, ok
this is harsh try again next year it feels weird
total percentage should be greater than equal to 40 only then you are pass or else you are not but
we don't have to see only this we have to see individual subjects too let's say
one student got 40 marks then 41 marks then 23 marks
you failed, try again if I run this program again 66, 78, 21 marks
still he failed because 40 marks is not much
but if I do here 100 again and 33 or 31
still he failed let me check 230 divided by 300
ok I have to do this into 100 I have to do this into 100 I have to do this into 100
so I will say total percentage is 100 multiply
100 multiply by this divided by
300, ok I have put brackets so nicely I don't need this
I said multiply 100 by its sum and then divide by 300 you will get percentage
total sum divided by 300 you have to do 300 into 100 for percentage now let's do
21, 100 you are passed but
if someone got 21, 33 and 21 then he will fail because percentage will be bad
so let me do one thing I will write his percentage so let's do one thing
let's write tp here and here let's write his tp here
so that we can know total percentage so see here I wrote 234 wrong
it was 100 but let's run it again this is 2% marks
who is this useless kid who will get 2 out of 100, 4 out of 100 and 1 out of 100 but I know people like this
I won't take their name because what is the use of taking their name
let's implement this thing again at least 33% should be there in all subjects
so what we will do here, total percentage should be greater than equal to 40 and marks 1
should be greater than 33 and marks 2 should be greater than 33
and marks 3 should be greater than 33 ok should I put equality
what is the meaning of this should I put equality it will be passed even if it is 33
33 should be there in all subjects and total should be 40
total percentage should be greater than 40 and individual marks should be
greater than 33 in all subjects or else you will be failed now see here
21 marks in one subject but in other two he got 100 even after getting 73%
why he failed he failed because he didn't have 33 in all subjects
but there is one more person who got 34 34 and 100 marks he passed
because his total was 56% and he maintained more than 33 ok, so if I come to the program
let me tell you this from programming aspect first we input marks 1, marks 2, marks 3
as an integer then we get the total percentage which is sum of the marks divided by 300
and multiplied by 100 ok, so I intentionally wrote this formula
I could have removed 2, 0 and 100 but I want you to understand the mathematical aspect then we said
total percentage should be greater than or equal to 40 and individually in all subjects marks should be greater than or equal to 33
then you pass or else you will fail we showed the total percentage
I hope this question is clear and you understood how we used it now the question number 3
is very close to my heart because in this I will tell you about IN keyword IN keyword
and don't say that I know these comments I didn't tell you in chapter 6, why are you telling me in practice set
when I am making you practice set chapter 6, there are learnings in that too after that you will be able to do all the questions related to this, so I will tell you about
IN keyword a spam comment is defined as a text containing following keywords
if this word is used in any comment, then it will be considered as a spam it happens that people say come on telegram, I also get comments on youtube
a lot of comments say hi code with harry 1 code with harry 77
and replies to people in the comment brother, I want to give you macbook pro on telegram
what is this I want to give ferrari, I don't have ferrari how will I give it to you
some people are innocent, get stuck in it so how to detect such spam comments we have to write a program for it, so first of all
what will I do here, these phrases I will take them and I will make them
in the name of p1, p2, p3 see how I make this program and after that you will know
problem3.py after that you will know that I will change all occurrences actually this is not a double quote, this is some other
character, so by changing all occurrences I replaced it with a valid double quote and
and I will do this too change all occurrences, backspace double quote
and now what I will do I will save them in the name of p1, p2, p3 I will save them in the name of p1, p2, p3
I will save them in the name of p1, p2, p3 I will write here p1 means face1 is equal to
and here also I will p2, p3 and p3 here and p4 here
p3, p4 so I have made 4 faces here now message is equal to
input, message is a string enter your comment message is a string, you will write a comment
then I will say if p1 in message or p2 in message
or p3 in message or p4 in message and what is the meaning of this
I will do one thing, I will put brackets here just so that readability increases so I will do this
and I will do this and this and understand this, what does in keyword do
I would like to come back to RAPL whenever I have to explain something, I come to RAPL to show you let's say I have a string A is equal to Harry Bhai
is a good boy now if I write if I take a string Harry in A then it will be true
if I write Shubh in A then it will be false because the string Shubh does not come in this
so if this string comes in this then it will be true or else it will be false so you should know this
so now you tell me in the comment section will it be true or false if I do capital H-A-R-R-Y in A
I have to tell you in the comment section, I am waiting for you and please keep two way interaction you guys do comment
yes you are right, it will be false because its H is capital and it is small this string is not its substring
this string is not its substring this string is not its substring because its H is capital and its H is small
I hope that is clear, now I will bring you back to the question now I will bring you back to the question I will type colon and enter and I will write print
this is a spam this comment is a spam otherwise what will happen
else I will print and I will write comment is not a spam
so we have made a kind of spam filter here this kind of logics are written, this kind of logics are written in youtube in the back end of youtube
which one is spam and which one is not, it is written in a similar way let's run it write a comment
hey I am good, please buy now this comment is a spam if I write nice video bhai
nice video bhai, so this comment is not a spam but if I write nice video bhai click this, so this will be a spam
because in this click this comes, this is a very old implementation of spam filter, a long time back spam filters were written like this, now a days they are written through AI
but in total this kind of things can be done by using if-else statement, so I hope you understood that why we did p1 in message, p2 in message, p3 in message
and what is the benefit of doing this I will move to the next question write a program to find whether a given username contains
less than 10 characters or not, super simple question super simple question 04 underscore problem 4 dot py
and here you see what we have to do we have to take a username name is equal to input
we don't need int this time enter username if username's length
if len of username if len of username is greater than what was the question, contains less than 10 characters or not
less than 10 your username contains less than
10 characters else all is
well so if my username is harry your username contains less than 10 characters
I will make a colon space, it will look good when I type I will run it as soon as I type all is well
all is well I wrote it randomly we have to detect here that username is less than 10 characters or not
I will write it properly your username contains more than or equal to
10 characters super simple question write a program which finds out whether a given number name is present
in a list or not and in keyword list I will write here
05 underscore problem 5 dot py I have a list with l name and it has 5 names, one is harry
one is rohan, one is shubham and one is divya name is equal to
name is equal to input enter your name
if name in l print your name is in the list
else print print
your name is not in the list your name is not in the list let's run this program
if I write harry with capital H then it will show me your name is in the list if I write here roshni
then roshni is not in the list if I write here shub if I write shubham with capital S
then your name is in the list if I write divya in small letters then your name is not in the list
divya in lower case is not in the list if I decapital divya then it will show me divya is in the list
your name is in the list this was a simple question we can use in keyword with list
we can use in keyword with string now you know about in keyword write a program to calculate the grade of a student
from the following scheme from 90 to 100 marks from 80 to 90 marks
from 70 to 80 marks in IIT kharagpur this is a mark
this is a A we can do like this this is a mark
finally we will make problem number 6 06 underscore problem6.py
what we will do marks is equal to enter your marks
then what we will do if marks is less than equal to 100 less than equal to 100
and marks is greater than equal to 90 greater than equal to 90 then what we will do
great is equal to ex then what we will do and we will make great is equal to empty
and we will make this also empty and see what i am doing how many times i have to replicate
5 times i have to replicate so i will select this and like this 1,2,3,4 total 6 times i have to replicate
10,9,8,7 7,0,6 6,5
8,7 6,5 5
less than equal to 50 less than less than
less than great is equal to great is
great is greater than greater than
greater than greater than greater than
great is great is your greatest
or your greatest your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
lower lower lower
lower lower Harry
Harry Harry Harry
through through through
through Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry By the way, keep in mind, we used to write 1, 6, so it used to be 1, 2, 5
So here if I print and write i, then what will happen, the value of i will go from 1 to 5 And what does this range function do, see 1, 2, 3, 4, 5, it printed exactly the same thing Now come back to the PDF
Sometimes we want to repeat a set of statements in our program Like we have to print numbers from 1 to 1000 Let's make it easy for the loops programmer to do this
We have two types of loops, one is a while loop and the other is a for loop The loop I just showed you was a for loop, we will talk about it in detail But now I just wanted to give you an introduction to it
So let's start without wasting time with while loops So while loops.py is a .py extension, a python file Okay, now we will come back to our PDF
What is a while loop? Okay, while loop and for loop are two different types of loops, we talked about this So are they different, which one is better?
See, both are two different ways of doing the same thing Which one to use when, I will teach you all this in this chapter So first of all, learn the while loop
Assume I have a variable i, its value is 1 Okay, its value is 1, listen carefully I will write while, I will write condition in it
Assume I write i is less than 6, I will put colon and hit enter, see indent Means I have entered in the while loop And now if I write print i here, what will happen?
i's value is 1, condition will be checked, is 1 less than 6? Yes So while loop is executed only when condition is true What is the condition in this case? 1 is less than 6 is true
So it will come in the while loop Print i whose value is 1 And after this I will write i plus equals to 1
Means what should I do to i's value? I should make i's value 2 So i's value was 1 earlier, control came here
Control means our program Checked, 1 less than 6 was true Printed, 1 will be printed in the console
This will happen, i's value is now 2 While because there is a loop, so this control goes back and checks the condition Listen to this carefully
Once this is executed with 1 value, then here i's value will be 2 It will come back, will check the condition Then will run this code, then will come back, will check the condition
Then will run this, then will come back, will check the condition And then will run this, and this will continue till condition is true If condition is always true, then this will become an infinite while loop
Which we don't need in ideal scenarios But you guys see here, what will be the output of this program I will do one thing here, I will write output here
Let's make a multi-line comment, in which I am telling you what will be the output To explain, I will use triple single quote And let's toggle the screen cast mode here
So that when I do such operations, you don't ask how I am doing it See directly which key is pressing below, okay? Now you see here, when i's value was 1, then this condition was true
Print i, means 1 was printed, i's value is 2 now Is 2 less than 6? Yes, so 2 will also be printed So this will be printed, then i's value will be 3
Is 3 less than 6? Yes, print i will be 3 Then after this, i's value will be 4, then this condition will be checked Is 4 less than 6? Yes, so 4's value will be printed
Then after this, i's value will be 5 Is 5 less than 6? Yes, 5's value will be printed Now i's value will be 6, is 6 less than 6?
No, it is not, that's why this loop will not be executed on 6 less than 6 This loop will exit, means it will come out of the control loop And the execution of this loop will end, and our output will come 1, 2, 3, 4, 5
So the work I did with for loop, the same work is done with while loop So what will the while loop do, it will check the condition once, will execute all the masala inside Will check the condition again, if it is true, it will check again
Everything will be executed again, will check the condition again and so on Until the condition is false One important thing in while loop is that you have to make the condition false at some point
Otherwise your while loop will never exit, means whatever you are checking in the condition You have to alter that in the while loop, like in this case, we changed the i's value And eventually our i's value became less than 6, means greater than or equal to 6
Less than 6 became false, basically I will say that our condition became false eventually Eventually our while condition became false, because of which our while loop exited I hope that is clear now, I hope you understood
Now come back to the notes, I have written it very lovingly for you This block will keep on executing until the condition is true So I just want to tell you about the while loop
In while loop, the condition is checked first, if it evaluates to true, the body of the loop is executed Otherwise not, if the loop is entered, the process of condition check and execution What is happening here, condition is being checked, then it is being executed
Then condition is being checked, execution is happening, then condition is being checked Execution is happening, then condition is being checked, execution is happening and so on It keeps on continuing until the condition becomes false
Quick quiz for you, write a program to print 1 to 50 using a while loop Which I have already done So what we will do here, we will do 51
So if we do 51, then our values from 1 to 50 will be printed It is simple, because when i's value will be 51, then nothing will be printed i's value will be 50, then only it will be printed
So this loop will keep on running First i's value will be 1, then 2, 3, 4, all the way till when value will be 50, then also condition will be true And when 50 will be 1, then also the condition will be false
So I hope this thing is clear to you and you understood Now I want you to solve the quick quiz by yourself Here we have given an example that we have to print Harry 5 times
So what we did, we made i's value 0, while i's value less than 5, inside we wrote print Harry We wrote i is equal to i plus 1, which is same as writing i plus equals to 1 You can write this or i is equal to i plus 1, it is the same thing
I will also write here for you, so that you keep on learning You can also write i is equal to i plus 1 You can also write this
Now here is another quick quiz, write a program to print the content of a list using while loops How will you do content of a list using while loops? Come, I will show you
So what I will do here is, 03 underscore list using while dot py So I have a list in which there are some numbers There is 1, there is Harry, there is false, then there is this
Then there is Rohan, then there is Shubham, then there is Shubhi Now what I want is to print this from while loop So I will do i is equal to 0, while i is less than length of l
And plus 1 Why did I do plus 1, I will tell you now Now what I will do is, I will write print and I will write li
And I will do this and as soon as I run it, you will see And i is equal to i plus 1 is very important here If I did not write that, then it will go to infinite loop
So you see here, what happened, list index came out of range In fact, I should have done length of l Because it will start from 0 and go to length of l minus 1
So my bad, I should have done it like this And you see here, it has printed So it has printed, you can see here
Harry, false, this, Rohan, Shubham, Shubhi And it must have also printed 1, I will run it again and show you You can see, 1 came, then Harry came, false, this, Rohan, Shubham, Shubhi
It has printed all these values Why did it print all the values? Let's understand step by step
First of all, value of i was 0 So value of i was 0, i is less than length of l What is the length of this list? 1, 2, 3, 4, 5, 6, 7
This list length is 7, smaller than 7 is 0 So here it will print l0 Then value of i will become 1, then it will print l1
Then l2, then l3, then l4, then l5, then l6 Then when value of i will become 7, 7 less than 7 is false And our while loop will exit
Because of which this while loop will exit And we will finally get the content of this list I hope that was easy, I hope you understood how
We will print the content of a list using while loops Let's talk about for loops, it is the brother of while loop I say while loop's brother because
Whatever can be done with while loop can also be done with for loop So let's do 04 underscore underscore four underscore loops.py And it is very similar
We will write for i in range And whatever number we will write in range, suppose we wrote 4 So it will automatically make it 0, 4
So if we will do i in range 4, then it will go from 0 to 3 So I will show you by printing i And I will show you what happens, it printed 0, 1, 2, 3
So basically what does range function do? It generates numbers from 0 to n-1 on the fly So if you will do for i in range
So what does range 4 mean? In range 4, there will be 0, 1, 2 and 3 So what will happen?
It goes from 0 to n-1 So if I show you the range function So range function in python is used to generate a sequence of numbers
We can also specify the start, stop and step size Like we already saw Where did we see?
We saw in string slicing So you should explore this step size What is step size?
But I would like to show you quickly in rappel About step size So if I will write python here
And I will write for i in range And I will write 0 to 100 And I will write step of 4
And I will write shift enter And I will press tab And I will write print i
And I will press enter So see what it did, it started from 0 Then it made it 4, then it made it 8
So if I do step size 4 So what will happen? First of all, take numbers from 0 to 100
Then start from 0, 1, 2, 3, 4 Like I told you earlier Like we saw in string slicing
You have to jump So I will tell you the meaning of jumping So I will make a new file
Suppose I have 0, 1, 2, 3, 4 5, 6, 7, 8, 9 So first of all, 0 colon 100
All the way till 100 Write 0, 1, 2, 3, 4 Write 4, 1, 2, 3, 4
Write 8, exactly the same 0, 4, 8, 12 I have already told you in string slicing
That's why I am closing it But I hope you have seen the string slicing chapter In which I told you about step size
So you can give step size in range function Which is very good So for loop is used to iterate
Through a sequence like list, tuple or string You can put for loop in string You can put it in list
You can put it in tuple Now I will show you how easily You can iterate a list
For loop Iterate.py Iterate.py
So suppose I have a list Which has some numbers Like 1, 4, 6, 2, 3, 4
6 again And 7, 6, 4 If I write for i in l
And print i Then all the elements of this list Will be printed one by one
1, 4, 6, 2, 3, 4 6, 7, 6, 4 What is happening is that all the numbers
Are being printed one by one If I put i in l Now if I do the same thing
With tuple If I write t is equal to Suppose I make a tuple
If I start with 6, 2, 3, 1 75, 1, 2, 2 And if I do for i in t
And print i Then you will see that All the elements will be printed one by one
6, 2, 3, 1, 75, 1, 2, 2 And this output is of the top one So if I just comment out
The top one Then you can see 6, 2, 3, 1, 75, 1, 2, 2
And these four are In this tuple So in this tuple there are 6, 2, 3, 1, 75, 1, 2, 2
And if I do for i in t And print i Then all the elements will be printed one by one
I hope I was able to explain you properly That how you can iterate I have already written in the notes
That you can iterate tuple, list, even string So I will give you an example Of string
And I will write for i in s And print i And I will comment out this
And you can see H, A, R, R, Y Is written here
So characters will come one by one H, A, R, R And Y will come as values of i
So for i in s H, A, R, R And Y
And we are basically iterating The string So I will do one thing
I will give you some good headings So as you can see This is for loops with tuples
This is for loops with list And this is for loops with strings I hope you understood this
So this example is for list, tuple And string Now I have already told you
About range function Explore step size of range function I have already told you step size
Now I have given an example of range function Which I have already given you For i in range 0 to 7
So it will go from 0 to 6 From 0 to 6 Now here is a very important concept
Which is taught very less And very less people know And you can become different by knowing this
That is for loop with else With for loop An else statement can be used
An optional else can be used Which What it will do
I will show you So basically we have a list I said for item in l print item
So what will happen When it will exit for loop Then it will execute else
So with this example I explain you for loop with else So basically what happens
The loop will continue Till it will get values And once it stops getting values
Then what it will do Then it will execute else I will indent it properly
it is not indented properly so I will indent it properly and you see what happened here
my list was 178 for item in L and I did print item
then I did else, then I did print done this is printed when the loop exhausts okay
so see what will happen that I did for item in L and did print item so first one print will be done, then 7 print will be done, then 8 print will be done and finally when there is no value left to print it
then what will it do? it will print this print done
okay so this thing will be printed so if I run this program and show you
what will happen first? one print, 7 print, 8 print finally it was trying to print next value
so it couldn't because for loop was exhausted and that's why it
entered in else and printed done so when do we use for loop with else? for loop control will go inside else
when it will exhaust okay when it will exhaust
I hope you understood this thing so for loop with else is asked a lot in interview is it used in programs?
I will say that if I talk about practical programs then for loop with else is used very less but yes, interviewer can ask you to trap you
that how to use for loop with else? tell me this so that you get trapped and you get trapped
and after that he can make you turn so all these things interviewer can do with you okay
now we will talk about break, continue and pass statements okay
what is break? break is used to come out of the loop when encountered it exits abruptly
okay abruptly exits this program like suppose we have a for loop
which is printing from 0 to 80 okay I will quickly tell you break, continue and pass
I will do one thing break and continue.py suppose I have a function
for i in range 100 I have done print i here so you will say yes, it will print from 0 to 99
what to do then? I will tell you what is break if I say that for i in range 100
you keep printing values one by one but if value of i is 34 then you break from here
means print from 0 to 33 I got value of i as 34 similarly run this break
what is the meaning of break? exit the loop right now forget everything and exit the loop
what is the meaning of break? forget everything and exit the loop so if I run this
you will see here that it printed from 0 to 33 because break means forget everything
and exit the loop forget everything okay
so I hope you understood what is break we wrote i in range 100 i equals to equals to 34
break means exit the loop right now okay there is a continue also
continue is also very similar but understand the difference in both break means exit this loop
and forget this loop but continue means forget this iteration skip this iteration
it means skip this iteration what is the meaning of iteration? value of i is 0
that is an iteration value of i is 1 that is an iteration
value of i is 2, 3, 4, 5, etc up to 99 that is an iteration
okay so here from 0 to 99 every value is an iteration
so what will happen here if I am saying if value of i is 34
then continue so what will it do? it will say value of i is 0
this is false so it will print i value of i is 1
this is false it will print i value of i is 3, 4, 5, 6, 7, 8, 9
it will go all the way till 33 so what will it do?
it will print till 33 as soon as value of i is 34 it will see continue
so it will not execute the code below it will start running for the next value continue means
skip that particular iteration that means in this loop
this iteration the part that was executed till now is done
but it should not be below now the value what should you do?
for the next value you go what should you do?
for the next value you go okay
so what will happen here? continue means for the next value
you go so what will it do? value of i was 34
this will not run because we have continued in 34 it will run for 35
okay so I will show you it will skip 34
so if you see it closely 31, 32, 33 skipped 34
then 35 so as soon as continue is seen it skips that particular iteration
but break is seen it exits the loop break means
from now on no value will run this means the story of the loop is over
the break statement kills the loop but what does continue do? continue says
for this value for the value below whatever is left
that don't execute execute the next iteration
like i was 34 whatever is below that don't execute continue
work for 35 okay then i was 35
this will be false then 36 this will be false
so if continue is seen then that iteration skips if break is seen
then the loop skips okay we will ask questions
about break and continue you will understand so
if I come back to the notes then see continue is used to stop
the current iteration of the loop and continue with the next one it instructs the program
to skip this iteration continue means skip this iteration
and what does break mean? exit the loop now it instructs the program
to exit the loop now and what is the instruction here? skip this iteration
okay I told you from 0 to 80
if we break at the value of 3 then what will happen?
0, 1, 2, 3 will be printed but as soon as this break is seen it will exit from this loop
what will happen here? if the value of i is 2 then the iteration will skip
2 3 and 4 will run here
okay so I request you that you guys
run it in your computers don't look at it like a movie
I mean seriously don't look at it like a movie okay
let's talk about pass sometimes what we do that sometimes we simply
don't want to do anything in the for loop so let's say
I want that this is my for loop let me show you
0, 8, underscore pass.py let's say I say
for i in range 645 colon
now I want to do this later let me write another while loop first
while i is less than 45 let's say I want to do this later too or I do it right now
print i and then after this i
plus equals to 1 let me do it here plus equals to 1
let me do it here i is equal to 0 let me do it here
now I will think that this is not working and this will work
but as soon as I run I am getting an error indentation error
he is saying how is your indentation? what are you doing?
here you see that you left half incomplete complete the for loop
but I say that I will do it later I want to leave it now
and do this I want to see its output I want to leave this
so for this python has given a convenience keyword
whose name is pass python says write pass
it means that you will work here later run
see it worked from 0 to 44 the output of our while loop
has come okay so we write pass
so if I tell you what is pass? pass is a null statement in python
it instructs to do nothing if you don't write pass then there will be an error
but if you write pass then it means that in for loop
don't do anything here so this whole statement will be skipped
okay without pass the program will throw an error
okay now we will see some questions because I have understood
the loops very well and what a great codes are being made here
I am really happy with myself such a great explanation is coming out of things
I enjoyed it I am enjoying it and I am very excited
that how much you will enjoy when this video will be published and tell me in the comment section
by writing I like it from the heart when you give feedback
I will meet you in the practice set of chapter 7 practice set of chapter 7
so guys now we will move towards the practice set of chapter 7
I have made a folder in the name of chapter 7 ps and I will right click here
first I will close this folder then right click and open this folder in vs code
the first question is of practice set 7 write a program to print
multiplication table of a given number using for loop it is a very simple problem
first of all I will make a file and after this I will write
let n is equal to input and I will write
enter a number colon I will space it
and then what I will do I will write for i in range I will go from 1 to 11
then I will get from 1 to 10 because it goes from this number to n-1
so I will write print and I will use an f string I have already told you
what is f string what is f string here basically you put an f here
and write a normal string and now you can use variables in this string
so you can write here n and after this we will write multiply by
and we will write here i is equal to and we will write here
n multiply by i ok
so whatever will be its value it will come so let's see it
so this number is asking me suppose I want to write 7 so see
it gave me a table of 7 but again I made the same mistake
about which we were talking in this whole course that
whenever we are talking about integer then you have to do int input it is your duty
you have to do it if I will write 7 here then the right table of 7
will be print ok so you have seen here
that 7 into 535 it will be correct it is python
it is not you and me who will make a mistake ok