Python tutorials for beginners : Part - II

in #utopian-io8 years ago (edited)

What Will I Learn?

  • Loops in Python
  • Lists and Tuples
  • Sets

Requirements and Difficulty
Requirements and difficulty are same as Part I. I will leave a link to previous tutorials at the bottom of this post.

Tutorial Contents

Lists:
A list is the ordered collection of any data types of python. It is also a data type. It is enclosed in square brackets '[]' and values are separated by a comma. Indexing in list also starts with zero. List can contain another list inside it.

Some of the inbuilt functions of lists are as follows:
list.append(item) adds an item to the list.
list.count(item) counts the occurrence of given item in the list.
list.remove(item) removes the given item from the list.

info = ['programminghub', 'steemit' , 2, 'posts', 'python']
info.append('useless')
print(info)
info.remove('useless')
print(info)
print(info.count('posts'))
info1 = [['programminghub', 'steemit'] ,[ 2, 'posts', 'python']]
print(info1[0])

Output:

['programminghub', 'steemit', 2, 'posts', 'python', 'useless']
['programminghub', 'steemit', 2, 'posts', 'python']
1
['programminghub', 'steemit']

Tuples:
Tuples are similar to lists. The differnce between tuples and lists are as follows:

ListsTuples
enclosed in []enclosed in ()
mutableimmutable
orderedstructured
usually homogeneoususually heterogeneous
variable lengthfixed length
can be changedcannot be changed

We can perform operations that we did with lists to tuples but the operations that changes or modifies tuples will raise error while executing.

info = ('programminghub', 'steemit' , 2, 'posts', 'python')
print(info.count('posts'))
info1 = (('programminghub', 'steemit') ,( 2, 'posts', 'python'))
print(info1[0])

Output:

1
('programminghub', 'steemit')

But when we try to modify it's values then error occurs.

info = ('programminghub', 'steemit' , 2, 'posts', 'python')
info.append('useless')
print(info)
info.remove('useless')
print(info)

Output Error:

 in <module>
    info.append('useless')
AttributeError: 'tuple' object has no attribute 'append'

Sets:
Sets are the unordered collections of unique elements. Elements in sets are not repeated. It supports mathematical set operations such as intersection, union, difference, and symmetric difference. The sets are enclosed in curly brackets'{}' or also we can use set() to create it.

x = {'a', 'a', 'b', 'c', 'd', 'e', 'f', 'f', 'f', 'g', 'h'}
print("Sets after removing repeated elements:")
print(x)
y = set("ghijkl")
print(y)
print(x-y)
print(x | y)
print(x ^ y)

Output:

Sets after removing repeated elements:
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'h'])
set(['g', 'i', 'h', 'k', 'j', 'l'])
set(['a', 'c', 'b', 'e', 'd', 'f'])
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'l'])
set(['a', 'c', 'b', 'e', 'd', 'f', 'i', 'k', 'j', 'l'])

Loops:
While writing programs there may come a situation where we need to run a block of statement number of times. Loops allow us to execute that block of statement multiple times. The loop statement executes the block of code until the condition is fulfilled. The types of loops in python are: while loop, for loop and nested loops. the three loop control statements in python are: break, continue and pass.
while loop: It executes while given condition is TRUE.
for loop: It iterates over and over until the condition is met.
nested loops: It is one loop inside another loop.
break: It terminates the loop and executes the following statement after the loop.
continue: It continues with the next iteration of the loop.
pass: It does nothing. We need pass when the program demands it syntactically but we don't need to execute anything.

#while loop example:
num = 0
while True:
    print(num)
    num += 1
    if num >= 4:
        break

In above code, while loop prints the num which is increased by 1 until the num is less than or equal to five. After that break terminates the loop.

Output:

0
1
2
3
#for loop example
for x in range(5):
    if x % 2 == 0:
        continue
    print(x)

Above code will check the numbers up to range of five whose remainder will be zero when devided by two and continue makes to skip that and remaining number will be printed.
Output:

1
3

For more details visit Python Docs
Above codes can be found on my github link.

Curriculum
Link of Part I



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Can you add some comments to your code snippets explaining what they do (since it's for beginners) or format it differently. For example, this is better in my opinion:

info = ['programminghub', 'steemit' , 2, 'posts', 'python']
info.append('useless')
>>> ['programminghub', 'steemit' , 2, 'posts', 'python', 'useless']

Dear @amosbastian I guess the format I used is not difficult to understand for anyone. As I already mentioned I am using Pycharm IDE to run my code, this tutorial code format is exactly how it works in Pycharm. I explained the code in the tutorials. Should I further edit this tutorial? Or Will I add more comments in code from next tutorials?

In the future please do it, since I think your tutorial is very simple in the first place, so you need to explain it thoroughly to actually make it a valuable contribution to Utopian.io.

@amosbastian Yeah, I am going to make tutorials from begginers to advance level and wanted to cover various topics on python.Thus , I am starting from very begining. It will advance in every tutorials in coming days.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @programminghub I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.31
JST 0.073
BTC 63719.64
ETH 1673.22
USDT 1.00
SBD 0.42