Python Tutorial for Beginners | Python Full Course From Scratch | Python Programming | Intellipaat

Good morning, good afternoon and good evening. Ladies and gentlemen I am Atul from Intellipaat and I welcome all the Python enthusiasts from different part of the world to this session on python tutorial Let me start this session with some thoughts and research based on python According to stock overflows 2018 developer survey it was found that python is a most-wanted programming language of 2018 even if you talk about the career in Python, Python Developers are in a great demand for a wide range of jobs all over the world these include machine learning Databases data analysis cloud infrastructures website designing web scraping security mobile developments and many more Let me just give you an example if you search for a Python developer job on any major job website like Or you will find thousands of job opportunities out there Based on Indeeds March 2018 data the average salary of a Python developer in US was around $115835 such high average salary in the case that many companies are competing to hire knowledgeable Python developers and It also supports the fact that python is an in-demand skill. So let’s move ahead and without delaying any further Let me discuss the agenda for today’s session Since no prerequisites are required for this session. So I’ll talk about Python from scratch Well just FYI. No prerequisites are required for this session as I’m going to teach you guys Python from scratch I’ll try to cover almost all the fundamentals of Python including some tips and tricks Well in order to make this session more interactive, I’ve added some quizzes in between which will help you to test your skill so we’ll start this session with Introduction to Python 3.0 followed by Python tokens where I teach you about keywords identifies and different types of literals and operators in Python Next you will learn about various data types and how you can use them in Python Followed by it will learn about flow control where you learn about the flow of execution of program using for while and other flow control statements once you get comfortable till this part We’ll move ahead and learn about how to define and call a function in Python. Seeing the market demand additionally I have included topics like lambda function, arrays, classes and objects as a part of the tutorial and Finally we’ll end this session with file handling in Python. I hope the agenda is clear to you guys. Shall we move ahead? Ok, so let’s start our session with introduction to Python. So what is Python? Well python is a high-level Object-oriented programming language for general-purpose programming it is one of the most widely used High-level programming language and is well known for executing basic tasks with less which makes it more compact and an economical language to learn if you see Python coders often three to five times shorter than the Java codes and Around five to ten times shorter than c++ codes Well if you talk about its history It was developed by Guido van Rossum in the year 1991. And since then it’s open-source community is still growing So, how do you think he came up with this name? Wasn’t the cause of a snake well no. Rossum named Python over the name of a sitcom TV series Monty Python’s Flying Circus It seems he was highly impressed with the show, right? So these are some other high-level introduction to Python Now a question might arise in your mind that what made python so popular or why you should learn it So let’s discuss it one by one. So the first point we have is simple and easy to learn Well, if you compare Python with any other language, for example in Java or C++ You will find that at syntax is very lot easier than them you don’t have to worry about the missing semi commas in the air and Its syntax is more readable and you are safe from writing multiple lines of code just for printing a statement like in case of Java If you talk about Python All you need to write is just one line of statement for example print and within the double quotes mentioned, whatever you want to print So this is how it is simple and easy to learn The next point comes the career opportunity Well, like I told you in the beginning of this video itself that Python has a huge career opportunity in the IT industry Almost every other IT company be it a startup or a multi-billion a company are using Python and some or the other ways So if you’re a Python developer You will be on a huge demand for a wide range of jobs in the domain of machine learning databases data analysis, cloud infrastructure, website designing website reliability, testing, scraping, security, mobile developments and many more if You don’t trust my words just go and look out for Python developer job on any major job website You will find thousands of job opportunities out there with an average salary of around one hundred and sixteen thousand dollars This indicates that there are many companies out there who are competing to hire a knowledgeable Python developer for them So yeah, you have a huge career opportunities in Python all depends on your skill All right. Next is large open source community Well, if you become a Python developer and when you are stuck or you want to learn something new then you don’t have to worry about it You will find a large number of resources, like reference, manuals, books, tutorials,videos, forums Etc out there on the internet If you have any doubt or some technical issues You can directly seek help from thousands of Python community members on forum, Twitter or Facebook and pretty much everywhere Now since Python is an open source community You will always find people who are trying to improve it keeping it fresh and up to date with current Trends and bringing out newer version of the language, right? So yeah, it has a damn large open source community Next we have is reliable and efficient Ask any Python developer or anyone who has ever use the language and they will agree that it’s speedy reliable and more efficient You can work with and deploy Python applications in nearly any environment and there is little to no performance loss No matter what platform you work with again because it’s versatile This also means that you can work across several domains including web development, desktop application, Mobile applications and many more. You are not bound to a single platform or domain as it offers the same experience Almost everywhere. All right. So next we have is The extensive library. Well Python has a huge set of library lists Which would be useful for any programmer interested in Python depending on the area of interest for? Example you have numpy for scientific computing, plotlib for data visualization, nltk for natural language processing Django for web framework Pygim for game development and there are many more It can be used in a lot of places and has a wide variety of application like for web development you can use Python web framework like django flask and tornado. If You are into developing desktop application you can go with pyGTK and cocoa or if you are planning to use Python in the field of machine learning or deep learning or data science will find many Python libraries for it Like SK learn tensorflow, theano and many more If you talk about cloud and DevOps, you can use OpenStack Python software development kit Which is generally used to write Python automation scripts to create and manage resources in your OpenStack cloud You can even use a Python library named fabric for streamlining the use of SSH for application development or system Administration tasks. All right Last word we have is Hardware programming Next if you are planning to switch over to hardware programming you have raspberry pi to connect your project to the real world with Python Well now that you know, how important is fight into the world. Let’s see the popularity of Python and Industry Well, big tech giants are using Python. In fact python is one of the Google’s favorite languages They are always hiring expert in it and they have even created many of their popular products with it But I don’t think I should again mention that since many big companies like Yahoo! Facebook Netflix Dropbox YouTube BitTorrent NASA and many other rely on Python developers So these companies are always in search of talented professionals who have already worked on and has some experience in Python There is and there always be a demand for Python developers and Python web developers and they are even offered some decent packages Well, this was all about the background check of Python. Now, let’s move ahead and dive deep into Python programming So the very first thing that would be doing is installing Python on our system So I already installed Python in my system for those of you who don’t have it You can download it and get it installed on its official website triple w dot Python dot or slash download So, let me just show you how easy is it to install Python on your system, let me open my default browser and I’ll write here triple w dot Python dot or slash download So as you can see here We have a download of of download Python 3 point 7.1 if you want a different version For Windows Linux or Mac. You can just click over to these links For example if I have Windows user and I want a different version of it then let’s see I click the link and you can download different versions of Python from here So the first two option that we got up here is latest Python 3 release Python 3 point 7.1 and latest Python to release Python 2.7 dot 1:5 now These are the two popular version of Python. Now, which version should you download? Well, I have installed Python 3 in my machine in the past There was a bit of debate in the coding community about which Python version was best going to learn Python 2 or Python 3 Specifically Python 2.7 or Python 3.5 or 3 point 7 alike? Let me just tell you Pisan to husband most popular version for a decade and a half and is still in demand at certain companies however, more companies are moving from Python 2 to Python 3 for example in 2017 Instagram migrated the majority of the Python code base from Python 2.7 To Python 3 as at each newer version of Python. It is getting a faster runtime on the other hand Nobody is currently working on to make Python 2.7 work faster Also the community support for python 3 is more better so for someone who wants to learn Python programming for beginner I’d suggest that you should avoid spending time on a version that is becoming obsolete Well, let’s download Python 3 for Windows So let’s directly jump up to our first window and click on download Python 3 point 7.1 Download it So, yeah, it’s downloaded Just click over the dot exe file and follow the installation step and it will get installed click Click on the Install Now Version select the drive where you want to install it and click on the next and it will be installed on a machine All right. So let’s click on the install option Yes And this setup will install Python on my system Well if you are starting Python You will find that you will have various option where you can write down your codes and get it executed by default We use the Python CLI you can use any text editor like notepad or notepad plus plus or you can even use any Python IDE like Anaconda, which has chip at a notebook and spider pre-install inner Python is majorly based on indentation. So these IDs will help you a lot while coding and debugging your program So by then my setup was successful, let’s close it for this tutorial I’ll be using Japan a notebook which is a web-based application That will allow me to write my Python codes on it It’s ready to use and it gives me an interactive data science environment All right, if you’re installing Jupiter notebook and Python I would strongly recommend you to use the anaconda distribution which already includes the Python the Jupiter notebook and other commonly used Packages for starting with Python Holley so for downloading it Let’s visit the website triple w dot anaconda. Kong slash download. Let’s open our default browser triple w dot Anaconda Dot-com slash downloads So as you can see we have a download option up here Just click over it onion stelae Chanel start Open the downloaded file and follow this series of steps and install anaconda on your machine Alright, so for me, I have already installed an a corner on my system. So let me just go over there and open it Anaconda navigator This might take some time to start So there’s how the Anaconda Navigator looks like after the installation From here. I’ll select my Jupiter notebook and hit the launch button My jupiter notebook will open in my default browser So here’s my Jupiter notebook it opened on the localhost with port number triple eight nine So this is how the Jupiter notebook will look like so from here Go on to the new select Python three from the notebook And this will redirect you to the Python notebook page where you can write all the different sorts of Python codes on it Ranging from a beginner level to advanced one Now that we have set up our environment for coding Let me just give you a glimpse of how easy is Python. You want to perform addition? It’s as simple as one plus two equal three That’s it. All you need to do is 1 plus 2 and hit enter you will get the output So let’s move it and let me teach you more about it. So let’s get back to your tutorial and see what’s our next topic So our first topic is Python variable, how do you think the value is assigned? Well for assigning the values There are two different methods first as assigning single value to a variable and the next is assign multiple values to a variable So let’s start with assigning single value to a variable You know what unlike Java or C++ You don’t have to add any data type for a variable just specify the name of the variable For example a equal 10. So depending on your data Python will automatically detect the datatype of your variable Which in this case is integer alright Let’s see other example suppose we have a name as victim So Python will automatically detect that this name variable is of data type string since it stored the string value All right next we have salary as mm point 2 3 so since 2002 3 is nothing but a floating type so automatically Python will detect that Salvi use of a datatype float You don’t have to specify a data type to a variable. Alright, let me just show you this example in algebra notebook For example, I need to store our integer value So I’ll name a variable like a equal 10 10 is integer, and I’m not specifying any data type for it right hit enter Since no error is appearing. So I guess It’s correct. But what if I just write int a equal 10? So as you can see it gave me an invalid syntax so you cannot use any data type before a variable Alright next what if I want to store a string variable? Just like name equal John Holly hey Tonto Similarly for a floating data type salary or any variable name equal 2020 3.2 3 4 honey It’s as simple as that all right, so Now that you have learned how you can assign a value to a variable Let’s see how you can have multiple assignment to a same variable all right, so Let’s suppose you are assigning three variables as equal to ten Be equal ten and again C equals ten, so Here, we have three variables and what you have to do you have to assign the same value to all of them. So Instead of assigning value one by one what you can just write you can just write a equal B equal C equal ten All right, and it will be executed without any error fine now this was simple now what if you have to assign different value to each variable, for example I have a variable as X equal to 20 Why equal 30 and Z equal 40. So in this case what you can do for multiple assignment, you can just write X comma Y comma Z equal 20 comma 30 Comma 40 so as simple as that now Let me just print any of the two variables and check if we are correct or not. So let’s print Why and let’s print a So, yeah, so we got the output as 30 and 10 y equals 30 and Equal 10, even if you want you can just check it by updating the value like equal B equal Z equal 20 and this is make it something else like 14 and 50 and now print the value of yn a So you’ll get the value as 40 and 20. All right So this was about the assigning single value to a variable we also learned about assigning multiple values to a variable Equal B equals equal 10 this example we already did right and we got the output as 2010 fine So now that you know how you can assign value to a variable So let’s move ahead and start our discussion with some basics of Python. So I’ll start with Python token So what exactly is this Python token? Well python token is the smallest meaningful component or it is a very basic component of the source code The characters are categorized as one of the four class of tokens that describe their functions. It can be a key word It can be a literal. It can be identified or it can be an operator Fine. So let’s discuss them one by one starting with keyword So, what are these keywords Well, these keywords are some sort of reserved words which are designed for a specific purpose you cannot use the keyword as a variable name or an identifier name some of the keywords and Python include for if del break continued True/false etc. You cannot use them as a your variable These words in Python have a specific purpose and have a specific meaning to it alright Let’s move ahead Next is the identifier Well, it is the name which you give to your variable or do function or to your class For example, if you are defining a variable as a equal 10 Then the word a or the variable a is defining the identifier. All right Well before you start naming your variables or methods or class You must know that there are certain set of rules or guidelines for naming an identifier. Let’s see them one by one So the first one is no character except the under scope can be used as an identifier. So what does that mean? Let’s see So you cannot have a variable name as a tea at the rate One two three equal something any value. You cannot have a variable name like this. It will throw you an error. All right Or you cannot even have a variable like a t dot w e equal 20 even this will throw you an error All right, but instead of it if you write eighty underscore w/e this would be perfectly fine All right, next is keyword should not be used as an identifying the site oh you earlier also, right It’s like if you are mentioning some keywords, like if equal 10, obviously, this will throw you an error Or you’re writing V equal ten Then this will give you an error. You cannot use any keyword as a name of a identifier That is a name of a variable a class object or a function. Alright? Next is Python is case-sensitive that is Var with capital V and var with small V are two different identifiers. Let me show you this Suppose I mentioned equal 20 and capital e equal Ten So, what do you think the value should be if I print a Would it be updated to 10 or? Would it be same as 20? Well, let’s see So the output is 20, so it means that Small e or the a and smaller case and a in uppercase both are treated differently in Python print capital e alight So let’s move ahead next and the last one is first character of an identifier can be a character underscore but not a digit Well, what does this mean? 1 to 80 equal 10, this is wrong One two or or 41 at the rate RAC equal something This is wrong But what you can do instead you just specify underscore before this underscore 41 RAC without the at the rate part as No special character other than underscore is allowed Equal ten. So this thing would be accepted without any error fine. So this was all about the identifiers next comes the laterals Well literals are the constant used in Python or you can say that literals are the data given in a variable or a constant Basically, there are four different types of literals String literals numeric literals boolean literal and special literal. Let’s have a look at them one by one starting with string literals Well, you can create a string literal just by enclosing a text within the code both single and double quotes are allowed in a string literal for example You can have a variable as name one equal Double quotes John and you can even have a variable as name to equal within single quote John or maybe James Both are correct You’re not getting any error, right So since you are not getting any error in the output So it verifies that you can use both the single and double quotes and string it Okay, now what if you have multiple line input so how you print that Let’s see for a multiple line input multi Line equal. So all you need to do is mention three single quotes just three Holly and after that start writing like string one Enter different line string to line three strength three And so on Alais, and again, you have to end it using three codes Fine executor he won’t get any error and if you print the multi-line You’ll get the output as str1 str2 str1 Tulane’s moly Let’s get back to our tutorial So what was our input name? Unequal John name two equal James friend name one name too and We got the output. Yeah, we discuss this. We have a multi-line thing within triple quotes Okay, you had a multi-line thing in which he needed the output and three different lines. So yeah, you got it using three codes Elect fine. So this was about these string literal later in this session We’ll see what are the various of the function you can perform on the string literal Using some of the pythons existing library for now. Let’s move ahead and let me give you an overview of numerical entry So what are these numeric literals a? Numeric literal is a character string whose characters are selected from the digits 0 to 9. It consists of 4 sign character it can be either positive or negative or It can even contain a decimal point well, if the little contains no decimal point then it means that it’s an integer if your number is too long and we add Capital L or small L at the end of the number it makes it a long number or If the little contains a decimal point it becomes a float but yes remember that you can only use one decimal point in a number Have you ever seen a number with more than one decimal point never write? Well, just for the fYI I’m not talking about the IP All right, by the way in Python, the value of integer is not restricted by the number of bits It can expand to the limit of the available memory and since the value of the integer depends on the available memory So no special arrangement was made in Python for storing large number. So any doubt up till this portion Well, if you have please add it in the comment section below. All right, so moving on ahead Next we have is the boolean literally. Well these boolean literal have just two values either true or false Remember that both these two and faults are also keyword so you cannot use them as an identifier They are generally used when we are dealing with some condition whose outcome is either true or false. All right, so this was about the boolean lateral Next we have is the special literally Well python has just one special little as none which is used to specify the field that is not created Well, if you are from a programming background or you just have a basic knowledge about C C++ or Java I think you must have heard about the keyword null, right? Well, if yes, then this null is nothing but it equivalent of null keyword and C C++ or Java Alright, so I hope that you guys have understood about literal. Well, there are many cases where you can use none. Let’s see an example It’s a self explainatory example just go through it and we’ll proceed All right, nothing needs to be explained in this so let’s move ahead okay. I hope that you have understood about littles So let’s move ahead to operators So what are these operators? Well, these are some specific character which have a specific task to perform and based on the function and the task the operators are further segregated into seven different types of operators or thematic operator assignment operator comparison operator logical operator bitwise identity and membership operator Fine, so let me discuss them one by one. So starting with arithmetic operator Well, arithmetic operators are the one which is used to perform some arithmetic calculation So what it does it takes two operand to perform operation on them. For example, 2 plus 3 So here 2 & 3 are operand and the character plus over here is nothing but arithmetic operator alright, so there other arithmetic operators – Like plus minus multiplication division mod. All these characters are a part of arithmetic operator Let me just show you one more example Like 1 plus 2 so you have 1 & 2 are the operand and + here is a automatic operator 1 – 2 again – is a arithmetic operator 2 mod 1 mod is what a death Matic operator Alright, let’s move on Next we have is the assignment operator well This assignment operator is used to assign a value to a variable the character which comes under this operator are equal to plus equal to minus equal to multiplication equal to an acceptor for Example you declare a variable as bar equal to 10, so This equal to is your assignment operator and is used to assign value to a variable Let’s get back to our Jupiter notebook and learn more about it. So For example, I am defining a variable var equal 10, so this equal to is nothing but a assignment operator Ver plus equal 10 again an assignment operator, but what does this mean? So What does this mean? Well, it means that very equal that plus 10 All right So if you print the value of that what do you think the output should be Well, you’ll get the rs.20 since the value of R is already 10. So 10 plus 10. It’s 20 So the updated value of Rs 20 Similarly you can perform – equal to ten and again get your value printed so again, the updated value of our would be 20 minus 10 10. So this plus equal to or minus equal to it is generally named as a shorthand Which means add or subtract and assign value to self? All right Same example is mentioned over you like equal 10a Multiplied equal to ten. So it means that equal ten into ten when you print a you will get the output as 100 All right. So next we have is comparison operator Well, it is used to compare two values and it returns true or false as the output. All right The operators which are a part of it are less than greater than less than equal to greater than equal to or not equal to For example, let’s see what example we have up here Equal ten B equal 20 a is greater than B Obviously ten is not greater than twenty. Right so it has given me false as the output All right fine. So let’s move ahead Next is the logical operator Well, these logical operator are used to perform some logical calculation and the operators are the keywords which are a part of it are and or and not For example we have a equal ten is less than ten and two is greater than minus one Then print a so you are getting output as false Okay, let’s jump back to our Jupiter notebook and see why we are getting the output as false So you had a equal ten is less than ten and Two is greater than minus one. So if you see we have two comparison operator up here So what is the value of ten less than ten, since it is a comparison operator? So it will give me a false right ten is not less than ten and Two is greater than minus one. This is true. So you can summarize this as False and true. Alright, so since it’s false and true therefore at equals two Faults Well, how well do you remember the binary calculation that you have learned in your college? It’s like 1.1 equal 1 or 1.0 equals 0 or 1 + 1 equal 1 or 1 + 0 is 0 so Consider true as 1 and false as 0 so false and true is false Alright that’s why you got the result as false now if you print the value of a You’ll get the output as false, all right. So next we have is the bitwise operator So this operator is used to perform the bitwise calculation this operator includes And or left shift right shift not Alright, so, let’s see. They use one by one. Let’s see the example what we have up here So we have seven or five we output a seven seven and five the output is 5 so How do you think we are getting this output? Let’s see the calculation. So how it is calculated? So if you convert seven into its binary form its triple 1 and if you convert five into its binary form its 1 0 1 Now if you are performing or so, basically you are performing a binary addition on it And if you are performing an and you are performing a binary multiplication on it, alright, so let’s see So if you are performing a binary addition on it so 1 plus 1 is 1 1 plus 0 is again 1 and Again, 1 plus minus 1. So that’s why you are getting the or of 7 & 5 as 7 Even you can verify this using the calculator When you calculator Yeah, it’s a programmer calculator so seven or five so you’ll get the output as Seven let’s correct fine next operator that we have got up here is Tilde operator or in not operator. Well, this operator is used to perform in not operation. So what does not of seven? What is not of seven its minus eight right how it is – it let me just show you using a notepad That’s open notepad Sorry, so seven in its binary form with four bits or what it as zero one one one I like if you perform a lot of seven that is not of Seven what you will get all the bets would be switched like zero would be converted to 1 again 1 to 0 again 1 to 0 so you will get 1000 and Automatically and negative sign would be added at the beginning So you will get minus 1000 and binary form which convert into decimal you will get it as minus of 8 All right. That’s why the output of not of 7 is minus 8 fine Next is the left shift and right shift operator Let’s see. What is the output first? for example, I am trying for a Right shift then 10 right shift 2. What is the output? It’s two Let’s see how we got the output as to open our notepad Let’s move this So 10 in its binary form what it is. It’s one zero one zero. All right Well when you are performing the right shift You are telling the interpreter that you want to shift your bets toward right and how many bits you want have shift is Basically two since you have mentioned ten right chef – all right So one zero one zero shifted two bits to the right zero zero and remove one Zero from the end. Alright? So that is how you are shifting two bits towards the right. So you got the result as zero zero one zero Now if you convert this into decimal form you will get the output as to fine So it’s like you are trying to push two bets from the left towards the right as a team I like fun. Let’s move Next we have is 10 left shift 2. What is the output? It’s 40 Let’s see how we got that output. Again Tannen. It’s binary form is 1 0 1 0 So when you are performing a left shift, it’s like pushing the beds from right towards the left So ten left shift two is like one zero one zero and you are adding two bits in the end Since you are shifting two bits to the left So you are getting one zero one triple zero. So let’s convert this into its decimal form So one zero one triple zero in its decimal form is what 40? So that is why you are getting the result as 40 So there is a shortcut trick Well the shortcut trick is that just add zeros equal to the number of bits that you want to shift towards the left all right, for example, I want to perform ten left shift three so my output should be One zero one triple Zero and one more zero. Alright since I’m moving three zeros All right, since I’m moving three zeroes towards the left So let’s convert it So it’s a bee So you are getting the output as a tea. Let’s verify it again Well if you perform ten Left shift three you’ll get the output as 80 fine Well, I hope the bitwise operators clear to you guys Well in case you have slightest doubt, please add your doubt to the comment section below and we’ll try to reply them at the earliest For now, we can just move ahead to be Identity operator Well, these operators are used to test if two operands shares an identity The operators which are part of it are is an S. Not. These are basically two keywords. Alright For example What we have up here X equal ten existent It’s true or X equal ten X is not an it’s false. All right Has just used to test if the operand shares and identity or not All right. So next and the final operator in Python is the membership operator Well, this operator is used to test whether a value is a member of a sequence or not The sequence may be a list. It can be a string or it can be a pupil I know some of you guys might be thinking what is this list or what is this? Tuple? Guys, I’d say that just be patient. I teach you about them in detail during this session for now You can just understand that. This list is almost same as array, which you had already studied in C C++ or Java It’s almost same as that All right, and this membership operator it is used when you want to check if a certain element is present in a list or not So, let’s see. What are the operators which are a part of it So well, we have N and Norton keywords, which are a part of this membership operator All right. Let’s see an example So, what’s the example up here? So we have defined a list or you can see we have defined an array which consists of dog cat and wolf So we are checking for line and pets Since pet is the name of our list or an array you can see So we are searching for line and pet so since there is no line in it. So the result is false Next what you are doing we have seen for wolf in pets. So yeah wolf is present in pet So we are getting output as true All right So this was all about the Python token So moving on ahead we’ll learn about datatype and Python well python has majorly two data types immutable data type and mutable data type Immutable are the one which cannot be changed or modified Immutables are the one which can be changed only immutable datatype consists of numbers strings and tuples on the other hand mutable data type they consist of lists dictionaries and sets All right So let’s discuss about them one by one starting with Numbers so under the topic of numeric little I have already discussed that Python asks for different types of numeric literals integer long integer of floating number and complex number Well, I have also told you that you don’t need to specify the data type of a variable while you are declaring it remember Python would automatically convert a number from one type to another if it means fine Don’t worry If you want to explicitly convert One data type to another you can do it using n function long function float function or complex function. Alright You have that option available with you For now, let’s just move on and see how we can find the data type of a variable Let’s see Suppose, I’m declaring an integer variable equal to 10. Let’s define a name And gel Now what if I want to check the data type of these variables? So, how can I do that? So for that we have PI function so print the type of variable a Alright or Print the Type of variable name or printer type of variable salary fine executed say there’s a Typo its type. All right So you can see that variable is of type integer variable name is of type string and variable salaries of type float Fine. All right. Let’s get back to our tutorial So this was about the numbers next as strengths Well anything which is written under the single or double quotes is treated as a string Alright let’s perform some operations on string and understand them in detail Suppose I have first string as Hello And my second-stringers in Delhi part All right now what if I want to print first character from the string one and the last character from string – So how will I do that? Let’s see. So what you can do print STR one and I need the first character So my first character is at index number zero. I’ll just mention STR 1 of 0 And I want the last character from string to write so print str2 So if you want the last character just specify minus one Executed So he got the output as capital H and small T Since capital H is the first character from string one and small T is the last Character from string – alright, so now what if I need to extract hello from Hello oil. So how will I do that? What I need to do Extract hello from Hello So all you need to do is print From with string string one Hello starting from zero and it should end till zero one two, three four It should end till five. All right, and Execute it. So you’ll get the output as hello Fine You can collect the substring from a string One thing to remember is that if you check the index number of hello So it would end at 0 1 2 3 4 4 right So when you want a substring so you have to mention it as plus 1 so 4 plus 1 is 5 So 0 10 5 it won’t include the 5th position Generally, if you are extracting a substring the last number which are including that thing is not included it means that everything which would be between 0 and less than 5 Would come as the substring All right, let’s perform some operations on it. So first we have is the find function So what are these fine function? Well, this fine function returns the position of the string. Let’s take an example For example I have a string as STR equal Attachment And what I need to do, I want to find the position of the substring me from it. So STR dot find and inside that Just mentioned what you want to find like I want to find the position of me That’s executed. So you got the output as 6y since Emmy starts from six Zero one two, three, four five and it’s on sixth version. That’s why you got the potion as six Next is replace Well, this replace function is used to replace one character or string with other fine it’s like What if I want to replace this Emmy with em so st r dot replace? What I want to replace first mention that I want to replace em ii with what I want to replace its m fine executed so you got the output as attachment 8 TT ACH MNT Fine Emmy is replaced with M one more thing that you can do up here is Let me just show you You can convert the word attachment into attach Let’s see. How attach dot replace So what you need to replace from here is em ENT You don’t want em e and d from you know, so what should you replace it with? You can replace it with space. That’s a few excuted will get the word attached What if I don’t mention the space of you sure you get the output as attached even that work Holly Next we have is the split function. Well, this function is used to create split on the base of a character. For example, I have a split string which consists of What 1 comma 4 2 comma 3 So all these three different words are separated by comma so what I want to do I want to split the entire string on the base of comma so I’ll write Split STR dot split on the base of , And executed so as you can see I got the output as 3 different words What one word two and word three? Let’s move ahead Next is the count function well count function returns the count of the character in the string. For example Let’s take a string us Str three equals in Delhi pattern Now I want to find the count of letter I from this particular strengths STR 3 dot count I’ll mention I Let’s execute it. So what do you think the output should be I N te Double L. I P Double A T. So you think it should be too? Well according to me it will be one. I’ll tell you I see the output is 1 because Well, remember I told you Python is case-sensitive. So both the upper case and lower cases are treated differently so if I am searching for small, I that small I is just appearing once so I got the count of small, I as 1 Now what if I convert this string to uppercase? . You dot upper let’s convert it to uppercase Okay, that’s it Str 3-dot upper so it gave me in telepath let’s store it in some variable Like str. 4 equal STR 3 dot above fine Now if you count the number of eyes and STR four will get the output as do By since in capital Intel apart, you have two eyes I’ll a Fine So we discussed about the upper which converts the character string to the uppercase fine Next is the max or main function. Well, these functions are used to return the Max or men ask a values For example, let’s take a string file as anything For example exclamation mark at the rate hash One two three capital e small B small C capital C. All right So this is my string five Now I want to find the maximum asking value from it. So what I’ll do, I’ll just write max STR five executed I’ve must still mention this What a ton of the quotes I executed so the character with maximum ASCII value is small see early Or What if I want to find the minimum ask a value from string file, let’s check for that so minimum asked a value and string five is of exclamation mark Fine so let’s move ahead. So well next we have is the tuple People are a group of values within the parentheses and since they are immutable data type so the values within the tuples cannot be changed for example We have defined a tuple up here as my group equal a b c and d Alright, so this is the example of a tuple. So let’s move ahead and perform some operations on it to understand how to enter tail Let’s get back to our Jupiter notebook. Let’s define a tuple as My tuple equal within the parentheses I’ve mentioned a And B then see And d All right, so this is my tuple so the very first function that we are going to perform on it is Concatenation. So what are these concatenation? It is used to add two strings or character a lie Basically concatenation is used to add two or more tuples All right for example, I will define another tuple as my pupil one equal II If so if I need to concatenate it I’ll just write My tuple plus equal my tuple one And let’s Printed Bring my cube See the output you gotta error as invalid syntax What is the invalid syntax of here Something is invalid. Yeah, let’s define it begin You Okay, the semicolon is missing up here The cuter say, yeah So this concatenated me and gave me the result as a b c d e NF So this is how concatenation is done in? cuba Carbonation You Next we have is the repetition Repetition is used to duplicate the string or character for a given number of times For example, what if I want to double the contents in my tuple, so I’ll just write my tuple x 2 and again That’s it my tuple multiplied by 2 and hit enter so you will get the output as double values of a b c d e and f fine So this is how it repeats down in a tepee So let’s move ahead Next function that we need to perform is indexing Indexing shows the index position of a character or a string within the group or within the tuple For example, I want to find What characters present at the index number one That’s right my tubalove one So at first index B is present over there It’s like a is present as 0th index B at one C A to D at 3 and so on Fine. So this is how indexing is done in tuple Next is slicing slicing shows a specific set of index character or string fine for example, I want a specific set of strings which lies between the index number one to four So my tuple I want a specific set of string which lies between one to four So this is my zero eight one, two, three and four fine So I’ll get the output as B C D and E. Let’s check it So I got the output as B C D and E fine All right, so let’s move ahead So next we have is the list List is a sequence of mutable Python data type which is defined within the square brackets and Now since they are mutable they can be easily changed Let’s move ahead and perform some operations on it and understand it entity So what is an example? Okay, so we define a list as a ten seven point one two and data So it is consisting of mixed data type values it even has an integer a float and a string fine Get back to a notebook and define a list So there’s my list My list Equal within parentheses a string a number Decimal You And a string with multiple character Fine So this is my list now. Let’s see. What are the operation that we need to perform on it first is concatenation Well again, the concatenation is same as the one which we learnt in tuple It is used to add two or more less together Let’s define another list as minus two – – consists of element as stake Python Or it stuff I stuffin integer now what if I want to concatenate my list with -2, so what I’ll do My list plus equal my list – Let’s see. What is the result? Let’s spend the value of my list So you got the result as a 1-2-3 3.14 John Python 25 and 20 So you got this result two times cause see I executed it twice Time for tea So what you can do you can just You my list dot remove Hold on I don’t want to execute this entire Lester statement. So I define it under different block So my list dot To move Remove bitin You Okay, I’m a spare by tonight, okay remove this I can remove this Are Remove 20 the kingdom of 20 if you’re in remove it you’ll get another fine So See what we have currently in my list let’s print it okay Let’s get back up here And executed just once okay So as you can see my list got updated with Python and 20 Fine you Next is reputation. Well reputation is again same as tuple. It is used to duplicate your list with n number of times Well next is reputation. It is used to multiply your list by n number of time For example my list into two would duplicate your list with each element appearing twice in the list alright fine next is Slicing again same as tuple if you want a specific portion of a list We can just mention the starting index number and the ending index number plus one and it would return as the sliced list Early, for example, as you can see here on the screen, we have my list and inside that we have a1 3.14 and python and we want a sub list from one to fourth index number like 1 2 3 0 1 2 3 Plus 1/4 won’t be included. So I get the output as 1 3.4 and 3.14 and Python, alright Next is append. Well appending is adding any value to a list It’s almost same as the function what we did up here Right, it’s like my list dot append and add any value With which you want to open like I want to append with ten And print my list. That’s it So as you can see we got 10 in the end Fine so moving on ahead next we have is extend Well again extend is another function which is used to concatenate two different string early example I define my list three as See and d and I want to append my list with My list 3 let’s print and see what residue we get So as you can see up here C&D got added with my list Fine. Well next is insert Well using the index number you can specify any value and between the list For example, I want to insert a number before 3.14 so I’ll write my list dot insert I’ll mention the index number My next number is 0-1 I wanted after one And what value I want for example, I want to insert at 20:30 Alight, let’s print my list and see what is the result You cannot use a string of you you just have to mention the number index number So, yeah, you got my list at first position So what if I want to insert it before 3.14 That was my target right now you updated string is this zero? One, two, three So you want to insert it at third position and you want to insert 40? So at third portion that is zero one, two three So after one two, three what he would come so after one two three four days up here All right So this is how you can insert any number at a given index number So this was all about list. I Hope you understood the difference between a list and a tuple If not, let me just summarize it again a tuple is immutable while a Leicester’s mutable Open and close brackets are used when you’re grouping items under it. And in the case of lists square brackets are used fine So this was all about the list. So let’s move ahead and discuss about dictionaries Well, these are more flexible datatype and Python And You define a group within the curly braces? Fine while defining the elements in a dictionary you have two elements key and its value Fine or in other words We can say that we define an index for each element. Let’s explore various operations that we can perform using dictionary So first one is defining an empty dictionary don’t mention anything within the curly braces, that would be an empty dictionary All right Next is integer keys That is dictionary with integer keys, for example one apple two ball So the key one and two are just integer, right? So it’s a dictionary with integer keys Next is mixed keys dictionary with mixed keys That is we have key of mixed data type for example name is string up here one is integer, right? This mixed key next we have is pairing Is from sequence having each item as a pair dictionary one apple two ball all are taken as a pair element fine Next is accessing dictionary. So what if I want to access a dictionary so I can directly access using the key fine? For example, the word one is associated with key one and word two is associated with key too So if I want to access word one, I can directly write my dictionary of 1 and it will give me output as word one fine Let’s move ahead Now what if I want to find the length of the dictionary then I can directly do it using the Len function Len of my dictionary is giving me the output as to why since it consists of two elements word one and word to find Next is key. Now. What if I want to find the key of my dictionary? So I can just use the key function my Dictionary dot key. It is returning me all the keys within the dictionary fine It is returning me all the key defined within the dictionary Next is value. Well, this function is used to return all the values defined within the dictionary For example, we have Apple and ball as value within this dictionary my dick All right, so that’s why using value function. We got the output as Apple and ball fine It’s move ahead Okay. So now that you have learned about dictionary. So let’s discuss about the last data type that has set Sets are the unordered collection of item within the curly braces. That is Items are defined within the opening and closing curly braces and you don’t have to define the key value on it it has basically unordered collection of item if you are adding elements in the set that make sure that every element in the set is unique for example, you can define a set as My set equal one, two three. Well, it’s almost similar to as dictionary type, but it does not has any key value associated to it It’s the only defense between a set and a dictionary All right let’s perform some of the function using set and understand it in a better way, so The first one is creating set. You already created a set. All right Next is Union Well union of two sets Return the common value from both the set For example my set s 1 equal 1 2 and C My set s 2 consists of 1 B and C. So if I am performing a union on it So it is returning me with 1/2 C and B So basically what it is doing it is returning mede common values of both the set fine Next is the intersection Where intersection is used to return the common result from both of them that is and off my set s1 and my set s2 So what are the common elements and both of them? We have 1 2 c1 BC So we have 1 + C common in both the set so we got the output as 1 + c5 Next is the difference Well, this difference would emit all the common value from both the set and it will return the only value which is a unique and first set for example, we have one to see in set 1 and 1 BC and said 2 so if you subtract it, so you have 1 in both the set so it will emit a 2 and B so nothing can be done about it C and C again It will emit it now since only 2 is left and set 1 so you get the output as 2? All right fine So this was all about the datatype and Python So now that your base is clear, let’s have a quiz, so what would be the output of the following code Test 1 equal Python programming and print test 1 from 2 to 6 In order to know the correct result do post your answer in the comment section below Alright so let’s move ahead Next is the Python flow control Well, these flow controls are the one which control the flow of execution of your program So we have six different types of flow control and Python if else nested if-else for y break and continue Let’s discuss them one by one starting with if-else So, what is this offense, let’s see its syntax If the condition is true then execute statement one else Execute statement two it’s as simple as that. Let’s see the flow of execution. You started you check the condition if the condition is true Then execute block one if the condition is false, then go to the else part and execute block two Fine yeah, this block one. It comes under F part. All right Smooth next let’s move on next is nested if-else. Let’s see the syntax If Condition1 is true. Then execute statement one else if condition2 is true Then execute statement true if none of them is true then executes statement 3 Let’s check the flow control of it Start check for condition 1 that is if condition 1 is true So if condition 1 is true, then the statement executed with the if block is executed. That is execute block 1 a First condition is false then go and check for the second condition that is else if condition now if else if condition is true, then your second block would be executed if None of them are true. Then finally the else block would be executed. That is execute block 3 And yeah Remember one thing there is no condition associated with a else block else block would only execute if all the above conditions are false fine Alright next is the 4. Let’s see the syntax For iterating variable in sequence : execute statement check Well, it’s the example of a program like start we are defining a variable as count equals zero then executes statement then increment the value of count by one and then check the condition if count is less than 10 is 1 less than 10 here. The condition is true so again Repeat it And again execute the statement again increment the value of count by one that is count become 2 and again check F 2 is less than 3 and so on continue this till 10 is less than 10 and When this thing arrived that 10 is less than 10 your condition become false and you exit the loop So this is how a for loop ‘two let’s understand this in a better way with an example. So there’s our code alright, we are defining list fruits as apple banana and cherry for x and fruits x and fruit of 0s apple Then print X X is what Apple so Apple would be printed as the output now again What we’ll do we’ll again go back and check for X and fruits now X of 1 is banana Then Trent X X is what banana so it will trend as banana Again, it will go back X and fruit of two words what cherry? Bring Tex We printed Now if it goes back, there’s nothing left in the flume, so nothing but we printed All right, so you got the final output Isles apple banana and chairing fine Olle So let’s move ahead. Next is the why the syntax for the says While condition is true, then execute set of statement under the why? Start check for the condition If it is true Then execute block one and then again repeat the loop until the condition is true In case the condition is false then just exit the loop Let’s understand this better with an example. Here’s my code Like a equal one And variable part. I got a equal one while a is less than five is Equal one less than five. Your condition is true and what print the value of a All right, so we got the output as 1 after getting the output increase the value of a by two So a plus equal to 2 that is one plus two equal three Now again, go back to the loop check for the condition is three less than five Condition is true Then again go to the print a part what is a three so print a So you will get the output as 3 Again increment the value of 8 to 2. That is a equal 5 now check the condition is 5 less than 5 No, I think here we got a condition as false So we got the final output us one and three, all right, let’s move ahead Here’s another example equal 1 In the variable bar equal one check the condition while a is less than three is one less than three here condition is true Inside the y part the first condition that we check if a more to equal equal zero is one more to equal equal zero No, the condition is false So I will jump to the else part So we’ll jump to the else part and print a is odd Now since the value of a currently is one. So one is odd Fine, then what we do we’ll increment the value of a by one That is one plus one equal to Fine now again, we’ll go back to the Y part Now we’ll check if two is less than three. Yeah, the condition is true fine now what we’ll do, we’ll jump to the F part and Check if two more two equal equals zero. Yeah. The condition is true So this time we’ll check if two more two equal equals zero. So yeah, the condition is true so what it will do it will print the if part statement that is a is even in this case was two so two is even Fine, this is our output Next again, it will go and increment the value of a by one. So now two becomes three Again it will go back to the y part as three less than three. The condition is false So your final output is one is odd and two is even fine So this was about the while statement, let’s move ahead and learn about break So this break statement it is used to break the loop at a certain condition. All right Let’s understand this with the help of this flow chart Started check the condition if the condition is true then the break Then check the break condition if the break condition is false then only execute block one and repeat the loop If the break condition comes to be true then break the entire loop and then exit the loop All right Let’s check it with an example up here For example equal 10 While is greater than zero Check for if he is Not equal to five if it is not equal to five Then print a Else Break up fine. That’s executive say This bench a – – You a – equal to one Stop it you Just mention it up here you So we got the output as nine eight seven and six fine So the moment loop becomes fine. We got exited out of the loop Next we have is the continue statement Well, the continue statement won’t break the loop. It will just keep the statement in case the condition is true Let’s understand this with the help of this flow chart. So start check for condition 1 if it is true then check for Continued condition if it has Falls then execute block and repeat then in case if the condition is true then stop executing that particular block for that particular loop and Cut for that particular iteration and again go back and repeat and check the condition alright Let me just show you let’s just replace the spray. Quit continue and let’s see. What is the result so Yeah, see got the output as 9 8 7 6 4 3 2 1 but you don’t got 5 up here Right. Why? So as I told you this 5 was skipped Fine So this was all about the flow control so let’s proceed and learn about function so what are these function Well a function is a block of organized reusable set of instruction that is used to perform some related action But why do you even need a function? well, what if you have to write a program in which you have to use same set of code again and again for example you wrote a code of 20 lines or 40 lines and those 40 line of code are coming your main program at 10 different places and You have to use those 40 line of code 10 different times in the program so I don’t think that none of you would be interested in writing the same lines of code again and again So in order to resolve this issue, they created function So well this function is a block where you write your code, which you think you might use it again at some point of time therefore it allows the reusability of the code and thus minimizes the redundancy in Python they are basically two types of function. The first one is the user-defined and the second is built-in function Let’s see them one by one. So what is a user-defined function? Well, any function defined by user is a user-defined function try syntax It starts with a keyword def after that an identifier for a function name and Then you pass the arguments into it. All right followed by a semicolon and After that, you mentioned the set of statement inside it. Alright, let’s see an example For example if you want to add two arguments So you define a function as ad and inside ad you pass two parameters as a and B So sum equal a plus B and it turns out that’s it It’s as simple as let me just define a user-defined function for you. It’s like it start with the keyword def After that, I I’ll write a function named as ad inside that I will pass two arguments a and B semicolon And this function should return me sum of a and B All right fine So that is my use a defined function Next is the Belton function Now these built-in functions are a predefined set of functions in Python We have ABS function which it runs the absolute value of a number We have all function which returns to if all items and unutterable objects are true We have any function ask a function been function Bool function and many other built-in functions are available there and Python if you want more information about it You can just check out the Python official doc. Alright Now that we have defined a function now, let’s see how we can call a function so calling a function Well, there are two ways of calling a function. You can either call it by passing a value or by passing a reference Let’s see them one by one So first is pass by value Calling a function by passing the value for example a equal to ten The BEF that is keyword. So we are defining the function as print. The value of B is B and again inside the function we are updating the value of P to be hundred and print the new value of B is B and Then what you are doing change it and we are passing a now since the value of a was 10 So this function would take the value of B as 10 So print value of B is 10 a new value of B is 100. Let me just show you in order to resolve this issue Let’s define the value as a equal 10 define my function That I’ll pass an argument as be Print The value of B is Be African cat Fine and next we’ll update the value of b2b hundred and again print The new value of beers Be Fine so this is my function what I’ll do I Call my function by passing a value calling a function By passing a Value to it So, I’ll write my function and inside that I will pass the value as a Since the value of a was 10, let’s see the output C so we got the output as the value of B is 10 and the new value of B is 100 So this was about calling a function by passing a value to it Next we have is passed by reference Calling a function by passing the reference for example So we have a list as c10 2013 so we are defining a function as change them and we are passing an argument D to it inside the function we have print value of D is d D of 0 it’s a numeric constant We are defining it as 99 D of 1 as 98 then print the value of T is T Then change them now when we are calling a function by passing the reference So what we are doing we are just passing the entire lest of it So what do you get the output? will get the output as value of D is 10 20 and 30 and the new value of D is 99 98 and 30. Let me just show you one example C equal 10 comma 20 comma 30 right Now let’s define my function 2 Inside this D is what? Print The value of D is D. All right. Next we define some numeric literals like Like D of 0 is equal to 99 D of 1 equals 98 and then print The noon value of D is Again d Fine this is my function now if I Call my function using pass by reference So my function – and I pass the value C into it. So let’s see what we get the output So we got the output as the value of D is 10 20 30 and the new value of D is 99 98 + 30 Fine Let’s move ahead Well, when a parameter is passed by reference the collar and kadhi uses the same variable for the parameter Well, if the collie modifies the parameter variable Effect is visible to the collars variable But when a parameter is passed by value the caller and callee have two independent Variable with the same value if the collie modifies the parameter variable the effect is not visible to the column Well, the things that you need to note in this definition are well the variable volt here means the qualit bearable itself Not as if I pass a local variable by reference and assigned to it it will assign the caller’s variable itself You know, what is the meaning of reference and password efference? Well, it is different from the general term reference It is that this reference is temporary and implicit What the callee basically get is a variable. That is somehow the same as the original one Here on the screen is a very good example of pass by reference and pass by value All right And pass by reference you are filling a cup using The reference of the cup and in pass by value you are actually filling the cup using some values So this was all about how you can define a function and how you can call it in Python Let’s get back to the quiz and test yourself The following function takes two parameters and print the first one so fill in the missing code fill in the missing code What is the code my definition function takes first name last name and water? It should print so post your answer in the comment section below and Get it verified with us Holy Next question is a function which returns the X parameter plus 10. All right fill in the missing code Define my function as X and write the entire function for it, you have to write a function which returned the experimental plus 10 and Again, post your answer in the comment section below and will let you know if it is correct or not Similarly we have fourth question. What is the output of Fruit we have We have a cupola pure as fruit equal apple banana and cherry if mango and fruit then turned three plus four else print two into two Again, print the output of the following code in the comment section below with the question number. Alright Now let’s discuss about the lambda function in Python, so what is this lambda function Well, a lambda function is a small anonymous function lambda function can take any number of arguments But can only have one expression Note the fact that lambda function cannot have more than one expression So here’s the syntax Lambda argument : expression for example we have X equal lambda equal and a plus 10 and Print X of 5 so what it is basically doing it’s defining a lambda function That adds 10 to the number pass 10 as an argument and then print the result Let me just show you an edge over a notebook Let’s define a lambda function as X equal lambda Of a such that a +10 and then Print X inside that will pass a value of 5 So what do you think the output should be 5 plus 10 it would be 15 All right, if you change 5 to 10, the output would be 20 All right Let’s move ahead So next is function versus lambda function. So what we are doing up here, we are comparing function with a lambda function so here’s a normal function which takes two argument and return the multiplication result of both of them and Here is a lambda function for the same it’s like our equal lambda X comma Y such that X multiplied by Y and if you Call our 12 comma 3 water will do it will return the result of 12 multiplied by 3. That is 36 I’ll just show you this an algebra notebook For example, I’m defining a function as de f let’s name my function as my team I’ll pass two arguments up here x and y : now return me the product of x and Y. That’s it. It’s done, right? now What if I have to define a lambda function for this, so I’ll write like Z equal lambda X comma Y such that it equals 2x x y Fine now if I want to test this, I will write print Z off I’ll pass two values on it 12 comma 3 So it should return me as 36, right? So yeah, I got the output as 36 fine. Let’s move ahead Next is the power of lambda function Well, the power of lambda function is better shown when you use them as an anonymous function inside another function For example, let’s say you have function definition that takes one argument and that argument will be multiplied with a unknown number, right? For example, you have my function as n returns. Lambda a such that a plus n So you’re defining a variable my sum equal my function of three for example At Sina Jupiter notebook Define my function And we’ll pass an argument as n such that it should return Lambda of a says that a x And honey Now you can use this function definition to make another function that always doubles the number you send in for example, I Can write Double me Equal my function I want to double it so my function of two, I need to multiply it with two now Brent What number do I need to multiply so double Me for I won the double of twelve So double me twelve Output would be 24. So if you want to triple it just change it to 3 you are put would be 36 And so on Holly So this was all about the lambda function fine It’s test yourself right a lambda function that sums the argument a B and C and then print the result Note post down your answer in the comment section below All right Next quizzes. What is the output of F my function and return lambda a into n my W my function to my table 11 Very simple. We just discussed it So just both answer in the comment section below, okay, so this was all about he learned a function Next we have is Python classes an object. So what is the class and object in Python? Well python is a object-oriented programming language and almost everything in Python is an object with its properties and method and a class It’s like a blueprint for creating objects Let’s see an example of class We define a class as class space class name : and inside that you define its objects If you want to call an object, let’s see how you call it. You are defining a variable as obj1 and you are Assigning all the object of my class to it And if you want to print one of the variable of my class so you can access it using obj one. Alright So this was the body class is an object and Python So coming to our last topic of file handling in Python, well file handling is a very important of any web application Well, you have four different features, which you can perform in a file handling You can open read write and delete a file using Python Alright, let’s see them one by one the first one being open So you can use open function to open a file and it takes two parameters the file name and the mode let’s see the syntax Syntax is f equal open path of the file So you have the syntax as f equal open path of the file and its mode fine. Let’s see. What are the modes? So we have four different types of mode read append write and create For reading you specify our it’s a default value and it opens the file for reading and the file does not exist It would return you an error. Next is the append and the mode is a Alright what it will do it will open a file for appending and in case of the file does not exist It would create a new one Next modus W which is right It opens a file for writing and creates a file if it does not exist Fine. I’ll tell you about the difference of appending and writing later in this section Alright next and the final – create That is X it creates the specified file and it returns an error of the file with the same name exist it Creates the specified file and it returns an error if you already have a file with same name Fine. Let me just show you Like F equal open part of the file Open it in my D Drive D slash name on the file test file dot txt And comma What should be my mode it tells a reading mode. I? Hit enter Okay So as I told you earlier it would throw you an error at the file does not exist, right If you want to create it, you have to mention X up here And it will create your file. Let’s go to our D directory and let me just show you the file D so you can see the test file up here, right? So this is how you can open a file and Python or you can even write W up here It won’t throw you an arrow Since it creates a file if it does not exist, but it won’t throw an error in case the file at signum exists Hello Next is the read function Well suppose, there’s our file it consists on document. Hello, welcome to demo file dot txt. This file is for testing purpose. Good luck Let’s open our demo file Test file. Hello everyone Welcome To Intel about good luck See Save it as tests filed on one fine Let’s see. What a operation we can perform on it Now what if I want to read it? So how will I do? So f equal open demo file dot txt Comma mode R R is worth reading mode and then print F dot read fine Let me just open that file for you F equal open The path of the file. It was D colon slash Test File one dot Txt and in what mode I want to open it in read mode And I want to print all the content which is present in that particular text file so F dot Read it first and then print it Hit enter. So these are the content of my file. Hello everyone. Welcome to Intel apart and good luck Fine Now what if I want to read some parts of the file? Like I want to read the first five characters of the file, so what will I do? Let’s see just copy and paste it I want to read the first five characters. So just mention five up here. What will it do? Print first five characters from the Text file. Hey Tonto – hello 0 1 2 3 4 & 5 so these are the first five character from the string. Alright? So Now what if you want to read the first line so for that you have a function as read line All right You have function as read line, which would be used to return one line or the first line Again, copy it We stood up here read Tradeline just hit enter, so You got an error? Okay, I oughtta be like, okay. Sorry L is small read line So you’ve got the output as hello everyone there is my first line Next what if I need to read first to line of my document? So what will I do in that case? so in that case what you can do you can use read line twice and It will print first two line of your text Like this again do it once more f dot readline Tanto In the same manner, okay. So what you got – you are the first line from your text, all right Now looping through the file now what if you want to read the file line-by-line so you can use a for loop in that case So it’s like for X in F Such that Print X if X is 1 then print first line X is 2 then print second line 3 so on All right So you’ll be able to read the file line-by-line. Hello everyone. Welcome to intel apart and good luck Next is reading and creating Now to write to an existing file You must add a parameter to the open function like you can write a or W a will append the file that it will append or add something at the end of the file and W it will override any other existing content for example Let’s go to be straight up here instead of our let’s mention a it’s for our bending and f dot right Whatever you want to write This is the appending statement Executed Fine now if you will check the file, let’s check the existing file Well, Nick and Hadley landlord, I’ve For X and F Print X Executed So yeah, so as you can see, this is the appending statement got added at the end of the file Now what if I use? w instead of a So, what will it do this film Overwrite my statements So here I can write print f dot right Print f dot right this will Overwrite everything Executed done now. Let’s check it again. What are the contents of my file? So if you check it So only this is left This will overwrite everything Fine, this was about are you can write to an existing file Alan Next we have is creating a new file. Well, you can create it using X a or W Using X creates a file and it returns an error if the file exists Append it creates a file at the specified file does not exist And finally W it creates the file in the specified file does not exist For example F. Open my file or dxdx, we’ll just create a new file In case you are attempting to create a file with the same name or to throw you an error For example I don’t know this This file already exists, right if I am trying to create it again, it will throw me an error All right, as file already exists or file exists fine Next and last we have is the Delete Well import OS module is used to delete a file How you can delete it import the OS module or start to remove demo file dot txt Tata? But for that, would you have to do you have to first close the open filing So f dot flows inside this mention the name of the file At the path of the file de colon slash Test file one dot txt first close it Sorry you It’s like f dot close Close the file Now if you want to delete it, you need to import OS mode. You’ll do it import OS and OS dot Remove That name of the file, what is the name of the file? Test file 1 dot txt. That’s a You Or the error the system cannot find this specified file, so I guess the file s now start deleted Okay, I haven’t specified the path of the file I just written the name of it so I’ll write d : And the file got deleted so there is no test file by ali fine So, thank you guys This was all for this session in case you have any doubt. Feel free to add them on the comment section below We’ll be more than happy to resolve your queries. Thank you

