advertise on nct forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Awesome Python Tricks A Beginner Should Know
#1
[Image: kM33ZaTQWSrb1QLymej9]Trick #1
Reversing a string in Python


Code:
>>> a =  "codementor"
>>> print "Reverse is",a[::-1]
Reverse is rotnemedoc


Trick #2
Transposing a Matrix


Code:
>>> mat = [[1, 2, 3], [4, 5, 6]]
>>> zip(*mat)
[(1, 4), (2, 5), (3, 6)]


Trick #3
a = [1,2,3]
Store all three values of the list in 3 new variables


Code:
>>> a = [1, 2, 3]
>>> x, y, z = a
>>> x
1
>>> y
2
>>> z
3


Trick #4
a = ["Code", "mentor", "Python", "Developer"] 
Create a single string from all the elements in list above.


Code:
>>> print " ".join(a)
Code mentor Python Developer


Trick #5
list1 = ['a', 'b', 'c', 'd']
list2 = ['p', 'q', 'r', 's']
Write a Python code to print
ap
bq
cr
ds


Code:
>>> for x, y in zip(list1,list2):
...    print x, y
...
a p
b q
c r
d s


Trick #6
Swap two numbers with one line of code.


Code:
>>> a=7
>>> b=5
>>> b, a =a, b
>>> a
5
>>> b
7


Trick #7
print "codecodecodecode mentormentormentormentormentor" without using loops


Code:
>>> print "code"*4+' '+"mentor"*5
codecodecodecode mentormentormentormentormentor


Trick #8
a = [[1, 2], [3, 4], [5, 6]]
Convert it to a single list without using any loops.
Output:- [1, 2, 3, 4, 5, 6]


Code:
>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]


Trick #9
Checking if two words are anagrams


Code:
def is_anagram(word1, word2):
   """Checks whether the words are anagrams.
   word1: string
   word2: string
   returns: boolean
   """


Complete the above method to find if two words are anagrams.


Code:
from collections import Counter
def is_anagram(str1, str2):
    return Counter(str1) == Counter(str2)
>>> is_anagram('abcd','dbca')
True
>>> is_anagram('abcd','dbaa')
False


Trick #10.
Take a string input.
For example "1 2 3 4" and return [1, 2, 3, 4]
Remember list being returned has integers in it. Don't use more than one line of code.


Code:
>>> result = map(lambda x:int(x) ,raw_input().split())
1 2 3 4
>>> result
[1, 2, 3, 4]

I am a proud member of NIgerian Coders Team
#2
Its indeed awesome.
I am a proud member of NIgerian Coders Team
Reply
#3
Yeah, Python is awesome and easy to learn.
Reply
#4
Can you pls share some tutorials on python?
It takes so long to learn simplicity.
Reply
#5
I don't like python. To me the aim of py is just that everybody should be able to program. The programming language is neither hard to learn or too easy to learn. Py is just like a mix of JAVASCRIPT AND PHP... When you see a programmer programming in c/c++, Java or Javascript give him kudos and not a programmer programming in py. Like I said py was just made for Novice programmers but it is very efficient don't quote me wrong, Infact great softwares are written in py, such as BLENDER AND INKSCAPE... and hey that dosen't mean I can't code in py.. I just always try to avoid it.. :-)
I am a proud member of NIgerian Coders Team
Reply
#6
(12-03-2016, 08:53 AM)Ahkohd Wrote:
I don't like python. To me the aim of py is just that everybody should be able to program. The programming language is neither hard to learn or to easy to know. Py is just like a mix of JAVASCRIPT AND PHP... When you see a programmer programming in c/c++, Java or Javascript give him kudos and not a programmer programming in py. Like I said py was just made for Novice programmers but it is very efficient don't quote me wrong, Infact greate softwares are written in py, such as BLENDER AND INKSCAPE.


What????!!!!!!! Python is a very efficient programming language that uses an easy to understood syntax(That alone makes it easy for anyone to use it). a programmer coding in Python deserves Kudos too! its not an easy task to apply logical reasoning into solving a problem with any programming language.
It takes so long to learn simplicity.
Reply
#7
(12-03-2016, 08:58 AM)Dealwap Wrote:
What????!!!!!!! Python is a very efficient programming language that uses an easy to understood syntax(That alone makes it easy for anyone to use it). a programmer coding in Python deserves Kudos too! its not an easy task to apply logical reasoning into solving a problem with any programming language.


I agree with you and that was why I wrote that great opensource softwares are written in py.. but when ever I code in python I feel unease... I love rocket programming languages (pl that are hard to learn) because they always dare me... NICE POST.
I am a proud member of NIgerian Coders Team
Reply
#8
(12-03-2016, 08:53 AM)Ahkohd Wrote:
I don't like python. To me the aim of py is just that everybody should be able to program. The programming language is neither hard to learn or too easy to learn. Py is just like a mix of JAVASCRIPT AND PHP... When you see a programmer programming in c/c++, Java or Javascript give him kudos and not a programmer programming in py. Like I said py was just made for Novice programmers but it is very efficient don't quote me wrong, Infact great softwares are written in py, such as BLENDER AND INKSCAPE... and hey that dosen't mean I can't code in py.. I just always try to avoid it.. :-)


Well, that is in your own perspective. What I understand about Programming is making life easier for every computer users, it doesn't mean if the programming language is easier or difficult, what matters most is how it makes work easier for computer users.
Reply
#9
(12-04-2016, 11:00 AM)Lekxel Wrote:
Well, that is in your own perspective. What I understand about Programming is making life easier for every computer users, it doesn't mean if the programming language is easier or difficult, what matters most is how it makes work easier for computer users.


Correct, that's right..
I am a proud member of NIgerian Coders Team
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  FUN: Rhombus With Python Mbabalawlar 0 35 01-06-2018, 10:00 PM
Last Post: Mbabalawlar
  FUN: Love Calculator With Python Mbabalawlar 0 66 01-06-2018, 09:56 PM
Last Post: Mbabalawlar
  TUTORIAL: My First Project Quiz In Python Programming Mbabalawlar 0 62 01-06-2018, 09:50 PM
Last Post: Mbabalawlar
  SHARED: Why Learn Python? cheatex 5 404 04-27-2017, 08:54 PM
Last Post: Horlahcoded
  Creating A Simple Random Username And Password Generator With Python Jolliano 4 581 01-01-2017, 10:58 PM
Last Post: Jolliano
  Python Program To Solve Quadratic Equation Dealwap 4 599 12-10-2016, 08:43 AM
Last Post: Dealwap
  How To Validate User's Input In Python Ahkohd 1 278 12-03-2016, 12:39 PM
Last Post: Dealwap
  Check If A Variable Is Defined Or Not In Python Ahkohd 0 228 12-03-2016, 09:42 AM
Last Post: Ahkohd
  Why You Should Learn Python As Your First Programming Language Dealwap 0 315 11-04-2016, 07:37 AM
Last Post: Dealwap
  Learn Python From Scratch Here On Nct Dealwap 8 704 11-02-2016, 04:50 PM
Last Post: Sakpobi Maro



Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links

              Advertise