MVC & Python - Part1
MVC & Python - Part1
Try helloworld.py
print(“Hello,World!”)
•You can also write in the python command line directly to test some
functionality
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Example
x = 5
y = "Hello, World!“
#This is a comment.
print("Hello, World!")
•To add a multiline comment, you could insert a # for each line:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
x = 5
y = "John"
print(type(x))
print(type(y))
x = "John"
# is the same as
x = 'John'
2myvar = "John"
my-var = "John"
my var = "John"
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a list:
•To combine both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
x = "Python is "
y = "awesome"
z = x + y
print(z)
x = 5
y = 10
print(x + y)
x = 5
y = "John"
print(x + y)
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
• To create a global variable inside a function, you can use the global keyword.
If you use the global keyword, the variable belongs to the global
scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
• However, Python does not have a character data type, a single character
is simply a string with a length of 1.
a = "Hello, World!"
print(len(a))
Example
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
b = "Hello, World!"
print(b[2:5])
b = "Hello, World!"
print(b[:5])
Get the characters from position 2, and all the way to the
end:
b = "Hello, World!"
print(b[2:])
b = "Hello, World!"
print(b[-5:-2])
• The format() method takes unlimited number of arguments, and are placed into
the respective placeholders:
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price)
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of
item {1}."
print(myorder.format(quantity, itemno, price))
•When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
print(10 > 9)
print(10 == 9)
print(10 < 9)