Post: My First Python Script
05-11-2011, 06:30 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Im new to Python, just want to show off my first script =D

    def pick_add():
a,b,c=0,0,0
a = raw_input("Enter the first number :")
b = raw_input("Enter the second number :")
c = int(a) + int(b)
print str(a) , " + " , str(b) , " = " , str(c)

def pick_sub():
a,b,c=0,0,0
a = raw_input("Enter the first number :")
b = raw_input("Enter the second number :")
c = int(a) - int(b)
print str(a) , " - " , str(b) , " = " , str(c)

def pick_mul():
a,b,c=0,0,0
a = raw_input("Enter the first number :")
b = raw_input("Enter the second number :")
c = int(a) * int(b)
print str(a) , " * " , str(b) , " = " , str(c)

def pick_div():
a,b,c=0,0,0
a = raw_input("Enter the first number :")
b = raw_input("Enter the second number :")
c = int(a) / int(b)
print str(a) , " / " , str(b) , " = " , str(c)

while True:
print "Welcome to my first program in Python"
input = raw_input("To make a calculation, enter +,-,*,/ : ")
if input == '+' : pick_add()
if input == '-' : pick_sub()
if input == '*' : pick_mul()
if input == '/' : pick_div()


Heres a screen shot of it.

Originally posted by another user
You must login or register to view this content.


I love how Python is a very fast an efficient language at writing up simple quick functions and snippets, heres an example of Python shell to anyone unaware of Python.

Originally posted by another user
You must login or register to view this content.


So we open up Python Shell just like the Command Prompt Shell, we can write python directly into this shell and it will be interpreted as we write it.

Originally posted by another user
You must login or register to view this content.


We also can start defining functions in the Shell

Originally posted by another user
You must login or register to view this content.


Originally posted by another user
You must login or register to view this content.


So the following Function typed out directly in the shell can now be used until this shell is closed.

For example:

Originally posted by another user
You must login or register to view this content.


or

Originally posted by another user
You must login or register to view this content.


C++ was my first language, I feel like i can currently do significantly allot more in C++ than Python atm, but i have only just started learning, and i must say its a heck of a lot more exciting Happy *...Code.. Exciting... Sad*
(adsbygoogle = window.adsbygoogle || []).push({});
05-12-2011, 01:54 AM #2
schnzrs
Do a barrel roll!
Really good for a beginner! Just a heads up, you don't to conver ints to strings when printing.

Like this would work just fine:
     print a, " + " , b , " = " , c

But, if you are adding ints to strings with the + operator, then you need to use str().
05-12-2011, 02:45 AM #3
Fibonacci sequence ftw.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo