+ Reply Thread
#1
Hello africoders,
This thread is for tutorials on python programming fundamentals.
Hope you enjoy it.
#2
For python installation, head over to this site and download python 3.
Run the installation after it's downloaded.
After these, you should have the Python IDLE in your programs.

Open IDLE.

IDLE:
Python idle has two main windows; The shell window and The Editor Window.
- The shell window is the first window that opens when you open IDLE. This window is used usually for writing and running short codes and serves as output window for codes run in the Editor window.

- The Editor Window is used for writing proper codes, you can save your code using the .py extension, for later use there. Ti open the Editor window press Ctrl+N or click on file and then click new.

#3
Basic calculation with python.
Open your IDLE shell window.
You should see three forward arrow signs like this >>>
After the sign. type some basic math calculations like you would do in a calculator, after you press enter a result should be immediately returned.
Illustration:

>>> 3+3
6

>>> 6-3
3

>>> 6/3
2.0

>>> 6*3
18

So you can see that the symbols(+, *, \, -) function as it should in a calculator.

Now there are other interesting symbols like:
\\: this symbol when used, returns the rounded figure of division made. Known as floor division.
For example:

>>> 7//2
3

Normally with 7/2 should give 3.5, but this round the value to a whole number.
Another example:
>>> 2//5
0

Instead of 0.25, it returns the whole number.
Hope that's clear.


The next symbol is: **
This returns the nth power of a number. that is 2**n.
Example.
>>> 2**4
16

>>> 4**2
16

The Final symbol for now is: %
This returns the remainder of a division.
Example:
>>> 6%2
0

>>> 5%2
1

>>> 11%3
2

And so on...



You can use negation in calculation like;
>>> -3+9
6
#4
Print Function: print()
The print function in python is used to display values to the window. This serves for displaying results of your code.
To use the print function, you type print then put you value or expression within the parentheses.

For example:

>>> print(9+2)
11
#5
Strings:
Now we've seen how numbers are handled in python(atleast for the basic level.) so let's introduce strings;

Strings represents letters, numbers, characters and their combinations in python code.
Strings are contained within two single or double quotes.
For example:

>>> print('Hello World')
Hello World


>>> print("Hello Africoders in 2017.")
Hello Africoders in 2017.


From the simple example above, you can see how strings are represented in python.

However, when using including quote in the string, for now, you should use double quote so it doesn't affect your code.
For example:

