python
python
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Output:-____________________________
Example
print("Hello, World!") #This is a comment
Example
#print("Hello, World!")
print("Cheers, Mate!")
Output:-____________________________
Output:-____________________________
Since Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Output:-____________________________
Variables
Variables are containers for storing data values.
Example
x = 5
y = "John"
print(x)
print(y)
Output:-____________________________
Variables do not need to be declared with any particular type, and can even change type
after they have been set.
Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Output:-____________________________
Example
x = "Python is "
y = "awesome"
z = x + y
print(z)
Output:-____________________________
Example
x = 5
y = 8
z = x + y
print(z)
Output:-____________________________
Example
x = "awesome"
print("Python is " + x)
Output:-____________________________
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Output:-____________________________
Output:-____________________________
Output:-____________________________
Case-Sensitive
Variable names are case-sensitive.
Example
This will create two variables:
a = 4
A = "Sally"
#A will not overwrite a
Write a program to display the values of these two variables
Output:-____________________________
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:
Example
Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Example
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Output:-____________________________
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output:-____________________________
User Input
Python allows for user input.
That means we are able to ask the user for input.
The method is a bit different in Python 3.6 than Python 2.7.
Python 3.6 uses the input() method.
Python 2.7 uses the raw_input() method.
The following example asks for the username, and when you entered the username, it gets
printed on the screen:
Python 3.6
Example:-
name = input("Enter your name:")
print(name)
print("Username is: " + name)
Output:-____________________________
Example:-
n=input(“Enter Your Name”)
a=input(“Enter Your age”)
m=input(“Enter Your marks”)
print(“Name is”,n)
print(“Age is”,a)
print(“Total marks”,m)
Output:-____________________________
Example:-
n=str(input(“Enter Your Name”))
a=int(input(“Enter your age”))
m=float(int(input(“Enter Your total marks”))
print(“Name is”,n)
print(“Age is”,a)
print(“Total marks”,n)
Output:-____________________________
Addition Of two numbers
Example:-
n=str(input(“Enter Your Name”))
m1=int(input(“Enter your Marks1”))
m2=float(input(“Enter Your Marks2”))
t=m1+m2
print(“Name is”,n)
print(“Total marks”,t)
Output:-____________________________
Subtraction
Example:-
n=str(input(“Enter Your Name”))
m1=int(input(“Enter First value”))
m2=float(input(“Enter second value”))
t=m1-m2
print(“Name is”,n)
print(“Total marks”,t)
Output:-____________________________
Multiplication
Example:-
n=str(input(“Enter Your Name”))
m1=int(input(“Enter First value”))
m2=float(input(“Enter second value”))
t=m1-m2
print(“Name is”,n)
print(“Total marks”,t)
Output:-____________________________
Division
Example:-
n=str(input(“Enter Your Name”))
m1=int(input(“Enter First value”))
m2=float(input(“Enter second value”))
t=m1/m2
print(“Name is”,n)
print(“Total marks”,t)
Output:-____________________________