Python Programming
Python Programming
• You can get the type of a variable with the type() function
•x = 5
y = "John"
print(type(x))
print(type(y))
• Case-Sensitive
• Variable names are case-sensitive.
• a = 4
A = "Sally"
#A will not overwrite a
• Single or double quotes
• x = "John"
# is the same as
x = ’John’
Python - Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
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.
• Legal names:
• myvar = “Akash"
my_var = “Akash"
_my_var = “Akash"
myVar = “Akash"
MYVAR = “Akash"
myvar2 = “Akash“
• Illegal names:
• 2myvar = “Akash"
my-var = “Akash"
my var = “Akash"
Python Variables - Assign Multiple Values
myfunc()
print("Python is " + x)
Python Data Types
Text Type: str
Numeric Types: int, float, complex
• Python Conditions
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
Elif
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Short Hand If
a = 33
b = 200
if b > a:
pass
Python While Loops
for x in adj:
for y in fruits:
print(x, y)
The pass Statement
• For loops cannot be empty, but if you for some reason have a
for loop with no content, put in the pass statement to
avoid getting an error.
• for x in [0, 1, 2]:
pass