>>> print('I'm going')


SyntaxError: invalid syntax
You can see above that the code gives an error, because the quote in the sentence conflicts with the single quote for string representation.

So a better way is to do:

>>> print("I'm going")
I'm going
#6
Variables:
Just like the name implies, variables are used to store values.
You can name your variables any word or letter, but your variable name must not start with a number or characters.

When you store a value to a variable, this is known as variable assignment.

Here's an example for variable assignment:

>>> x = 4+1
>>> number = 10000
>>> word = 'Africoders'

Now just like we did the basic calculations for numbers, we can also do that with variables.
For example:

>>> a = 1
>>> b = 2
>>> print(a+b)
3



Moreso, python variables are mutable which means can be changed.
so if we had:

>>> a = 1

And after that we typed:
>>> a = 100
When we print the value of a we would get the latest assignment.

>>> print(a)
100
#7
String Concatenation:
This means adding two string together.
In python we use to + sign to add two strings together.

For example:

>>> print('Hello' + ' World')
Hello World

We can also do same for variables like:

>>> name = 'dhtml'
>>> site = 'Africoders'
>>> print(name + ' is the owner of ' + site)
dhtml is the owner of Africoders




Numbers and Strings:
We can add two strings together but we can't add a number and string together in python.
The way to go around this is to convert the number to string.
To a number to string, we use the str() funtion, where the number will be within the parenthesis.

For example:

>>> str(2)
'2'

You see that the result returns a string(represented by the quotes)

So to join a string and number, we can do this;

>>> age = 10
>>> site = 'Africoders'
>>> print(site + ' is less than ' + str(age) + ' years.')
Africoders is less than 10 years.




Using Comma(,)
We can also use comma(,) sign within the print statement to join strings, numbers, etc, together.
For example:

>>> print('I am', 19, 'years old.')
I am 19 years old.


You can see that we didn't need to convert our number when joining them together with strings.



Also, notice that i put space within my string when using (+) for separation, but i didn't have to this when using comma(,).
#8
I'm sorry for off-topic, I am thinking about building an informative web site as a student. May possibly commence with submitting interesting information such as"A man will ejaculate approximately 18 quarts of semen in his lifetime."Please let me know if you know where I can find some related info such as right here

http://phoneenemy64.blog2learn.com/8647084/essays-and-its-altering-elements-of-importance-with-modernization
#9
@Jamessuemo you can check Brainy Quotes

@HaroldExolo you can check Articles Factory or better still set up sheme to pay writers for creative contents like most websites do.
#10
User Input: input()
In computer programming, the usual part is to get users' input to work on.
Python's function for getting users' input is input()

If you type input() in the IDLE shell, you would see the cursor blinking for you type/input something and when you press enter your input would be returned.

Now with our understanding of variables, we can store this user inputs for use.
For example:
name = input()
When you press enter you should see the cursor blinking for you to type something, type anything and press enter and what you typed would be stored in the variable "name" that we assigned.

To make our user input more appealing, we can put a declaration for the user to see when inputing. And we put this as strings within the parenthesis of the input function.
For example:
name = input("Enter your name: ")
Now when you press enter, you can see the declaration to alert the user of what to enter.
Then you can do something like: print("Your name is" + name)


Note that the input the user makes are automatically converted to strings, even if they enter a number.
Later on we would use this input() function more appropriately.
#11
Python Data Types:
Data types are basically used to classify groups of data in coding.
Python has 7 "standard" datatypes and there are:

1) Integer- int(): We've seen int already, this are basically whole numbers; from and combinations 0-9. E.g 10, 6, 1000.
And there are denaoted as int in python as we've seen already.

2) Floats - float():Floats are basically decimal numbers; that is numbers with floating points like 1.2, 3.05 and so on. And are denoted as float in python.

3) Strings - str():We've discussed strings already, and they're denoted as str in python.

Now we shall take the others individually and there are:
4) Lists - list():

5) Tuples - tuple():

6) Sets - set():

7) Dictionary - dict():
#12
Hello 4kings, your boy from Nairaland is here with you. I asked you notify me when you start your tutorial here and you did. Thanks.


I may have missed something in this tutorial where you said;
Quote:
So to join a string and number, we can do this;
>>>age = 10
>>>stie = 'Africoders'
SyntaxError: invalid syntax
>>>print(site + ' is less than' + age ' years.')

First correct the name of the variable 'stie' or change the name you used in print function.

Secondly, why did stie throw syntax error?

Good work. Am with you.
#13
#14
So far so good. nice ride. Just some little typing mistakes.
#15
Festus Thanks. :)
I'll proof read the write-up again, i've been very busy that's the reason for the delay in the tutorial.
#16
...
#17
Quote:
Hello 4kings, your boy from Nairaland is here with you. I asked you notify me when you start your tutorial here and you did. Thanks.

I may have missed something in this tutorial where you said;

First correct the name of the variable 'stie' or change the name you used in print function.

Secondly, why did stie throw syntax error?

Good work. Am with you.
Ooops, Just seeing this post. (;一_一)
Na wa o, dhtml should build notification capabilities in his next upgrade.

Thank you very much for the correction. :)
#18
Lists
List function in python is denoted as [].
This square brackets contains all elements of the list, separated with comma(,).
For example:

