Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,783 members, 7,802,426 topics. Date: Friday, 19 April 2024 at 02:14 PM

Learn Python- Introduction To Programming - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Learn Python- Introduction To Programming (9480 Views)

My Journey To Programming / Are You New To Programming? Want To Learn But Do Not Know How? Come In! (2) (3) (4)

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

Re: Learn Python- Introduction To Programming by stealthtiger(m): 3:36pm On May 14, 2017
phililp:
pls someone help... whats the difference between mutable and immutable in python..


a detailed explanation will be highly appreciated

thanks
Immutable: the value stored in a variable can't be changed.
Mutable: the value stored in the variable can be changed.

Strings, dictionaries, tuples are immutable.
Lists are mutable.

We'd talk about them explicitly as we progress

1 Like

Re: Learn Python- Introduction To Programming by stealthtiger(m): 4:42pm On May 14, 2017
.
Re: Learn Python- Introduction To Programming by stealthtiger(m): 4:43pm On May 14, 2017
.
Re: Learn Python- Introduction To Programming by stealthtiger(m): 4:44pm On May 14, 2017
4. Loops
When you try to perform a task more than once, Python has the capability of handling that task.
For example of you were to open about 10 files, then do some thing to them , then close. It would be easy for you to perform the task without a loop. But of you were given 100 000 files , now this would take you about 10 years to do it manually. So computers are very capable of performing a task severally and Python has that ability.

The first loop we'd be talking about is the WHILE LOOP.

We basically perform repetitions using loops.
In programming context it's called ITERATION.
That is to say line one does something, then it goes to line 2, line 2 them says the program shouldn't terminate then it returns to line1 again and performs that task.
So that circuit of movement is called an ITERATION.

Let's get to coding

count = 0
While count <=4:
print(count)
count += 1

0
1
2
3
4

Explanation of the above code
To use a while loop you have to initialize a counter. We intialized our counter to 0 so our loops would start from 0.
Our counter is count.
The counter also acts as an accumulator.
As the program makes on iteration 1 is added to count, which then stores it.
On the second iteration another 1 is added to account until it makes 5 iterations. So count also gets updated after an iteration.

Beware: when using a while loop always make sure the loop terminates unless it would keep on running till it overflows the stack.
This kind of loop is called an infinite loop. For example

i = 0
While i == 0:
print("infinite loop. This loop would continue to run" )

This loop would run without stopping because there's no condition that terminates this program.


def sequence (n):
while n != 1:
print(n)
if n % 2 == 0:
n = n/2
else:
n = n*3+1

Explanation:
We created a function called sequence with a ln argument n.

Until the value n is equal to 1 before it terminates. Since we said while n is not equal to one.
So if it's contrary it should stop.
If the n is even it is divided by 2 if it's false it is replaced by n*3+1.
If you pass 10 to the argument n the result would be: 3, 10, 5, 16, 8, 4, 2, 1.
When it gets to one the loop terminates.

But there's something really interesting and fascinating about this loop.
Actually there are 2 things that are interesting but the second is more.
1. This is an indefinite loop but it doesn't overflow the stack. The computation keeps on occuring until the condition is met.
There's no even any obvious proof that any would reach 1.
The number of iterations are unknown to us but we know it would terminate due to the expressions in the if and else statements.

2. It's impossible to prove that this program terminates for all positive values of n.
No one has been able to prove this.

You can go read about the collatz conjecture.

This code was gotten from Allen Downey's think Python textbook. A recommended one for you all.

2 Likes

Re: Learn Python- Introduction To Programming by stealthtiger(m): 4:50pm On May 14, 2017
.
Re: Learn Python- Introduction To Programming by stealthtiger(m): 4:51pm On May 14, 2017
NB: I'm also a learner in Python so all criticisms are welcome.At least it should be fair minded, objective and be borne out of a heart that seeks to make positive changes.

So if you see me asking questions in Python don't be amused because I'm also learning.

2 Likes

Re: Learn Python- Introduction To Programming by niyihawking(m): 4:17pm On May 15, 2017
hello @stealthtinger I'm also a unilag student currently learn python, mind if I can meet with you in person?
if so, please send your number to ajayiolaniyi@gmail.com.
thanks



