Python
Python
◦ Example:
◦ #This is a comment
"""
print("Hello, World!")
This is a comment
written in
(Or) more than just one line
"""
◦ print("Hello, World!") #This is a comment
What is variable?
◦ Variables are containers for storing data values.
◦ Example
x=5
y = "John"
print(x)
print(y)
Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• A variable name cannot be any of the Python keywords.
Variable Name
Legal variable names: Illegal variable names:
◦ myvar = "John"
my_var = "John" • 2myvar = "John"
_my_var = "John" • my-var = "John"
myVar = "John" • my var = "John"
MYVAR = "John"
myvar2 = "John"
Python Data Types
Text str
Type:
Boolean bool
Type:
Python Number
◦ x = 1 # int
◦ y = 2.8 # float
◦ z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
String Concatenation
◦ To concatenate, or combine, two strings you can use the + operator.
Example-1
(Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c = a + b
print(c)
Example-2
a = "Hello"
b = "World"
c = a + " " + b
print(c)
String Format
Example-1
◦ age = 36
txt = "My name is John, I am " + age
print(txt)
Example-2
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
Example-3
◦ quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
Python Booleans
Example-1
◦ print(10 > 9)
print(10 == 9)
print(10 < 9)
Example-2
◦ a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Python Lists
◦ thislist = ["apple", "banana", "cherry"]
print(thislist)