Skip to content

Added Python examples #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Python/1-for.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for i in range(10):
print(i)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please format your code according to the PEP8 styleguide? Namely, four spaces should be used instead of tabs.

4 changes: 4 additions & 0 deletions Python/2-while.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a = 0
while a < 10:
print(a)
a += 1
9 changes: 9 additions & 0 deletions Python/3-do-while.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# There is no do-while loop in python
# But we can emulate it

a = 0
while True:
print(a)
a += 1
if not a < 10:
break
14 changes: 14 additions & 0 deletions Python/4-for-in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
dictionary = {'x': 1, 'y': 2, 'z': 3}

for i in dictionary:
print(i)

for i in dictionary:
value = dictionary[i]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not an idiomatic way to iterate through the values of a dictionary. I'd make an example with an array instead of a dictionary.

print(i, value)

for i in dictionary.values():
print(i)

for i in dictionary.items():
print(i)
10 changes: 10 additions & 0 deletions Python/6-break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# There is no break label in python

flag = True

while True:
print("Hello")
if flag:
break

print("There")
9 changes: 9 additions & 0 deletions Python/7-continue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# There is no continue label in python
i = 0

while i < 10:
i += 1
print("Hello")
if i == 5:
continue
print("World")
7 changes: 7 additions & 0 deletions Python/8-forEach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Python doesn't have functional forEach
# We use a for loop instead

dictionary = {'x': 1, 'y': 2, 'z': 3}

for k, v in dictionary.items():
print(k, v)
3 changes: 3 additions & 0 deletions Python/9-map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
map_obj = map(lambda x: x * 2, [7, 10, 1, 5, 2])

print(list(map_obj))