Part 5: Input and Output Functions, Loops
26. Python print() Tutorial : Printing single and multiple values, sep & end parameters
Lecture VideoCode Snippet
#Printing single value
print("------Printing single value------")
a,b = 10,20
print("Hello world!")
print(10)
print(a+b)
#Printing multiple values
print("------Printing multiple values------")
a,b,name = 10,20,"Sachin"
print(10,20,30)
print("Hello", name)
print("Sum:", a+b)
#sep parameter
print("------sep parameter------")
a,b,c = 10,20,30
print(a,b,c)
print(a,b,c, sep=" ")
print(a,b,c, sep="-")
print(a,b,c, sep="\t") #\t -> tab
#\n - newline
print("Hi Sachin\nHow are you?")
#end parameter
print("------end parameter------")
print("Sachin")
print(25)
print("India")
print("Sachin", end = "-")
print(25, end = "-")
print("India")
print("Hello world!")
#Example 1
print("------Example 1------")
print(10, 20, 30) #same as print(10, 20, 30, sep=" ", end="\n")
print("Sachin", "Sourav", "Rahul")
print("End of example")
#Example 2
print("------Example 2------")
print(10, 20, 30, sep="-", end="***")
print("Sachin", "Sourav", "Rahul", sep="-", end="***")
print("End of example")
#Example 3
print("------Example 3------")
print(10, 20, 30, sep="\n", end="***")
print("Sachin", "Sourav", "Rahul", sep="-")
print("End of example")
27. Python print() Tutorial: String Concatenation vs Comma
Lecture VideoCode Snippet
28. Python input() function: How Python Programs Take User Input
Lecture VideoCode Snippet
print("Program started")
name = input("Enter your name:")
print("Hello", name)
print("Find sum of two integers:")
a = int(input("Enter first integer number: ")) # "10" ---- not 10
b = int(input("Enter second integer number: ")) # "20" ---- not 20
print("Sum:", a+b)
print("Find total cost:")
item_count = int(input("Enter no of items: "))
price = float(input("Enter price of one item: "))
print("Total cost:", item_count * price)
29. While Loops Tutorial - Part 1: Simple while loop, Infinite loop, and Examples
Lecture VideoCode Snippet
# Printing first n natural numbers
print("Printing first n natural numbers")
n = 5
i = 1
while i<=n:
print(i)
i = i + 1
print("End of loop")
# Infinite loop
# while True:
# print("hello")
# Printing first n natural numbers
print("Printing first n natural numbers")
n = 5
i = 1
while True:
if i>n:
break
print(i)
i = i + 1
print("End of loop")
# Add integer numbers till user enters 0 -> Using while True + break syntax
print("Add integer numbers till user enters 0 -> Using while True + break syntax")
total = 0
while True:
number = int(input("Enter an integer number (0 to stop): "))
if number == 0:
break
total = total + number
print("Sum:", total)
# Add integer numbers till user enters 0 -> Using while condition syntax
print("Add integer numbers till user enters 0 -> Using while condition syntax")
total = 0
number = int(input("Enter an integer number (0 to stop): "))
while number != 0:
total = total + number
number = int(input("Enter an integer number (0 to stop): "))
print("Sum:", total)
30. While Loops Tutorial - Part 2: Input Data Validation, Break and Continue statements
Lecture VideoCode Snippet
# Age validation - Using infinite loop + break
while True:
age = int(input("Enter age: "))
if age >= 0:
break
print("Age should not be negative")
print("Age is:", age)
# Age validation - Using while condition syntax
print("Age validation - Using while condition syntax")
age = int(input("Enter age: "))
while age<0:
print("Age should not be negative")
age = int(input("Enter age: "))
print("Age is:", age)
# Using break and continue statements
print("Using break and continue statements")
total = 0
while True:
number = int(input("Enter an integer number (0 to stop): "))
if number == 0:
break
if number <0:
print("Skipping negative number")
continue
if number%5 == 0:
print("Skipping multiple of 5")
continue
total = total + number
print("Sum:", total)
# Using break and continue statements with while condition syntax
print("Using break and continue statements with non-infinite loop")
total = 0
number = int(input("Enter an integer number (0 to stop): "))
while number != 0:
if number==500:
break
if number<0:
print("Skipping negative number")
number = int(input("Enter an integer number (0 to stop): "))
continue
total = total + number
number = int(input("Enter an integer number (0 to stop): "))
print("Sum:", total)