Python CheatSheet
Basics
Basic syntax from the Python programming language
Showing Output To User
The print function is used to display or print output as follows:
print("Content that you wanna print on screen")
We can display the content present in an object using the print function as
follows:
var1 = "Shruti"
print("Hi my name is: ", var1)
You can also use f-strings for cleaner output formatting:
name = "Shruti"
print(f"Hi my name is: {name}")
Taking Input From the User
The input function is used to take input as a string from the user:
var1 = input("Enter your name: ")
print("My name is: ", var1)
Typecasting allows us to convert input into other data types:
, Integer input:
var1 = int(input("Enter the integer value: "))
print(var1)
Float input:
var1 = float(input("Enter the float value: "))
print(var1)
range Function
The range function returns a sequence of numbers, starting from start , up to
but not including stop , with a default step of 1:
range(start, stop, step)
Example - display all even numbers between 1 to 100:
for i in range(0, 101, 2):
print(i)
Comments
Single Line Comment
# This is a single line comment
Basics
Basic syntax from the Python programming language
Showing Output To User
The print function is used to display or print output as follows:
print("Content that you wanna print on screen")
We can display the content present in an object using the print function as
follows:
var1 = "Shruti"
print("Hi my name is: ", var1)
You can also use f-strings for cleaner output formatting:
name = "Shruti"
print(f"Hi my name is: {name}")
Taking Input From the User
The input function is used to take input as a string from the user:
var1 = input("Enter your name: ")
print("My name is: ", var1)
Typecasting allows us to convert input into other data types:
, Integer input:
var1 = int(input("Enter the integer value: "))
print(var1)
Float input:
var1 = float(input("Enter the float value: "))
print(var1)
range Function
The range function returns a sequence of numbers, starting from start , up to
but not including stop , with a default step of 1:
range(start, stop, step)
Example - display all even numbers between 1 to 100:
for i in range(0, 101, 2):
print(i)
Comments
Single Line Comment
# This is a single line comment