Jul 25, 2024
Downloads
and select the latest versionapp.py
)print("Hello, World!")
Definition: Used to temporarily store data
Example:
age = 20
print(age)
age = 30
print(age)
Types of Variables:
Example for different types:
price = 19.95 # float
first_name = "Mosh" # string
is_online = True # boolean
Exercise: Declare variables for hospital check-in
input()
to receive user inputname = input("What is your name? ")
print("Hello " + name)
int()
, float()
, bool()
, str()
birth_year = int(input("Enter your birth year: "))
age = 2020 - birth_year
print(age)
first = float(input("Enter first number: "))
second = float(input("Enter second number: "))
sum = first + second
print("Sum is: " + str(sum))
.upper()
, .lower()
, .find()
, .replace()
course = "Python for Beginners"
print(course.upper())
print(course.find("for"))
print(course.replace("Beginners", "Experts"))
print("Python" in course)
+
, -
, *
, /
, //
, %
, **
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 // 3)
print(10 % 3)
print(10 ** 3)
x = 10
x += 3 # same as x = x + 3
print(x)
x = 10 + 3 * 2 # Result: 16
x = (10 + 3) * 2 # Result: 26
>
, >=
, <
, <=
, ==
, !=
and
, or
, not
price = 25
print(price > 10 and price < 30)
print(price > 10 or price < 30)
print(not price > 10)
Structure:
if
, elif
, else
Example:
temperature = 35
if temperature > 30:
print("It's a hot day")
print("Drink plenty of water")
elif temperature > 20:
print("It's a nice day")
elif temperature > 10:
print("It's a bit cold")
else:
print("It's cold")
print("Done")
Exercise: Write a weight converter program
i = 1
while i <= 5:
print(i)
i += 1
i = 1
while i <= 10:
print(i * '*')
i += 1
numbers = [1, 2, 3, 4, 5]
for item in numbers:
print(item)
for number in range(5):
print(number) # 0 to 4
for number in range(5, 10):
print(number) # 5 to 9
for number in range(5, 10, 2):
print(number) # 5, 7, 9
names = ["John", "Bob", "Mosh", "Sam", "Mary"]
print(names)
print(names[0]) # 'John'
print(names[-1]) # 'Mary'
names[0] = "Jonathan"
print(names[0:3]) # ['Jonathan', 'Bob', 'Mosh']
append()
, insert()
, remove()
, clear()
, in
, len()
numbers = (1, 2, 3)