Print: "Hello, World!"
Print: "Hello, World!"
#This is a comment
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Variables
VariableName
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Output Variables
x = "awesome"
print("Python is " + x)
x = "Python is "
y = "awesome"
z = x + y
print(z)
x = 5
y = "John"
print(x + y)
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Python Numbers
There are three numeric types in Python:
int
float
complex
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
Python Casting
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Looping string
for x in "banana":
print(x)
len()
a = "Hello, World!"
print(len(a))
Check String
b = "Hello, World!"
print(b[2:5])
b = "Hello, World!"
print(b[:5])
b = "Hello, World!"
print(b[2:])
Modify string
a = "Hello, World!"
print(a.upper())
a = "Hello, World!"
print(a.lower())
Try it Yourself »
a = "Hello, World!"
print(a.replace("H", "J"))
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
Try it Yourself »
Concat
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Format String
age = 36
txt = "My name is John, I am " + age
print(txt)
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
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))