Stats: 3,190,615 members, 7,941,364 topics. Date: Friday, 06 September 2024 at 04:04 AM |
Nairaland Forum / Science/Technology / Programming / Learn Python With Kash (2825 Views)
Python With MYSQL: Registration Form In Python Using Tkinter And MYSQL Database / Learn Python With Me / Who Wants To Learn Python With Me? 90 Days Challenge (2) (3) (4)
Learn Python With Kash by HappyPagan: 10:08am On Jun 01, 2020 |
I will be teaching you how to program using the Python programming language. Python offers a relatively easy path into the world of computer programming, and this course will be your guide as you begin your journey to becoming a Python programmer. Why would you want to become a Python programmer? What are the benefits of learning how to program with Python? Why should you take this course? The world we live in today is a digital world. Computers are everywhere, and we use them to run software that manages our business information, provides live score updates, transfer money to our loved ones, pay for data subscription and so much more. Software is built by programmers using various programming languages. Quite a number of programming languages exist and they are used for various purposes and, sometimes, picked based on preference. The python programming language can be used to develop applications (both for desktop and web). if you want to learn a bit of hacking, python is a good choice especially in cybersecurity. if you have an interest in data science, Python is there for you too. What about web development? You can use Python too. Of course, this course will be more specific. I won't be teaching you web apps or cybersecurity - but you will be introduced to the basics, and you will learn more than enough to continue in whatever path you so desire at the end of the course. So have fun, enjoy, and ask lots of questions. Because the road ahead will be a tough one, but all will be well in the end. ![]() 4 Likes 1 Share |
Re: Learn Python With Kash by HappyPagan: 10:09am On Jun 01, 2020 |
Course Content - Python ( installation, setup, and programming concepts) - Git and Github ( every modern programmer should know this. sections will be introduced as the course progresses.) - Visual Studio Code ( my choice for text editor for this course.) Introduction This course will teach you Python, a computer programming language used in various fields, from data science to software and web programming. If you are new to programming, this course for you - it is assumed you have no previous experience with programming. In this course, you will also get an introduction to GIT and GITHUB, two tools that you will find useful as you develop your programming skillset. It will help you participate in collaborative programming (programming with various people all over the world). Course requirements - a laptop. (Preferably a working one.) - internet connection. (We'll be downloading some software online. Also, just a basic requirement of life today.) - perseverance. (No matter how well I might explain a topic, my lessons won't turn you into a programmer. Learning how to program will require effort from your end... You'll feel sad sometimes if it takes time for you to understand a concept. You must be willing to work through it - where there is inspiration or none. ) 3 Likes 3 Shares |
Re: Learn Python With Kash by HappyPagan: 10:11am On Jun 01, 2020 |
Lesson 0 (For when the going gets tough) You probably begin this journey with much motivation and eagerness to learn. Along the way, you will encounter difficulties - some concepts might take you time to grasp, you will struggle to solve some of the exercises, or you might just lose interest. This is a note for you, to read before you begin. Becoming a good programmer will take time and effort, and a lot of patience on your end. Practice and persistence will guide you to the end. Good luck. |
Re: Learn Python With Kash by HappyPagan: 10:11am On Jun 01, 2020 |
Lesson 0 (For when the going gets tough) You probably begin this journey with much motivation and eagerness to learn. Along the way, you will encounter difficulties - some concepts might take you time to grasp, you will struggle to solve some of the exercises, or you might just lose interest. This is a note for you, to read before you begin. Becoming a good programmer will take time and effort, and a lot of patience on your end. Practice and persistence will guide you to the end. Good luck. 1 Like 1 Share |
Re: Learn Python With Kash by HappyPagan: 10:16am On Jun 01, 2020 |
Lesson 1 - Setup and Installation Today's lesson will be about setting up our working environment. Think of the working enviroment like a workshop that contains the tools that you will need to build/write Python programs. To write Python programs, you have to install Python and also install a text editor. Please follow the attached guide and complete the exercises. 2 Likes |
Re: Learn Python With Kash by HappyPagan: 10:18am On Jun 01, 2020 |
Class continued here. Link: bitDOTly/2Lr4q13 (change dot to fullstop) |
Re: Learn Python With Kash by olamidedivotee: 10:42am On Jun 01, 2020 |
Following |
Re: Learn Python With Kash by Chazzyboy: 10:57am On Jun 01, 2020 |
Ok |
Re: Learn Python With Kash by HappyPagan: 7:03pm On Jun 01, 2020 |
olamidedivotee: Check the message above yours. |
Re: Learn Python With Kash by HappyPagan: 3:51pm On Jun 06, 2020 |
Classes still going on. |
Re: Learn Python With Kash by HappyPagan: 4:16pm On Jun 13, 2020 |
LESSON 1: Writing Python Programs Now that we have both python and Visual studio code installed on our systems, we can start writing programs. What is a Program A program goes by many names - software, code, application, executable. Computer programs are instructions that run on a computer to help us accomplish task. A computer program can be of two types: CLI (command-line interface) or GUI(graphical user interface) GUI programs have icons, mouse, and other graphical utilities to aid user interaction. They are much easier to use, and definitely more fun. Your browser, video games, and Office applications are all examples of GUI programs. CLI programs run on a blank screen - there are no icons, mouse, menus. Just a dark screen with text. As a beginner, these are the programs we will begin writing. GUI programs are usually more complicated to design and build - we will take a look at one towards the end of the course. Writing our first program. There are many ways to create and run a Python program. In this exercise, we will create a simple program that prints a message on your screen. We will first learn using the Python Shell, then IDLE, and finally Visual Studio Code. Using the Python shell From the Windows Start button, type 'Python' and click on 'Python 3.8' to start the Python shell. The Python shell (sometimes called console) allows you type Python code and see the result immediately. You will notice a series of greater-than symbols on your screen like this '>>>'. This is called the Python prompt, and signifies that Python is waiting for you to give it some commands to run. Some of these commands are included in the Python Standard Library. The Python Standard Library Let's say you want to display some text on your screen. A lot of technical knowledge is required to get a computer to display even a single character. Just think about it for a second - how do the characters on your screen appear? Fortunately, Python comes with a lot of code in-built that we have access to do common tasks like displaying text on a screen. These group of code is usually referred to as the standard library. Think of it as help for the programmer - you don't always have to write code from scratch. The Python standard library comes with an in-built print program that enables us print some value or text on the screen. This program is called the print function. So, let's say I want to display my name on the screen, I can use the print function from the Python standard library to do that. To print my name on screen, at the Python prompt, type the following print('I am a genius') Press Enter when you are done.You should see the text within the brackets display on your screen. So, congratulations, you just ran your first line of code. You can display messages on your screen. Further exercise Try typing the text without the quotation marks around it - print (I am a genius) did the program run? What message did you get? Put one of the quotation marks in and see what happens. Do you get the same error message? Why do you think this happens? SYNTAX Just like the regular language we speak, computer languages also have rules that determine what makes sense and what doesn't. We usually refer to this as the program's syntax - a set of rules that determines what is a valid python program and what isn't. The program isn't running correctly because it violates the program's rules. Every character you type in a program (even blank spaces) have some kind of meaning. Let's analyze the print function a little before we move forward. Be patient, you will be writing real programs soon ![]() When you type print at the command line, you put a bracket beside it. print() Inside this bracket, you will put the information that you will want the program to display on the screen. information can be of different types - for example, information about you could include your names, email addresses, age, and number of siblings. Your name will probably be made of characters from the English alphabet, your email address will include special characters like the '@' symbol, and your age and number of siblings will be numeric. Each of this information has unique characteristics, and these characteristics determine what kind of operations can be done on them. For this course, we will call the information you provide to print a 'value'. Values are categorized into categories called data types. In Python these data types are - Numeric (for values like age, price or cost of an item) - Strings (for values like your name, email addresses, home address etc) - Boolean (special set of values to represent on/off or true/false) - more advanced data types like tuples and lists. You don't need to commit any of this to memory. With time, you will be able to identify and know when to use them. So, let's go back to our print function. The message we want to display on the screen is a string. String values must be included within quotation marks (single or double) to be considered valid. so to print the messsage 'i am a genius', we type it into the bracket and enclose in single or double quotation marks. print('i am a genius') print("i am not a genius" ![]() print('I am becoming a programmer') print('print') You can also use the print function to display numeric values. Numeric values should not be enclosed in quotation marks, else they will be treated as strings. print(1) print(234) you can even do negative numbers print(-2) Now try this print ( 2 + 2) print ( 'Baby o' * 3) print ( 'Baby' + 3) print (3 + "Baby" ![]() 1 Like 1 Share |
Re: Learn Python With Kash by HappyPagan: 4:16pm On Jun 13, 2020 |
Currently on Lesson 11 in the Telegram group. Class continued here. Link: bitDOTly/2Lr4q13 (change dot to fullstop) 2 Likes 1 Share |
Re: Learn Python With Kash by HappyPagan: 7:55pm On Jun 13, 2020 |
Lesson 2: Integers, Strings and Operators In the last lesson, we learned about values and data types. Computer programs manage and organize information into different groups called data types. We also learned to display values or messages on our screen using the print function. In this lesson, we will dive some more into values, data types and operators. Data types The data type of a value is important. It determines what kind of operations can be run on it - can it be used in a mathematical expression? For example, I can add or subtract from your age because it is a numeric value. You wouldn't expect me to subtract 1 from your name - that just wouldn't make sense. Just like the print function displays messages on screen, the Python standard library also provides a type function that displays the data type of a value. Let's use this function to learn about the various data types in Python. Numeric datatypes Numeric data types refer to numeric values and include integers, floats and complex numbers. For this course, we will only integers and floats will be our only concern. Integers are whole numbers like 1, 100, -100, etc. type(1) the message <class 'int'> is displayed indicating the value is an integer. data types are also called classes. think of class as 'classfication' not 'dancing class' type(-1000) Floats or floating-point values are numeric values that include a decimal point. They are usually used to represent currency or fractions. type(2.5) type(100.2) You should see the message 'class 'float' displayed on your screen. String datatype A string is a sequence of zero or more characters, enclosed in single or double quotation mark. That definition may sound a bit technical. I will provide some examples. You can have an empty string. type('') Notice there's no space in between the quotes. Nothing exists between the quotes. You can also have a string with just a space. type(' ') (a space, even though invisible, takes space) Strings can be longer type('helloooooooooooo') They can also include spaces and numbers, and other characters too. type('good morning my beauitful 2-eyed neigbor.') Boolean Datatype Boolean datatype represents only two values: True and False. Notice the 'T' and 'F' are capital. If you type them in lower-case, Python will throw an error. If you put quotes around them, Python will interpret it as a string. type('true') type(true) type(True) type(False) A few more data types exist which we will discuss in a future class. For now, let's talk about operators. OPERATORS You may have come across this arithmetic symbols in a Mathematics class ( +, -. /, *). These are used to symbolise the addition, subtraction, division, and multiplication process. In Python, these symbols are called operators, and how they operate is based on the data type of the value which they are operating on. Python provides arithmetic operators. The values they operate on are called operands. So, for example, in the statement below, there are two operands (2 and 3), and the operator is the addition symbol. It combines the operands on both sides to give 5. print(2+3) Note that both values are numeric. You can also add an integer and a decimal number print(2+3.5) You can also do subtraction, multiplication, and division print(2*5) print(3-4) print(4/2) Note that the division operator dislays a float. If you want an integer(that's you do not require the extra part) you can do use the floor division operator. print(4//2) There is also a modulus operator that displays the remainder when one number is divided by another. It is represented by th percent(%) symbol. print(4%2) print(5%2) Python also provides an exponent operator. so to calcuate 2 raised to power 3, print(2**3) Strings and Arithmetic operators The Addition and Multiplication operators can be used for String values. print('Join' + 'Us') This is called concatenation. If you want spaces included, you can do this print('Space' + ' ' + 'Us') You can also repeat a string a set number of times using the multiplication operator. print('Multiply me' * 3) Managing text/strings is a very important part of programming - strings have a host of operators that are used to manage and manipulate them. We will talk more about them in a lesson devoted to strings. Note on Syntax and errors. In the previous lesson, we spoke about syntax, and how it determines well-formed programs. The art of programming is a process of finding and fixing errors in your program. Sometimes, the language helps you find this error by printing error messages (the message will usually include the line number at which the error occured and a description the error) Learning to ask questions and use Google is a critical part of programming. Being able to research and extract useful information is a key skill if you want to be a solid programmer. You don't need to know everything about a programming language to be a great coder - you just need to learn how to find information quickly and efficiently. In our next lesson, we will learn to write and store our programs in files. The terminal method we are using now is for testing single lines of code. We can create files with multiple lines of code and store our programs. 1 Like |
Re: Learn Python With Kash by HappyPagan: 11:37am On Jun 14, 2020 |
Lesson 3 : A short guide to Visual Studio Code So far, we have typed single lines of code into our terminal, and gotten a result displayed immediately. Creating programs involves writing more than a single line of code - there's a lot of text to be written and managed. To do this, we have software we use to write software. Visual Studio Code is a software for writing software - a code/text editor. Developed by Microsoft, it's more advanced than a regular text editor(Notepad for example). It provides features that help our code writing process be much easier. In this lesson, we will explore Visual Studio by learning what it is and how it works. We will begin by familiarising our selves with the user interface, then we will create and run a Python program in Visual Studio code. Starting Visual studio code Click the Windows Start button, type "visual studio code" in the search bar, and click on the program to start it. When you open Visual Studio code, you see a screen similar to the one below. Let's get to know our new workshop - this is where we will be producing our code. The Visual Studio Code user interface VISUAL STUDIO CODE INTERFACE The Editor: This is the main work area. This is where most of our creating and editing we will be done. The first time you open Visual Studio Code, this area will show a Welcome Screen. From here, you can start a new file or project. It also shows recent files you may have worked with so you can open them quickly. On the right side are some tools to help you customize the user interface, and some lessons to get you acquainted with VSC. The Editor area is highlighted in the white rectangle. The Menu Bar The menu bar sits at the top of the Visual Studio Code interface. It contains a host of submenus that can be used to make various changes to Visual Studio code. We will explore these menus indepth as the course progresses. The Menu Bar is at the top of your screen. In the middle of the menu bar area, you'd see the title of the current document you are working on or viewing. For now, it shows 'Welcome' cos you've still got the Welcome screen on. On the right side are the Window buttons, that let you minimize, maximize, or close Visual Studio Code. The Activity Bar and SideBar To the far left of your screen resides the Activity Bar. Beside it on the right is the sidebar. The activity bar defines various activities you can perform in visual studio code The activity you click on the Activity bar will determine what shows in the sidebar. The activity bar on the left, and the sidebar beside it. Clicking an activity on the activity bar will change the sidebar. You can hide or display the sidebar by pressing Ctrl + B. This is useful if you need more space on the right side for your editor. The Activity bar contains the following activites File Explorer This will display your project files in the Sidebar. It will also display a list of open editors (you can open multiple files for editing in separate editor tabs. For now, it will show just one editor(welcome). Search Use this to search for text in your files or project. Software can get very large and complicated. Navigating through text with the search features saves time. Source control This is a more advanced topic which we wil discuss when we learn Git and Github later on. Run and Debug An advanced topic for testing and debugging your code. To debug is to remove errors from your code. We will get to use this as lessons progress. Extensions Extensions extend the functionality of Visual Studio Code - think of them as apps for VSC. We will install a Python extension and a few others soon. The Status Bar This bar is at the bottom of the screen. It displays information about the file you are working on in the editor - number of lines, characters, etc. The Panel The Panel is also used for testing and debugging, and contains an integrated terminal. We will be using the integrated terminal to run our python programs. You can access the Panel by pressing Ctrl + ~ on your keyboard. So far, so good. Now that you have had an overview of the general sections of Visual Studio code, I hope it doesn't feel as overwhleming as it was whe you first opened it. Next, we will write and run a program using Visual Studio code. 1 Like |
Re: Learn Python With Kash by HappyPagan: 8:42am On Jun 19, 2020 |
Lesson 4: Writing and executing programs in Visual Studio Code In the last lesson, we became familiar with the interface of Visual studio code, our text editor for this course. We are now ready to start writing programs. Creating a Python file To create a new file in VSC, click on 'File' in the Menu Bar, and click on 'New File'. Alternatively, you can use the keyboard shortcut (Ctrl + N) File>New File (Ctrl + N) Take a look at your sidebar. Notice under Open Editors, it now shows 'Welcome' and 'Untitled-1'. On the right side (the Editor), there are now two editor tabs: one for Welcome, and another for Untitled-1. To close the Welcome editor tab, move your mouse to the right edge of the tab. An 'x' will appear - click on it close the 'Welcome' editor tab. You should now have just your untitled document open in the Editor. For now, it's empty and there's a blinking cursor just below the tab. This is where we will start typing our program. When we used the Python shell, we were limited to writing just a single line or instruction. With a text editor, you can write multiple lines of code and save all that code in a file. Each line of code is called an expression or a statement, and a software called the Python interpreter will execute/run each statement/line in the program. As an example, we are going to write and run a program that executes 3 print statements. In your editor, type print('Hello there') Press 'Ctrl + Enter'. This will move the cursor to the next line. If you press the Enter key without holding down control, the cursor will move the bracket down to the next line. There is an explanation for this odd behavior which we will talk about later. For now, notice on the left of the text you just entered, you have two numbers showing '1 and 2'. These numbers indicate the line number of the program. This is useful especially for debugging - Python usually provides the line of the code at which an error occurs. On the second line, let's print another statement. print('Welcome to Python') We have now written program that displays two messages. You can save it by going to FIle>Save (Ctrl + S). This will open a 'save as' dialog box. Select a folder in which to save your file. Give your file a filename 'myFirstProgram.py' and save it. Click 'save' or press enter to save the file. We have now saved our first program. To run the program, we will need to use VSC's inbuilt terminal. Presss 'Ctrl + ~' to open the Panel. Click on the Terminal tab. Using the Terminal The Terminal is similar to the Python shell we used earlier, but more advanced. Whereas the Python shell was created to interprete or run Python commands, the Terminal is a Windows feature - you can use it to run commands that will be interpreted by your operating system. To run python programs in the Terminal, you type 'python filename' at the command prompt. In the Python shell, the command prompt was three greater-than symbols (>>> ![]() In the Terminal, you have a string showing your filepath. The filepath is like a map showing where exactly your file is stored on your hard disk. Reading from Left to right: You can find this file on my hard disk - the C drive (C ![]() This folder is called the working folder, or working directory. It can also be referred to as your project directory. To run myFirstProgram.py, type ''python myFirstProgram.py". Press Enter to run the program. The program executes and the result is displayed in the terminal. You can hide or display the Panel by pressing Ctrl + ` |
Re: Learn Python With Kash by HappyPagan: 8:45am On Jun 19, 2020 |
Lesson 5: Writing programs In the last lesson, we wrote and executed our first python program. At this point, you should know what Python is, and how to write and run a python program in Visual Studio Code. In today's lesson, we will be writing new programs that do much more than printing messages on the screen. We will learn about variables and how they can be used to 'hold' values. We will also talk about the input() function, a program from the Python standard library that is used to accept information from the user. Thinking Before Writing Writing programs is more than just memorizing Python code. Understanding what a program should do, and how it should do it, requires some level of planning and planning. For the little console programs we are writing now, it doesn't matter so much. Think of the programs you write as a script the computer has to follow; it will follow you like a zombie, even if your code leads it to crash. Point being computers are dumb, so you have to be really, really specific when giving them instructions to follow. A program is a set of instructions that the computer is asked to execute - and those instructions must be clear, must contain no errors, must be precise. Let's engage in a little activity. Let's say you want to fry an egg for breakfast. How would you go about it? Can you write down the process? This is my attempt below. ''' Check if egg dey house. If egg no dey, go buy egg. If egg dey, get a bowl. Crack egg into bowl. Pour some salt. Whisk eggs. Cut some onions and pepper Put pan on cooker and turn it on. Pour oil in pan and will till hot. Pour pepper and onions in pan. Pour in egg. Flip egg. Put egg in plate. Turn off cooker. ''' That feels like a lot of work, does it? Still want that egg? It's the same with programs - you must understand the process and write it clearly to have a program that functions properly. Imagine we put the eggs in the pan before cracking them, or poured salt in the pan before turning on the cooker? These are errors, and errors come in different types too. Just as we read the list of actions from top to bottom, the instructions in Python programs are read and executed from top to bottom. Each line in a program is a statement or expression - a program can have one or more expressions. A program is a list of instructions the computer has to perform. In VSC, create a file called program1.py and type the following. `print(1 + 2 * 3) print((1+2) * 3) print(40 - 5 * 3) print(1-2*3+4) print(1-2*(3+4)) print(1-2*(3+4) Notice the last line throws an error. Can you figure out why? In th1r cases, notice how the answer varies depending on how the expression is framed. This is because of operator prece1dence. Operator Precedence defines the order in which operators are evaluated and operators are evaluated from highest precedence to lowest. So, on line 1, you get 7 because the Multiplication operator has a higher precedence than addition and so 2 * 3 is calculated first, before adding to 1. Below is the order of precedence for coommon mathematical operators. ** Exponent % Modulus/remainder // Integer division / Division * Multiplication - Subtraction + Addition You can use the parenthesis symbol () to override operator precdence like we did in lines 2 and 5. In VSC, create a file called program1.py and type the following. print(1 + 2 * 3) print((1+2) * 3) print(40 - 5 * 3) print(1-2*3+4) print(1-2*(3+4) Notice the last line throws an error. Can you figure out why? Can you fix it? In the other cases, notice how the answer varies depending on how the expression is framed. This is because of operator precedence. Operator Precedence defines the order in which operators are evaluated and operators are evaluated from highest precedence to lowest. So, on line 1, you get 7 because the Multiplication operator has a higher precedence than addition and so 2 * 3 is calculated first, before adding to 1. Below is the order of precedence for coommon mathematical operators. ** Exponent % Modulus/remainder // Integer division / Division * Multiplication - Subtraction + Addition You can use the parenthesis symbol () to override operator precdence like we did in lines 2 and 5. Working with Variables So far, our code has been pretty basic. Programs will work with data/information of various types. When we type these values directly in code, they are called literals - they are literally in the code. Variables are a way to give values names or labels. These labels can then be used to refer to those values. Some textbooks describe variables as containers that hold values - creating a proper mental concept of how variables work is quite valuable. Let's look at an example to understand when and how to use variables. Creating a variable In VSC, create a file called program3.py. Type the following. name = 'Nochi' print('Hello' + ' ' + name) Save and run the program. What's the result? Let's go through our program. On line 1, we have 'name = 'Nochi'. The name is a variable we have created to describe a particular kind of value. Picking variable names that properly describes the values it refers to is a good skill to learn as a programmer. The equal symbol beside name is the mathematical equivalent. The equal symbol here is a Python operator - the assignment operator. It's called the assignment operator because it ASSIGNS the value on its right side to the variable on its left side. So, 'name = 'Nochi' creates a string value and assigns that value to a variable. Now, rather than typing the string in an expression, I can simply use 'name' to represent that value. When Python sees 'name', it will be replaced with the value it refers to. So variables are useful in the sense that you can use them to hold values. A variable can only hold one value at a time, so if you assign another value to it, that previous value will be overwritten and lost. name = 'Nochi' name = 'Guyman' print('Hello' + ' ' + name) Variables can also hold values of any kind, not just string values. Add the following lines to the program. eggs = 2 cows = 4 totalEggsAndCows = eggs + cows print(totalEggsAndCows) Save and run the program. Take a look at the result. Did it run as you expected? So far, the programs we have written has values stored in code. A Python program can get values from the user through the keyboard. We use the input() from the Python Standard library to get input from the user. Create a new file and save it as program2.py print('Good day, please enter your name: ) name = input() print('Welcome to Python Programming, ' + name) Run the program. What's the result? First, the message asking you to enter your name is displayed. On the next line, Python sees the input() and stops, waiting for an input from you. Type your name at the keyboard, and press Enter. This will cause input to create the value you have entered, and that value will be assigned to name. On the next line, the program prints a customized welcome message for you. 2 Likes |
Re: Learn Python With Kash by HappyPagan: 8:46am On Jun 19, 2020 |
Join us on Telegram. Link: bitDOTly/2Lr4q13 (change dot to fullstop) 2 Likes |
Re: Learn Python With Kash by onlineit: 11:29am On Jun 19, 2020 |
Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles Python Programming Training 1 Like |
Re: Learn Python With Kash by nduprincekc(m): 1:07am On Jun 20, 2020 |
Nice |
Re: Learn Python With Kash by kkins25(m): 3:16am On Jun 20, 2020 |
HappyPagan:maybe joining would reed me of the procastination and finally finish this course i started yeats ago. procastination really is my greatest devil |
Re: Learn Python With Kash by HappyPagan: 9:56pm On Jun 20, 2020 |
kkins25:Procrastination is part of the journey. Don't feel guilty about it and get negative or sad. I've tailored the lessons so you shouldn't need more than an hour going through each. There are also exercises so you can practice. You will procastinate. Even as a tutor, I find myself procastinating a lot - I have got a shitload stuff I was supposed to work through from last year.. they are still on my task list. So come join us.. just do it. ![]() 2 Likes 1 Share |
Re: Learn Python With Kash by kkins25(m): 10:16am On Jun 23, 2020 |
HappyPagan:Joined. |
Re: Learn Python With Kash by HappyPagan: 11:33pm On Jul 05, 2020 |
kkins25:Nice. How is it going? |
Re: Learn Python With Kash by HappyPagan: 11:37pm On Jul 05, 2020 |
Lesson 6 : Arguments and type conversion In our last lesson, we learned how to create and initialize a variable using the assignment operator. Using that knowledge, we were able to create a program that took and displayed information we provided. Today's lesson will focus on type conversion - a method of converting a value's data type. We will also learn more about how some of the standard library functions we have used so far. We will learn about arguments and how they let use functions more purposefully. Let's say we write a program that takes a person's name and age, and displays it. print('What is your name?') name = input() print('Please enter your age:') age = input() print(type(name)) print(type(age)) What will be the output of this message? What's the type of the age value? Is that correct? Why is the value a string not numeric? A Little Bit On Functions and Arguments To understand what's going on in that code, let's talk a little bit about functions. Think about functions as little programs - yes, like a program within a program. A function does something - it prints a message, takes input, gets type of value - a function is a short program that performs a specific function. Some of these little programs come installed with Python - they are part of the standard library and you can use them in your own programs. It's also possible to create your own functions - this we will learn in a future class. Let's examine the print function. Displaying a message on your screen usually would require a lot of technical details - the print function hides all those details. It says, if you want to display something on your screen, just call me. So we call the print function by typing its name with a bracket after it. print() This calls the print function. In fact, if you run this code, it will run without an error but print nothing. Why? Becauese the brackets are empty - there's nothing in them. The parenthesis after the function name do just more than call the the print function. The bracket also provides a list of values that the print function should display. It says to the function - 'oh boy take these things use do your work' So, the bracket can be empty. print() Or we could tell print to display a value (numeric, string, or Boolean) print('aechi') print(1) print(False) We can also provide as many values as we want, so long as we separate them with a comma. Make sure they are all in the bracket. print('Kachi', 1, True, 'Kash', 15) print(2,3,5) print('Hallo', 'Come on', 'Get out') Each value you provide to a function is called an argument. In our example, the first print function has 5 arguments, the second and third have 3 arguments. You can even put expressions or variables in place of the values. As long as they are valid, the print function would display them. name = 'Kachi' age = '31' print(name, age) print(name + ' is my name ', age + ' is my age') So, the print function accepts arguments, and prints those arguments to the screen. An argument can be a value, variable, or expression. An argument can even be a function - functions can create values too, like the input() function. Quick Review To use a function, type the name of the function followed by a parenthesis. functionName() If the function requires arguments, you include them in the brackets, separated by commas. The input() function The input function accepts an input from a user, and creates a string value from the user's input. The input() function can also take a single argument (must be string) . We have not used this previously, let's try it. name = input('What is your name?') print(name) Run the program. Notice what happens? The string we provided displays as a prompt to the user. Prompts are used to describe to the user what information they need to input. We can do this too. prompt = 'What is your name? ') name = input(prompt) print(name) When you enter your information, the input function turns that to a string value (creates a string) and then passes the value to the name variable. Now, turning the values to a string might be convenient, but this can cause our programs to run into problems. Let's look at this program that takes a number from a user and increases it by 2. number = input('Please enter a number: ') print(number + 2) This will cause a TypeError. Python cannot add a string to a number. TypeError: can only concatenate str (not "int" ![]() So what do we do? We need to find a way to change the datatype of the value the input function provides to the variable. For this, we have type conversion functions. Type Conversion: int(), str(), float() If you want to change a value from one type to another, use the type conversion functions. Just provide the value, and the functions will change the value to the type you prefer. Use int() if you want an integer, float() if you prefer a decimal value, or str() to create a string value. print(int('3')) print(float('3')) print(str(3)) With our new found knowledge, let's run our program that increases a number by 2. Put the input() as an argument in the int function. number = int(input('Please enter a number: ')) print(number + 2) When the program runs, Python starts execution at line 1. The input function runs first, acccepting input from user and creating a string value. That string value is passed to the int function as an argument. int takes the value, and creates a new integer value which it passes to the number variable. The expression 'number + 2' in the print function on line 2 can now run successfully. Sidenote The print function in Python is a function that outputs to your console window whatever you say you want to print out. It might appear that the print function is rather useless for programming, but it is actually one of the most widely used functions in all of python. The reason for this is that it makes for a great debugging tool. "Debugging" is the term given to the act of finding, removing, and fixing errors and mistakes within code. If something isn't working right, you can use the print function to print out what is happening in the program. Many times, you expect a certain variable to be one thing, but you cannot see what the program sees. If you print out the variable, you might see exactly what value the variable holds. |
Re: Learn Python With Kash by atheistandproud(m): 12:14am On Jul 06, 2020 |
Following |
Re: Learn Python With Kash by HappyPagan: 1:32pm On Jul 06, 2020 |
atheistandproud:Welcome. |
Re: Learn Python With Kash by Felixnyle: 1:43pm On Jul 06, 2020 |
HappyPagan: Haven't been able to sign in what am I doing wrong.I changed the dot to fullstop still |
Re: Learn Python With Kash by HappyPagan: 1:52pm On Jul 06, 2020 |
Felixnyle: Copy the link to your browser. Remove the DOT. Put a . (full stop) That should work. Worked for everyone else. |
Re: Learn Python With Kash by HappyPagan: 9:55pm On Jul 06, 2020 |
Lesson 7 : More strings and print, and the len() function Good job so far. You have made quite some progress since you started your journey as a Python programmer. In today's lesson, we will be doing a review of what we have learned so far - but that doesn't mean we won't be learning anything new. We will. Lot of programming has to do with strings - think about most of the apps you use and how they show you information and get information from you. If you use Facebook, you might have seen (ThisFriend posted Something some Hours/Mins ago). Instagram shows you number of likes and followers for each account, your bank account shows your account balance when you get a credit or debit. Such personalised messages are created by manipulating, joining, and formatting strings. Programming languages come with a lot of tools for manipulating strings. Manipulating strings, or formatting them, is important when you want to determine how and what information is displayed to the user. Let's take a look at a few of these tools. Escape Sequence Create a new file called EscapeSequence.py. Type the following code and run it. print('I'm going to the cinema. Would you like to join me?') It should cause a SyntaxError. Notice how errors can be of different types? Last class, we had a Type error cause one of our values wasn't valid. A SyntaxError hints at a problem with your code 'grammar'. The error message also includes a part of your code, the line where the error occurs. It will also display a caret(^) symbol to indicate the exact position at which the error occurs. Remember, code is run from top to bottom, reading from left to right, line-by-line. In our code, the caret symbol indicates the error occured after the single quote in our string. We can avoid this error by using double-quotes to create the string, but what if we don't want to?What if we'd like our string to be single-quotes, and still want to keep the single-quote in it? In Programming, there is always a way, most times more than one way to do a single thing. First, let's use what is called an escape sequence. Make changes to the code. print('I\'m going to the cinema. Would you like to join me?') Now let's run our program again. Works now? Cool abi? What we have used is called an escape sequence. An escape sequence begins with the backslash '\' character. If you confuse the backslash and forward slash characters, just think of the backslash as an I falling backwards, and the forward slash as an I forming Michael Jackson(falling forward). Use the escape character when you want to incude any of these characters in your string ( \,/,'," ![]() The combination of the escape character and any of these characters form what we call an escape sequence. Try the following print('\\') print('\/') print('\'') print("\"" ![]() Now, let's try something more complicated. print(" The doublequote(\"\" ![]() ![]() print('"I\'m not going to the bank", he said to me.') print("The escape character is \'\\\' - also known as the backslash" ![]() What if you want some text in your string to appear on a new line, rather than print on the same line. There's an escape sequence called the new-line character. It forces any text after it onto a new line. print("Hi, I'm line 1.\nAnd I'm line 2.\nAnd I'm\na broken line" ![]() \t represents the tab key on your keyboard. It will push the text after it a fixed width forward. print('\tFirst tab\n\tSecond tab\n\t\tTwo tabs forward\n\t\t\tThree tabs forward') You'd probably see code like these when going through some other books or materials on Python. It's all about manipulating the strings so they are well formatted. The print() function attaches a '\n' to your string by default when you call it. This is why the code below displays on two lines. print('Line 1 will be joined') print('to Line 2') The first print() attaches a new line '\n' to the string. This forces the next print() function to start printing on the next line. This is possible because print function takes another argument called 'end'. This 'end' argument specifies what character should come at the end of a string. By default, the print() uses a new line. We can change this by supplying a value ourself - a string value . We can use any character, from blank space (' '), to symbols(,"+), a combination of more characters, or even none. print('Line 1 will be joined', end=',') print('to Line 2') print('Line 1 will be joined', end='') print('to Line 2') print('Line 1 will be joined', end=' ') print('to Line 2') print('Line 1 will be joined', end='-') print('to Line 2') print('Line 1 will be joined', end='+') print('to Line 2') Think of end as a special variable for the print(). As has been mentioned, strings are quite important in programming. In Python, there's another way to create a string. Let's say you want to write a poem, or send a long message to a friend - basically, any text that might cover a couple of lines or paragraph. You can use the triple-quote. myLongText = """ Good morning my neighbors, Are you doing Okay?? "Yes, we are", they replied. So they danced, and danced, and danced. Press tab key to push text away from left margin. """ print(myLongText) The string will be displayed as you typed it in your text editor. If this isn't powerful enough for you, Python has something even much better. It's called f-strings. F-Strings F-strings are special strings that can contain expressions which can be evaluated when a program is run. An f-string is just a regular string with an f prefixed to it. What makes f-strings powerful is that we can run expressions within the strings. To run expressions, you'd need a pair of curly brackets{}. Now you can insert any expression(whatever creates a value) into this bracket, and it will be evaluated when the program is run. Let's look at an example. cows = 5 pigs = 3 print(f'I have {cows} cows and {pigs} pigs') print(f'I own {cows + pigs} animals') Run this program and see if you can figure out how it works. Here's another you can try. fName = input('What\'s your first name?') lName = input('What\'s your last name?') print(f"Welcome, " ![]() Run it. It doesn't do much, does it? so, let's edit the program so it prints the first name and last name of the user using an f-string. Make changes to the last line. print(f"Welcome, {fName} {lName}" ![]() What's the output? So, as we have seen, programs will deal with string values a lot and there are many ways to handle and manipulate them. Before we move on to our next lesson, let's talk about the len() function. the len() creates a numeric value that represents the number of characters in a string - the length of the string. print(len('Abcde')) print(len('1234567')) print(len(' ')) print(len('')) So far, so good. We learnt a lot today on string manipulation . There's still a lot more to be covered. Let's keep going. |
Re: Learn Python With Kash by Felixnyle: 2:32am On Jul 07, 2020 |
HappyPagan: Had to use another browser before it worked. |
Re: Learn Python With Kash by HappyPagan: 9:58am On Jul 07, 2020 |
Felixnyle:Welcome. |
Help A Beginner Using Python / Greatest Programming Books (Must Have!) / 999 Apps/software Ideas For Developers, Entreprenuer And Programmers
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 169 |