cc: stealthtinger
Re: Learn Python- Introduction To Programming by adejumoadeoluwa(m): 4:40pm On May 15, 2017
if you are interested in joining a Python Fellowship. then .... https:///KTkRJuHrxs36Zq5r5u6DAW

1 Like

Re: Learn Python- Introduction To Programming by niyihawking(m): 8:03pm On May 15, 2017
adejumoadeoluwa:
if you are interested in joining a Python Fellowship. then .... https:///KTkRJuHrxs36Zq5r5u6DAW
your link is not working
Re: Learn Python- Introduction To Programming by Nobody: 10:18am On May 16, 2017
Python 3.(4).3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

if i run it will telling me invalid syntax alighting the 4 with red.

please family wat is the solution to this.
Re: Learn Python- Introduction To Programming by stealthtiger(m): 10:19pm On May 16, 2017
opaniyi12:
Python 3.(4).3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

if i run it will telling me invalid syntax alighting the 4 with red.

please family wat is the solution to this.
How many bits operating system are you using
Re: Learn Python- Introduction To Programming by Chrissammy(m): 4:07pm On May 17, 2017
i love your tutorial

1 Like

Re: Learn Python- Introduction To Programming by Nobody: 12:45pm On May 21, 2017
Bro.. I'm a Unilag student too.. Can i meet u face to face for lectures?
Re: Learn Python- Introduction To Programming by Nobody: 12:36am On May 22, 2017
stealthtiger:
.
bro.. I tried installing python and this is what it's saying

Re: Learn Python- Introduction To Programming by stealthtiger(m): 9:19am On May 27, 2017
samyomz:
bro.. I tried installing python and this is what it's saying
I haven't come across this problem.
I think you should ask about this in programming forums.
Or just a Google search.
Hope you get it working to explore to beautiful world of Python
Re: Learn Python- Introduction To Programming by Nobody: 9:25am On May 27, 2017
stealthtiger:

I haven't come across this problem.
I think you should ask about this in programming forums.
Or just a Google search.
Hope you get it working to explore to beautiful world of Python
I've already installed it bro.... Starting to learn it.. Although it's a lil bit confusing

1 Like

Re: Learn Python- Introduction To Programming by stealthtiger(m): 9:29am On May 27, 2017
samyomz:
I've already installed it bro.... Starting to learn it.. Although it's a lil bit confusing
Good bro.
Feel free to ask questions any where including nairaland there are smart Python programmers here
Re: Learn Python- Introduction To Programming by Nobody: 9:30am On May 27, 2017
stealthtiger:

Good bro.
Feel free to ask questions any where including nairaland there are smart Python programmers here
Aii. .. Thanks
Re: Learn Python- Introduction To Programming by stealthtiger(m): 11:35am On May 27, 2017
kechux:
Nice. Also look at this complete Phyton Trainning outline:

-Web Programming
-Network Programming
-Soft Development
-Scientific Computing
-GUI Library
-Excretion.

Learn Phyton with 100% practicals!
Checkout my signature for contact details!
Nice training outline.
But it'd be better if you create a seperate thread to publicise it
Re: Learn Python- Introduction To Programming by Nastydroid(m): 6:13pm On May 28, 2017
*spreads mat*
Re: Learn Python- Introduction To Programming by samijay8(m): 4:51pm On May 31, 2017
Bro. abeg continue this thing

1 Like

Re: Learn Python- Introduction To Programming by stealthtiger(m): 7:46am On Jun 02, 2017
My sincere apologies for not being consistent with the tutorials. It was as a result of some circumstantial factors
Continuation of While loop
BREAK
There are sometimes when you'd want to end the loop after a certain condition has been met. The break statement is the perfect remedy for that.


count = 0
while count != 20:
print(count)
count +=1
if count == 5:
print("Hold up" )
break

Result:
0
1
2
3
4
Hold up

Explanation: the while says it'd keep running until count is equal to 20. But there's an if statement nested in the while loop which says it should BREAK when count is equal to 9 then print Hold up.
The break terminates the code prematurely.

There's also the CONTINUE statement.
Unlike the break statement, the continue performs an action for a condition and continues from where it stopped.

We'd make use of the example in the break statement

count = 0
while count != 20:
print(count)
count +=1
if count == 2:
print("I am 2" )
continue
if count == 6:
print("I am 6" )
continue
break
print("I didn't get to 20" )

Result:
0
1
I am 2
3
4
5
I am 6
I didn't get to 20


