Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,232 members, 7,953,882 topics. Date: Friday, 20 September 2024 at 08:08 AM

Python Tutorial - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Python Tutorial (9452 Views)

My First Python Tutorial Has Been Released. / Please Am Stuck Here. (python Tutorial) (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Python Tutorial by Nobody: 9:25am On Jun 23, 2016
INDEX
LESSON 1: Introduction to Programming
LESSON 2: How Computers Work
LESSON 3: Installing Python
LESSON 4: Python Basics
LESSON 5: Values and Datatypes
LESSON 6: Working with Strings
LESSON 7: Variables
LESSON 8: Functions
LESSON 9: Flow control

LESSON 1
Programming With Python: An Introduction
Your girlfriend wants money for Valentine’s day. Your mom wants a gift too, after all, she is your first lady she says. Your boss has announced that salary will be coming in late. You realize you need to make some financial plans before you go broke. So, you whoop out a calculator and start punching numbers.

A calculator is a tool that aids you as you try to sort out your financial issues. Inside the calculator is a program that runs when you turn it on. You enter values, press the right symbols, and when you’re done, you tap the equal button to get an answer.

The calculator is a simple example of what a computer is and does. Computers are tools we designed that help us improve our productivity. Think about it for a second. What do you use your computer for?

You can create and edit documents with a computer, and print them when you’re done. You can download and listen to the latest music. You can browse the web, send a picture, make video calls. The computer is truly a multipurpose gadget. A really brilliant piece of invention.

Software (you may know these as applications) bring computers to life. If you’re using a smartphone or a PC, you’re able to access the site using a web browser software (Chrome, Mozilla, Safari etc). To play music, use music software like Media player or VLC. Skype is for video calls and so on and so forth.

These are all applications. And you can learn to write one too. Okay, not one as big as Chrome or Facebook, but simple programs that would help you create little software you can use. But first, before you start writing real programs, it’s important you learn how computers work.

3 Likes

Re: Python Tutorial by Nobody: 9:26am On Jun 23, 2016
LESSON 2
Consider these scenarios: The weatherman forecast there’s a high chance of rain tomorrow. The commentator mentioned Mikel Obi has made fewer passes than any other midfielder in the Premiership. The government hopes to perform a census nationwide late next year. What do all these have in common? Data.

Data is information about something. The weatherman’s prediction is based on sample meteorological data taken over some time. A census would give the government information on which it can base its policies to achieve growth and development.

Computers are not intelligent. It’s the programs humans write that are. But computers are fast, blazingly fast. Some computers can run a billion calculations per second. From whence does this juju happen? Let’s dive into computer architecture a bit.

Computers are a combination of hardware and software. Hardware refers to the physical component of the computer. Software is the programs or applications that enable computers to perform various actions.

COMPUTER HARDWARE
The computer hardware consists of different sub-sections/units that work together as one. The diagram below shows a nice layout of these units and how they interact.


Learn to program with Python

THE INPUT AND OUTPUT UNITS
The Input Unit is the ‘receiving’ section of the computer. Your Ipad or smartphone’s touchscreen is an input device. When you tap on a button or menu, the phone receives that ‘interaction’ through the touchscreen. On desktops/laptops, your keyboards, mouse and even a microphone can all be classified as input devices.

The Output Unit is the ‘shipping’ section of the computer. Your Ipad’s touchscreen acts an output device too. When you perform an action like opening a web page, the web page is shown on your device screen. Printers and speakers are also good examples of output devices.

THE CPU
The Central Processing Unit is the brain of the computer, the Oga-pata-pata. Feed it data and it will process it. The CPU is like a zombie – Garbage in, Garbage out. It will process your data as you have instructed it to.

RANDOM ACCESS MEMORY(RAM) AND SECONDARY STORAGE
For the CPU to work, it requires MEMORY. This is where the Memory unit comes in. Memory is classified into two types; Primary memory(RAM) and Secondary Memory(Storage).

Let’s dive into this a little. RAM is faster to access than regular storage memory like your hard disk and pen drives. But RAM gets more expensive as size increases, and it’s unable to store data permanently. This makes RAM volatile – when computer is turned off, data in RAM is lost.

Secondary memory provides permanent storage of data, or what is known in technical terms as ‘persistent storage’. It’s cheaper to produce but it’s not as fast as RAM.

A simple analogy should hopefully explain this better. Let’s say you want to prepar egusi soup. You’d need ingredients like egusi seeds, leaves, beef, palm oil and so on (before belle start to sing). When preparing the dish, you’d place these ingredients on the kitchen table. The kitchen table lets you access these ingredients easily and quickly, rather than having to get them from the fridge or store whenever you require them. The RAM performs a similar function in your computer. Secondary memory is similar to the kitchen fridge. It stores data so that it can be used later.

PUTTING IT ALL TOGETHER
Think of a computer as a tool we use to solve problems. Calculators best illustrate a simple computer. Your calculator has a program stored on it that handles your mathematical calculations and returns a result to you. You enter data(maths problem) using the various buttons. You press ‘=’ to have your answer display on the screen.

WHY YOU SHOULD LEARN PYTHON
If you’re interested in learning computer programming, Python offers an easy path to do so. It’s a programming language that’s not too difficult to master. The secret to becoming a better programmer is same as improving at any other activity :Practice, Practice, Practice

4 Likes

Re: Python Tutorial by CltrAltDelicious(m): 11:58am On Jun 23, 2016
FTC grin
Space booked

1 Like

Re: Python Tutorial by Nobody: 12:41pm On Jun 23, 2016
CltrAltDelicious:
FTC grin
Space booked
wink grin

1 Like

Re: Python Tutorial by Nobody: 4:13pm On Jun 23, 2016
LESSON 3
As humans, we communicate amongst ourselves using natural languages(Ogoni, Ikwerre, Tiv). Each language has its own particular grammar, words and sentence structures that combine to form its unique identity.
Machine language defines how machines communicate.However, machine language can be difficult to read.

Machine languages made developing software tedious and more of an esoteric task. To cut down production time and make programming much easier, high-level programming languages were created.

HIGH LEVEL PROGRAMMING LANGUAGES
High-level Programming Languages were designed to bridge the gap between “easy to understand” code and efficiency. C, Java, C++, Python and a host of other computer languages make it easier for us to write computer programs that are efficient and also easily readable.

print ("101001010"wink


You do not require genius-level IQ to figure out what the above code does. This is the strength of high-level programming languages. They closely resemble the English language, as such they are much easier to write and understand. This greatly saves time and also makes programming much easier

Our computer has to ‘learn’ Python to be able to execute the Python programs we will write. We should install the Python language on our system in order to run and execute our programs.

Go to Python official website. Make sure to select Python 3.5.1 and download the right version for your system.


Alternatively, if you have Windows installed on your computer, you can click here to quickly download to the Python installer to your system.

Next, double-click the setup file to begin the installation. Follow the instructions in the graphics below to continue. Make sure you select the “Add Python 3.5 to Path” checkbox”



Click on “Install Now” to begin setup.



Good news. You’ve installed Python on your system. One Origin for you, my guy!!!



In the next lesson, we’ll start writing basic Python programs.

7 Likes 1 Share

Re: Python Tutorial by owoade25(m): 8:20am On Jun 24, 2016
Make I siddon for middle row
Re: Python Tutorial by 4kings: 3:58am On Jun 25, 2016
when will the next lesson start.
Re: Python Tutorial by Alhajipablo(m): 8:51am On Jun 25, 2016
Ori e pe;I been one learn python teh teh
Re: Python Tutorial by encryptjay(m): 9:04am On Jun 25, 2016
I love this...
Ride on
Re: Python Tutorial by adejumoadeoluwa(m): 11:13am On Jun 25, 2016
Re: Python Tutorial by Nobody: 12:51pm On Jun 25, 2016
LESSON 4
In the previous lesson, we installed Python on our system.The Python Installer we set up includes an Integrated Development Environment(IDLE). IDLE lets you write and run Python code. IDLE provides two different modes in which we can write our programs: a shell window(Shell mode) and a file editor window (Edit Mode).

USING IDLE
To start IDLE, click on the Start Button, type “Python” in the search bar, then click on Python IDLE.


IDLE will always start in Shell Mode. You can identify what mode you’re working in by looking at the top of the window.


The shell prompt “>>>” is where you’ll enter instructions to the system. The Python shell is like a middle man between you and your computer. Give him an instruction and he will pass it on to the computer to perform. But you must speak in Python. Na only dat language im sabi.
Re: Python Tutorial by Nobody: 12:57pm On Jun 25, 2016
Shell Mode allows you to enter only one instruction at a time. A line of instruction is called an expression or statement. In Shell Mode, you press the ENTER key after typing an expression to get its value.

Alright, let’s have some fun. Type the following instructions at the prompt. Press the “Enter” key at the end of each instruction to run it.

print ("Hi, I'm your computer"wink
print ("You are my master"wink
print ("I'll always do as you ask me to"wink




Guess what? Water.. you’ve just written your first Python instructions. We instructed the computer to print a statement and the computer prints the statement(displays it on the screen) when we press Enter. Good job.
Re: Python Tutorial by koyla: 1:14pm On Jun 25, 2016
Nice
Re: Python Tutorial by Nobody: 1:19pm On Jun 25, 2016
WRITING PROGRAMS WITH IDLE
Usually, Shell Mode is used for testing/running small code, not for writing programs.
IDLE provides an edit mode (file editor) where we can write programs. To open the File Editor Mode, click on File in the Menu Bar and select New File.



A new window should appear on your screen similar to this.



You’re now working in the Edit mode or File editor mode.

PYTHON SYNTAX
Our natural languages provide words that are combined to formed sentences. Sentences combine to form paragraphs. Paragraphs combine to form an essay or article.

Similarly, computer programming languages, which come in various flavors (C, JAVA, PYTHON), all have rules that specify how programs are written in that particular language.

This set of rules is known as the syntax of the language. Instructions written in any programming language must obey and follow its syntax in order to run or execute successfully. When a program doesn’t run as we expect or breaks the syntax, we say the program has a bug or an error.

WRITING A PYTHON PROGRAM IN FILE EDITOR MODE
We’re now ready to write our first program. Computer programs are made up of lines of codes. Each line of code is an expression or statement, instructing the computer to do something.

We can combine the three instructions we wrote earlier into a single Python file to create a Python program.



Congratulations, you’ve just written your first program. It contains 3 statements, or lines, each of them instructing the computer to print a particular text. Alright, let’s save and run our program.
SAVING AND EXECUTING A PYTHON PROGRAM
We just wrote source code for a program that prints three statements on the screen. Source code refers to the actual code that defines a program. Save the code by clicking on “File”, then “Save As”.



I’d advise you create a directory where you can store Python programs. Enter a file name and click “Save”.



Python codes are saved in .py files. That’s a good clue if you need to identify a python program on your system. You have successfully saved your first program. Now, let’s run them.

To run your python program, click on Run in the Menu Bar, and click on Run Module. Alternatively, you can press F5 on your keyboard to run the program.




The Python Program will execute in the Shell Window. This time, it runs all three instructions at once as you can see in the output after the first “RESTART”.
Programs that run/execute or give output in Shell Mode are called CLI (Command Line Interface) programs. With time, you’ll learn to create programs that use windows, buttons and other graphical features for interaction. Programs that make use of images, buttons, icons and graphics in general are called GUI (Graphical User Interface) applications.

PUTTING IT ALL TOGETHER
So far, so good, we’ve written our first python program, and also learnt to use IDLE. In the next lesson, we’ll talk about values and datatypes.

1 Like 1 Share

Re: Python Tutorial by Ithee(m): 12:04am On Jun 26, 2016
grabs an ice cold zobo and kpokpof......... Class captain is here
Re: Python Tutorial by Kruzilano(m): 10:21pm On Jun 26, 2016
Am all ears.. Will appreciate if u can help with the ebook
Re: Python Tutorial by wiegraf: 5:37am On Jun 27, 2016
nice brah
abi make I do c# own?
asking folk to download visual studio sha
tho there are likely alternatives
Re: Python Tutorial by Nobody: 1:32pm On Jun 27, 2016
.
Re: Python Tutorial by Nobody: 4:06pm On Jun 27, 2016
DJHANDSOME:
The ( sonOfLucifer ) what a weired name! undecided
You prefer sonofChrist/sonofAllah? Don't wish me such evil pls.

@wiegraf
Fulaman198 is doing an excellent tutorial on Java. Me no like c# sha. You can handle that.

Davidconoh buharifan@gmail.com
Re: Python Tutorial by Nobody: 5:47pm On Jun 27, 2016
Following cool
Re: Python Tutorial by wiegraf: 2:27am On Jun 28, 2016
sonOfLucifer:

You prefer sonofChrist/sonofAllah? Don't wish me such evil pls.

@wiegraf
Fulaman198 is doing an excellent tutorial on Java. Me no like c# sha. You can handle that.

Davidconoh buharifan@gmail.com

java/csharp almost the same

good excuse for me to lazy out of it

csharp sweet oh. many lovely (and elegant) shortcuts.

and no madness, like say javascript

true though, I'll give python it's due. it's quite elegant too
Re: Python Tutorial by Nobody: 7:32am On Jun 28, 2016
wiegraf:

and no madness, like say javascript
men, you sabi. JScript is one fucker of a language, closure, function values, and weird OOP. Team behind JS was high on confusion..
Re: Python Tutorial by Nobody: 7:49am On Jun 28, 2016
wiegraf:


java/csharp almost the same

good excuse for me to lazy out of it

csharp sweet oh. many lovely (and elegant) shortcuts.

and no madness, like say javascript

true though, I'll give python it's due. it's quite elegant too

guy, don't worry. Just use SharpDevelop from www.icsharpcode.net/opensource/sd/ to teach C#.Net programming. I'm with you.
Re: Python Tutorial by jidez007: 8:02am On Jun 28, 2016
sonOfLucifer:

You prefer sonofChrist/sonofAllah? Don't wish me such evil pls.

@wiegraf
Fulaman198 is doing an excellent tutorial on Java. Me no like c# sha. You can handle that.

Davidconoh buharifan@gmail.com
There is no son of Allah, only servantofAllah.
Re: Python Tutorial by Nobody: 8:15am On Jun 28, 2016
jidez007:

There is no son of Allah, only servantofAllah.
Lol, oga learn Python, leave God matter. Morning.
Re: Python Tutorial by Nobody: 8:28am On Jun 28, 2016
.
Re: Python Tutorial by Nobody: 8:10pm On Jun 28, 2016
LESSON 5

Computers were first designed to solve mathematical problems. Let’s say you are asked to find the sum of 3 and 5. Using a calculator, you the ‘3’ button to enter the value ‘3’, then you hit the ‘+’ button to signal you want to perform an addition. You then hit the ‘5’ button to enter the second value ‘5’. When you press ‘=’, the value ‘8’ is displayed as result.

Values identify data that computers process. There are different kinds of values. The values in our calculator example were made up of digits. They are numeric values. Your username and password are alphanumeric values(made up of alphabets, numbers and other characters).

The type of a value is called its datatype. The datatype of a value determines what kind of operations can be performed on it and how it is processed. A fundamental understanding of Python’s datatypes is essential if you hope to become a good programmer. Let’s dive into this a bit. Nothing too technical, trust me

DATATYPES IN PYTHON
Python has five standard data types

Number
String
List
Tuple
Dictionary
Every value you will work with when programming, will fall into one of these categories. We’ll look at the last 3 later on. For now, we will focus on Numbers and Strings.

1 Like

Re: Python Tutorial by Nobody: 8:12pm On Jun 28, 2016
NUMBER DATATYPE AND OPERATORS
The number datatype describes numeric values. These values include numbers like 1, 1000 etc and even decimal numbers like 1000.345. In programming, whole numbers are referred to as integers, and fractional numbers are referred to as floating-point numbers.

The Number datatype allows us to perform mathematical operations on these values. Operators are symbols that tell the computer to perform an operation on a value. Mathematical operators perform mathematical operations on values. You might remember some of these symbols from Maths class. Let’s take a few examples. Open IDLE, and enter the following code.


5 + 3
5 - 3
4 x 3
4 / 3
4 // 3
4 % 3
4**2



Mathematical operators perform operations on values. On the first line, the mathematical operator for addition(+) adds two values 5 and 3 and returns the value 8. You could probably figure out the next two, so I’ll just highlight the DIVISION(/), FLOOR DIVISION(//), MODULO(%) and EXPONENT(**) operators.

The division operator would return a decimal/floating-point value. The floor division operator only truncates the digits after the decimal point and returns a whole number. The Modulo operator returns the remainder when two values are divided. The Exponent operator is used to calculate powers of a number.. For example, our code returns the value of 4 raised to power 2. 2**3 will return 8.

1 Like

Re: Python Tutorial by Nobody: 8:12pm On Jun 28, 2016
PRACTICE
Using the Python Shell, calculate the following, find the results of the following expressions.
– 8 + 5 * 4 – 3.
– 10 raised to power 8.
– The remainder when 6 is divided by 5.
Computers read instructions from left-to-right, line-by-line. The parenthesis symbol “()” when used around a part of an expression, indicates to the computer to evaluate its contents first. To understand this, run the following expressions.
– (8 + 5) * 4 – 3.
– 8 + (5 * 4 – 3).
– 8 + 5 * (4 – 3).
– 8 + (5 * 4) – 3.

1 Like

Re: Python Tutorial by gistsky: 12:46pm On Jun 29, 2016
Pls carry on
Re: Python Tutorial by Nobody: 7:33pm On Jun 29, 2016
gistsky:
Pls carry on
o yeah
Re: Python Tutorial by Nobody: 9:18pm On Jun 29, 2016
dhtml18:

o yeah
angry grin

Will update this weekend.

(1) (2) (3) (Reply)

Android Quiz Application With Json Parser, Php And Mysql Database / Has Anyone Here Worked For Crossover.com As An Independent Contractor? / Sorting List Of Numbers And Strings: Simple Puzzle

(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. 56
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.