>>> mylist = [ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']

That's an example of list assignment.

To get length or Number of items in a list.

>>> len(mylist)
5

I haven't discussed this, but len() function can also be used to get the length of a string too.

List Indexing
We can index position of elements of a list in python.
Python's indexing start from 0.
Therefore index 0 in the list created above is 'Lagos'
Illustration:

>>> mylist[0]
'Lagos'
>>> mylist[1]
'Ogun'

So we've seen how list are indexed.
More to indeixng, we can also index a list from last position without counting the number of elements by using -1.
Example:

>>> mylist[-1]
'Zamfara'
Therefore:
>>> mylist[-2]
'AkwaIbom'

List Slicing
The concept behind list slicing is to cut out and return part of a list.
Using [start:stop] method, where start is the value to index from and stop is the value to stop at.
For example:

>>> mylist = [ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[0:3]


Notice that index 3 is 'AkwaIbom' but the return didn't include this.
The concept behind that is that python start slicing your list from your first index and stop just before your last index. In this case our first and last index is 0 and 3 respectively.

Also we can add an increment parameter to the list to increment how elements in the list are to be sliced
So we use [start:stop:increment]

>>> mylist[0:-1:2]

So we can see the list is meant to start at index 0 and stop at the last index[-1] but we added an increment parameter which is to increase the slice of element by 2.
Therefore, when it starts at index 0, it goes to the second element, and does same for the next and so on depending on the length of the list and the stop parameter.

Mutability of List:
This simply means that list element can be changed once created.
For example:

>>> mylist =[ 'Lagos', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[0] = 'Borno'
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']

We can see above how we changed the first element of the list from 'Lagos' to 'Borno'

We can also change element slices of the list at the same time.
For example:

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist[1:3] = 'Jos', 'Kwara'
>>> mylist
[ 'Borno', 'Jos', 'Kwara', 'AkwaIbom', 'Zamfara']


List elements can be of multiple data types.
For example:

>>> mylist = [1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]
>>> print(mylist)
[1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]

We can also see that our list can have a sub-list too.

Now let's look at some List functions:
min
This function returns the minimum value of elements in the list. And is of this format==> min(list)
Example:

>>> mylist = [300,699,2,91,419, 1]
>>> min(mylist)
1

>>> mylist2 = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> min(mylist2)
'AkwaIbom'

We can also see that for string, min finds the minimum alphabetically.

However we can't use min for different data types.
For example:

>>> mylist = [1000, 'Nigeria', 9.11, ['Dhtml', 'Festus', 'Seun']]
>>> min(mylist)


Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
min(mylist)
TypeError: unorderable types: str() < int()


We would discuss errors later on in this tutorial.


append
This is another function of python's list. It's function is to add item to an already existing python list.
For example:

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara']
>>> mylist.append('Kogi')
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi']



extend
Extend function is similar to append, but extend adds a list or an iterable item to an existing list.

>>> mylist = [ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi']
>>> east = [ 'Ebonyi', 'Anambra', 'Abia']
>>> mylist.extend(east)
>>> print(mylist)
[ 'Borno', 'Ogun', 'Rivers', 'AkwaIbom', 'Zamfara', 'Kogi', 'Ebonyi', 'Anambra', 'Abia']



sort
As the name implies, this sorts an element in a list.
For example:

>>> mylist = [300,699,2,91,419, 1]
>>> mylist.sort()
>>> print(mylist)
[1, 2, 91, 300, 419, 699]


reverse
This reverse items in a list.
Example:

>>> mylist = [300,699,2,91,419, 1]
>>> mylist.reverse()
>>> print(mylist)
[1, 419, 91, 2, 699, 300]



count
This function counts number of times an item appears in a list.

>>> mylist = [1,2,4,5,1,5,2,4,5,8,3,]
>>> mylist.count(1)
2



These are the functions i commonly use, i'll address more(if necessary) as we go on.

#19
You're welcome boss.

Notification, mention and user profile need serious attention.
#20
You're good boss.
I love your work.


I am hoping I will one day become smart and learned enough to serve your likes and programming needs of the greater populace.
Back to top