Explanation:
In this example we made use of the continue extensively.
We created the while loop and gave it an intializer 'count' which also acted as a variable.
The count was intialized to zero. ie. The loop will start from zero. We told the loop to progress by adding one to count.

When the loop is not equal to 20, we told it to keep running.
We then nested two if statements. In the first if: if count is equal to 2 the loop wouldn't print 2 instead it would print I am 2, then it would continue the loop into the next iteration (this is as a result of the continue. Unlike the break once it gets to 2 it automatically terminates but in continue it executes the statement and moves on).
When it gets to the break statement it terminates the loop prematurely.

FOR LOOP:

The for loop is used to iterate over a know range of something. Eg a list, a range of numbers. What the for loop iterates over is mostly definite.

1 Like

Re: Learn Python- Introduction To Programming by Muhammedbashir(m): 12:18pm On Jun 02, 2017
stealthtiger:
My sincere apologies for not being consistent with the tutorials. It was as a result of some circumstantial factors
Continuation of While loop
BREAK
There are sometimes when you'd want to end the loop after a certain condition has been met. The break statement is the perfect remedy for that.


count = 0
while count != 20:
print(count)
count +=1
if count == 5:
print("Hold up" )
break

Result:
0
1
2
3
4
Hold up

Explanation: the while says it'd keep running until count is equal to 20. But there's an if statement nested in the while loop which says it should BREAK when count is equal to 9 then print Hold up.
The break terminates the code prematurely.

There's also the CONTINUE statement.
Unlike the break statement, the continue performs an action for a condition and continues from where it stopped.

We'd make use of the example in the break statement

count = 0
while count != 20:
print(count)
count +=1
if count == 2:
print("I am 2" )
continue
if count == 6:
print("I am 6" )
continue
break
print("I didn't get to 20" )

Result:
0
1
I am 2
3
4
5
I am 6
I didn't get to 20


Explanation:
In this example we made use of the continue extensively.
We created the while loop and gave it an intializer 'count' which also acted as a variable.
The count was intialized to zero. ie. The loop will start from zero. We told the loop to progress by adding one to count.

When the loop is not equal to 20, we told it to keep running.
We then nested two if statements. In the first if: if count is equal to 2 the loop wouldn't print 2 instead it would print I am 2, then it would continue the loop into the next iteration (this is as a result of the continue. Unlike the break once it gets to 2 it automatically terminates but in continue it executes the statement and moves on).
When it gets to the break statement it terminates the loop prematurely.

FOR LOOP:

The for loop is used to iterate over a know range of something. Eg a list, a range of numbers. What the for loop iterates over is mostly definite.

Boss, am a beginner in programming in which I need some advice and guideline fro u.

Don't mind if u can drop your WhatsApp/Phone no. Or better still, u can inbox am to my mail @ bashirzubair881@gmail.com
Pls I will be delighted if u can help me out wit this!!!
Re: Learn Python- Introduction To Programming by macaranta(m): 10:46am On Jun 10, 2017
stealthtiger:
My sincere apologies for not being consistent with the tutorials. It was as a result of some circumstantial factors
Continuation of While loop
BREAK
There are sometimes when you'd want to end the loop after a certain condition has been met. The break statement is the perfect remedy for that.


count = 0
while count != 20:
print(count)
count +=1
if count == 5:
print("Hold up" )
break

Result:
0
1
2
3
4
Hold up

Explanation: the while says it'd keep running until count is equal to 20. But there's an if statement nested in the while loop which says it should BREAK when count is equal to 9 then print Hold up.
The break terminates the code prematurely.

There's also the CONTINUE statement.
Unlike the break statement, the continue performs an action for a condition and continues from where it stopped.

We'd make use of the example in the break statement

count = 0
while count != 20:
print(count)
count +=1
if count == 2:
print("I am 2" )
continue
if count == 6:
print("I am 6" )
continue
break
print("I didn't get to 20" )

Result:
0
1
I am 2
3
4
5
I am 6
I didn't get to 20


Explanation:
In this example we made use of the continue extensively.
We created the while loop and gave it an intializer 'count' which also acted as a variable.
The count was intialized to zero. ie. The loop will start from zero. We told the loop to progress by adding one to count.