26 thoughts on “Python Tutorial for Beginners | Python Full Course From Scratch | Python Programming | Intellipaat

  1. Hope you learned something new from this Python Tutorial video. Please be kind enough to like the video and subscribe the channel. Make sure to perform hands-on side by side and feel free to ask your doubt in the comment section below.

  2. #q1

    ans: thonP

    # q2

    first = "sam"

    last = "roberts"

    def my_function(fname, lname):

    print("your first name is : ", fname)

    my_function(first, last)

    # q3

    a = 10

    def my_function(x):

    print("Old value of x is :", x)

    x += 10

    print("new value of x is :", x)


    # q4

    Ans: 4


    z = lambda a,b,c: a+b+c



    Ans: 22

    Please tell if my answer is correct or not…..

  3. Sir, I do not understand the 3rd question which is A function which return the x parameter +10.
    Please, explain in detail.

  4. I can't find Latest Python 3 Release – Python 3.7.1 only Latest Python 3 Release – Python 3.7.2 is available. It that okay too?

  5. In string datatype, if we want to print only "Tutorial" ( i mean the second word), how would we write this? Please explain.

  6. Another one can somebody tell me the output of this
    x , y , z=10 , ' ' ' 20

    30 ' ' ' , 50

    print ( x , y , z)

  7. Another for you all guys




    print(y)……………tell me the output without executing it

  8. All right guys i have a challenge for you…..CAN YOU PRINT THE LENGTH OF A STRING without using the length function and no loops …..i need one line command to print the length of the string

Leave a Reply

Your email address will not be published. Required fields are marked *