Variable is simply defined as:
my_string = "Always look on the bright side of life!"
Never use quotation marks (
") with booleans, and always capitalize the first letter! Python is case-sensitive (it cares about capitalization).
In Python, statements are separated by whitespace (no ; at the end!).
Code needs to be indented by 4 spaces (or a tab).
eggs = 12
Comments begin with # or use triple quotation marks for a multi-line comment.
""" Hi there
this is my comment
Long one indeed!"""
Arithmetic operators: +, -, *, /, % and ** (Exponentiation). The
operator raises the first number, the base, to the power of the second number, theexponent.
my_var = 10**2
Backslash (\) escapes a string with quotes.
'Help! Help! I\'m being repressed!'
So if you wanted "Y", you could just type "PYTHON".
String methods: string methods are pre-built pieces of code that perform specific tasks for strings. They are: len(), lower(), upper() and str()
print "The value of pi is around " + str(3.14)
+ works as string concat operator but first we need to convert 3.14 (a float) to string using str()
% (, , )
Getting user input on Console:
name = raw_input("What is your name?")
from datetime import datetime
now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year)