# variables need to be defined, i.e assisgned a value, before they can be used. #------------------------Variable Type Change During Math---------------------------------- # 2 is and examples of an integer while 2.0 is an example of a float # 2/2 will give an interger as the result # 2.0/2 will give a float as the result #------------------------Special Operators-------------------------------------------------- # the operator % gives the remainder of a divison. For example 3%2 = 1. # the operator // gives a floor division. For example 7//2 = 3. # the operator ** gives the power of a number. For example 2**3 = 8 #------------------------Imanginary Numbers------------------------------------------------- # the letter j after a number denotes it as imaginary print "Imaginary numbers" print 2 + 3j + 1 + 4j #------------------------Strings------------------------------------------------------------- # single and double quotes give the same result # the print command removes the enclosing quotes print 'single quotes' print "double quotes" # the backslash character is used to escape single and double quotes print 'doesn\'t', "\"yes\"" #use \' to escape the single quote and double quote. print "doesn't", '"yes"' #use double quotes to escape single quotes vice vesra #RAW STRINGS # r before quotes will print the raw text within the quotes print r'C:\some\name' #\n means a new line when the r is not used before the quotes print 'C:\some\name' #TRIPPLE QUOTES = spans multiple lines. The new line character is automatically included print("""This is using triple quotes and the new line is automaticalled added """) print("""Use the backslash to remove the new line\ when using tiriple quotes""") #INDEXING AND CONCATENATING # with indexing, the first character is at position 0 # the indexed positions are READ ONLY myname = 'johndoe' print myname[3] #indexed at the 4th character print myname[-1] #indexed at the last character print myname[0:3] #returns the first to the 3 character print myname[:3] #returns up to the third character print myname[3:] #returns from the thrd to the last character print myname[:3] + myname[3:] #concatenation hername = 'JOA'+myname[3:] #concatenation example 2 print hername #LENGTH OF A STRING print len(hername) #the function len() returns the lenght of a string
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