print("Hello Python") # declare a single variable a = 2 name = "george" print(name) #declare multiple variables at the same time b = c = 2 # standard math operations work a = a + b +c print(a) #plus sign is used to put variables and strings together. #str converts a string to a number. Numbers cannot be added to strings. print(name + " you are " + str(a)) # int truncates decimals num = 9.345 print( int(num)) # x = "9" y = int(x) y=y+2 print(y) # x= 2 y = 3 # you can print the result of a math operation result = x > y print(result) # conditionals require a colon. #indentation is extremely important. Make sure to use the tab key!!!!!! if x<y: print("x is less than y") print("wow you did it") #else uses a colon also. #input allows you to input data into a popup box or into the output section of your IDE #in many IDEs the text i the input will appear and then you type a result. In pythonfiddle your text #and result appear after you enter your data. score = 0 resp = input("who are you. Please type here: ") if resp == "bob" or resp == "jane": print("welcome bob and or jane") score = score + 1 else: print("who are you") #input is always considered to be a string #use int() to convert the input to a number b = input("give me a number") # This would produce an error #print(b + 5) #this code works print (int(b) + 5) #defs are just functions that are used in python. They can return data but they don't have to. #parameters are also optional if you need to use them #the function call needs to occur after you have defined the function def checkScore(myScore): if x >=90: print("good") else: print("bad") checkScore(85) #these defs return values to the function call def subtract(z,y): return z-y def multiply(z,y): return z*y num1 = 5 num2 = 2 if subtract(num1,num2) >0: print ("the result is positive") else: print("the result is negative")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question