Roshan Book

My Tech Notebook

Python tutorial 3 Starter Python in one post. Syntaxes


# This is comment
# ! path to python intepreter
print(“Hello World!”);

# Conditionals with format method of strings

# If-else

a,b =0 ,1;
if a<b:
print(‘a({}) is less than b({})’.format(a, b)) # value of a and b is inserted at the place of curly braces
else:
print(‘a({}) is more than b({})’.format(a, b))

# Conditional expression

print(“foo” if a<b else “bar”)

another example

v= ‘this is true’ if a>b else ‘this is not true’

# loops
# While Loop
a,b =0,1
while b<50: # while and then condition
print(b)
a,b=b,a+b
print(“Done”)

# For loop
a=(1,2,3,4)
for b in a:
print(b+1)

# Functions
def myfunc(a): # Defining a function
b=a+5;
return b ;
print(myfunc(4));

# Generator function
# Function which geenrates iterators

def isprime(n):
if n == 1:
return False
for x in range(2, n):
if n % x == 0:
return False
else:
return True

def primes(n = 1): # This is a generaor function
while(True):
if isprime(n): yield n
n += 1

for n in primes():
if n > 100: break
print(n)

# Classes and Object
# the sum of two elements defines the next set
class Fibonacci():
def __init__(self, a, b):
self.a = a
self.b = b

def series(self):
while(True):
yield(self.b)
self.a, self.b = self.b, self.a + self.b

f = Fibonacci(0, 1)
for r in f.series():
if r > 100: break
print(r, end=’ ‘)

# Inheritence
# polymorphism

# Exceptions and error handling
try:
write code
except IOError as e:
print (“something bad happened({})”.format(e))
print(“after badness”)

8 responses to “Python tutorial 3 Starter Python in one post. Syntaxes

  1. architektura wnętrz October 16, 2011 at 9:50 pm

    *Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  2. Understand the Australian Accent October 17, 2011 at 8:26 pm

    I need to mention My group is fascinated. Pretty don’t often does an individual knowledge a fabulous site that is certainly equally educative and amusing. Need to show you that there is reach all the nail bed for the start. An individual’s consideration is brilliant. Thx is definitely all of When i really have to claim!

  3. Monte Metos October 22, 2011 at 12:32 pm

    Good post… Just killing some time at work browsing searches and found your site. Great looking page. I will have to save your blog to come back and see what’s new. Cheers! My Blog: Lethalia Blogs

  4. amazing weathervane October 31, 2011 at 6:51 am

    Greetings from Idaho! I’m bored to tears at work so I decided to browse your site on my iphone during lunch break. I love the knowledge you present here and can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyhow, awesome site!

  5. awesome weathervane October 31, 2011 at 12:10 pm

    This design is wicked! You certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  6. Sale Designer Handbags December 5, 2011 at 5:23 pm

    Just wanted to say I love reading through your weblog and look forward to all your posts! Keep up the fantastic work!

  7. Sale Designer Handbags December 6, 2011 at 8:56 am

    Maintain up this superior work, you have a good blog over here with much fantastic information and facts! When you post some new stuff, I’ll visit your weblog again and I’ll follow it.

  8. cheap canada goose|c December 10, 2011 at 8:54 am

    My neighbor so i were simply debating this specific topic, he??s normally on the lookout for to demonstrate me incorrect.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: