0% found this document useful (0 votes)
4 views

python

The document provides practical examples of Python programming concepts, including comments, variables, data types, and user input. It explains how to create single and multi-line comments, declare variables, perform type casting, and handle user input in Python 3.6. Additionally, it covers arithmetic operations such as addition, subtraction, multiplication, and division with user input.

Uploaded by

alenajain27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python

The document provides practical examples of Python programming concepts, including comments, variables, data types, and user input. It explains how to create single and multi-line comments, declare variables, perform type casting, and handle user input in Python 3.6. Additionally, it covers arithmetic operations such as addition, subtraction, multiplication, and division with user input.

Uploaded by

alenajain27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python(Practical Questions)

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:-____________________________

Multi Line Comments


Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line:
Example
#This is a comment
#written in
#more than just one line
print("Hello, World!")

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:-____________________________

Get the Type


You can get the data type of a variable with the type() function.
Example
x = 5
y = "John"
print(type(x))
print(type(y))

Output:-____________________________

Single or Double Quotes?


String variables can be declared either by using single or double quotes:
Example
x = "John"
# is the same as
y = 'John'
Write a program to display the values of these two variables

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:

• 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)

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"

Many Values to Multiple Variables


Python allows you to assign values to multiple variables in one line:

Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Output:-____________________________

One Value to Multiple Variables


And you can assign the same value to multiple variables in one line:

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:-____________________________

You might also like