When the loop is not equal to 20, we told it to keep running.
We then nested two if statements. In the first if: if count is equal to 2 the loop wouldn't print 2 instead it would print I am 2, then it would continue the loop into the next iteration (this is as a result of the continue. Unlike the break once it gets to 2 it automatically terminates but in continue it executes the statement and moves on).
When it gets to the break statement it terminates the loop prematurely.

FOR LOOP:

The for loop is used to iterate over a know range of something. Eg a list, a range of numbers. What the for loop iterates over is mostly definite.
Hi man, can we discuss a project on whatsapp?? Message me 08151097447
Re: Learn Python- Introduction To Programming by Nobody: 2:09pm On Aug 20, 2017
We are still sitted..waiting for the class to continue sir..Thank you for impacting us with knowledge..more power to your elbow...
Re: Learn Python- Introduction To Programming by classicdude1(m): 8:46am On Aug 21, 2017
I also prepared some awesome Python tutorials at www.coolpythoncodes.com

1 Like

Re: Learn Python- Introduction To Programming by Southboy(m): 12:44pm On Aug 29, 2017
stealthtiger:
4. Loops
When you try to perform a task more than once, Python has the capability of handling that task.
For example of you were to open about 10 files, then do some thing to them , then close. It would be easy for you to perform the task without a loop. But of you were given 100 000 files , now this would take you about 10 years to do it manually. So computers are very capable of performing a task severally and Python has that ability.

The first loop we'd be talking about is the WHILE LOOP.

We basically perform repetitions using loops.
In programming context it's called ITERATION.
That is to say line one does something, then it goes to line 2, line 2 them says the program shouldn't terminate then it returns to line1 again and performs that task.

So that circuit of movement is called an ITERATION.

Let's get to coding

count = 0
While count <=4:
print(count)
count += 1

0
1
2
3
4

Explanation of the above code
To use a while loop you have to initialize a counter. We intialized our counter to 0 so our loops would start from 0.
Our counter is count.
The counter also acts as an accumulator.
As the program makes on iteration 1 is added to count, which then stores it.
On the second iteration another 1 is added to account until it makes 5 iterations. So count also gets updated after an iteration.

Beware: when using a while loop always make sure the loop terminates unless it would keep on running till it overflows the stack.
This kind of loop is called an infinite loop. For example

i = 0
While i == 0:
print("infinite loop. This loop would continue to run" )

This loop would run without stopping because there's no condition that terminates this program.


def sequence (n):
while n != 1:
print(n)
if n % 2 == 0:
n = n/2
else:
n = n*3+1

Explanation:
We created a function called sequence with a ln argument n.

Until the value n is equal to 1 before it terminates. Since we said while n is not equal to one.
So if it's contrary it should stop.
If the n is even it is divided by 2 if it's false it is replaced by n*3+1.
If you pass 10 to the argument n the result would be: 3, 10, 5, 16, 8, 4, 2, 1.
When it gets to one the loop terminates.

But there's something really interesting and fascinating about this loop.
Actually there are 2 things that are interesting but the second is more.
1. This is an indefinite loop but it doesn't overflow the stack. The computation keeps on occuring until the condition is met.
There's no even any obvious proof that any would reach 1.
The number of iterations are unknown to us but we know it would terminate due to the expressions in the if and else statements.

2. It's impossible to prove that this program terminates for all positive values of n.
No one has been able to prove this.

You can go read about the collatz conjecture.

This code was gotten from Allen Downey's think Python textbook. A recommended one for you all.
I really don't understand how passing 10 to the argument will give that result. when i ran the code, it keeps printing 10.
As i see it, we told it to print n while n != 1,
Re: Learn Python- Introduction To Programming by Nobody: 7:16am On Aug 30, 2017
samyomz:
bro.. I tried installing python and this is what it's saying

install miniconda or anaconda.

please don't waste your time on python if you can't read html.
Re: Learn Python- Introduction To Programming by Nobody: 8:24am On Aug 30, 2017
TheCabal:


install miniconda or anaconda.

please don't waste your time on python if you can't read html.
so before i can know python, i have to know html?
Re: Learn Python- Introduction To Programming by Nastydroid(m): 12:15pm On Aug 30, 2017
samyomz:
so before i can know python, i have to know html?
he is trolling on you bro.

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

How Do I Edit Datagrid ? / Using Finger Print Scanner On PHP / Is Anything Better Than The Netbeans Ide?

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