Python: Simple codes 1

I  want to add all the digits in a positive integer. I will write a function in python that would automatically do this for me.

Let’s tame the python!

Easy way:

I will tell my function to take a number make it a string, then read each member of the string , make them an integer and finally add them up.

def add_digit(x):
    summation=0
    y=str(x)
    for i in range(len(y)):
        summation+=int(y[i])

    return summation

That will do the job for positive numbers.

Now the more challenging way:

Interesting way:

def digit_sum(x):
    y=x
    summ=0
    while y//10 !=0:
        summ+=y%10
        y=y//10
    summ+=y
    return summ

We are telling python to add the modulo of y divided by 10 (the right most number) to the variable summ.

Then floor divide (//) y by 10 . This will remove the right most number.

The while loop will make sure that this is going to stop at the final number.

 

Better way of doing it? Share in the comments 😀

Enjoy!

PS:

This is an awesome way to do it!

def digit_sum(x):
    if x==0:
        return 0
    else:
        summ=0
        summ=x%10+digit_sum(x//10)
        return summ

Thanks to Homayoun!

 

Night sky

The night sky always excites me. Sometimes it brings a nostalgic moment, where I remember a lot of good memories with my friends, travelling into central deserts of Iran or hiking all the way up to mountain peaks with binoculars and telescopes to gaze at stars and galaxies.

Tonight, I was thinking about people whom lived couple of thousands (perhaps even more) years ago. People whom were lucky enough to stare at a night sky without any light pollution. Then their creativity bird would open its wings and they travelled in the unfathomable universe and listened carefully to the story of the nature (Heavens!).

Look up my friends, what we see is a plain and lackluster darkness with an aeroplane flying every now and then, but below is what they were seeing. Maybe, rather than looking at the shiny screens, we need to rediscover the sky and gaze at the shiny celestial objects.

Scottsdale Sky.
Scottsdale Sky.
 

Poem of the week 1


Poem by Ahmad Shamloo.
Bellow you can find a rough translation.

There is enigma in tears.
There is unfathomable mystery in smile.
And love is a conundrum itself.
And tears of that night were nothing but my love smiling at you!

I’m not a story to tell,
I’m not a song to sing,
I’m not a sound to hear,
neither something to see, nor something to know!
We are sharing the same grief, let’s shout and let it out.

Tree chats with the forest,
Grass mingles with the meadow,
Star communicates with the galaxy
and I’m talking with you.

 

Princess II- The sacred geometry.

And I found happiness in the tress of your hair and peace was nothing but the chaotic, playful looks of your eyes accompanied by your blissful smile and how incredible this harmonic piece was composed by the magic of your hands, princess.

The divinity should be reinterpreted in light of the profoundness of your eyes. The laws of geometry should be rediscovered, since they are baffled by the elegance of the curves that connect your neck to the softness of your body.

I will rewrite the stories. I will rewrite a new Romeo and Juliet where all the lovers will make the Utopia of joy.

O Sweetheart, in what part of the story you have enchanted me with your heavenly scent and what that kiss was, which intoxicated me for eternity (shall I be sober again?)!

I keep contemplating that doubtlessly there should be a poet sitting in a mystical mountain and that poet is in charge of writing my destiny…the destiny which takes me in to your arms… Goodnight Princess.

 

 

Princess – I

Will it scare you that I keep ranting on and on about how I like you in millions of different ways?
May be you keep thinking about the pace of our relationship and you worry about our hearts have the last saying over our minds, but princess I feel like We are beyond these mundane equations.

I am flying high over the coucou’s nest with my eyes wide open, since this adoration has empowered me with wings and it rekindles my everlasting passion for gliding in the limitless sky.

My days are lighten up with sun rays of affection and nothing worries me but the fear of having my wings burnt either by separation or customary. As customary, I believe, is the killer of joy and passion.

Princess, I was supposed to be happier by our frequent meetings, but alas the bitterness of each farewell has dimmed the sparks of joy in my eyes.
Cuddle me and hold me tight princess, because your arms are like an asylum for me against all the roughness and coldness of time.

 

Wisdom of the far east

We have all heard so much about the wisdom of the far east and how many mindful quotes can be found in their sacred ancient books. But as they always say: seeing and believing!

Today I found out that in order to write my name in Mandarin, you need to spell out the word “valuable”!

PS1: Naah! I don’t have a big ego! (#denialphase)
PS2: It also means Shell/ cowries
PS-portable: Happy holidays!

 

In Search of Sarcasm Lost!

Somewhere along the way, I have lost an essential element of my character. It was nothing but a combination of snap comments + sarcasm + humor!

From now on I shall call myself dull! َAnd it’s just harder to get a doll when you are dull!

 It’s like having a gift and unconditionally share it with people for a while. Although gifted, you still need to make an effort to apply satire to different situation, with the right doze, to the right people.

 But there comes a time, when you’d stop being a sun, shining unreservedly and regardless of the audience. When this happens there comes the death of a star. It’s kind of as Heinrich Boll described in his The Clown.

 

I have blocked the hot shinny light beams of the notorious sun with drapes. My roommate is sleeping. I have opened the Rainymood and listening to the pacifying sound of a mellow rain. I light up a candle on my desk and listening to a new song from Leila Forouhar, Toofan (Tempest), and a nice warm cup of Irish tea in hand. Life can be calm for some precious moments 🙂

 

استاد عزیز ما

You guys, young scientists, are like street dogs. We will train you how to fight, but at one point you have to show your own teeth and go out and pick your fight and get your portion!

 

Grrrrrhhh!

 

Quanta of life

The breeze gives high five to all the leaves, and they gently move, with elegance, to contribute to the symphony of the moon light.

The moon is playing peekaboo with the poet… Drenched in his thoughts, laying on the chair, contemplating.

The young poet was not the first hitchhiker in this universe, whom was excogitating the unraveled mystery of love.

O uncertainty, how shall we traverse in the right path with the correct momentum?