Experiment No 1 Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

EXPERIMENT NO: 01

Date of Performance :

Date of Submission :

AIM: To study basics of input/output methods and operators in python.

SOFTWARE REQUIREMENT: Python

THEORY:

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is


designed to be highly readable. It uses English keywords frequently where as other languages
use punctuation, and it has fewer syntactical constructions than other languages.

Python Keywords
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function
name or any other identifier. They are used to define the syntax and structure of the Python
language. In Python, keywords are case sensitive.

Keywords in Python
False Class finally Is return
None Continue for lambda try
True Def from nonlocal while
and Del global not with
as elif if or yield
assert else import pass
break except in raise

Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0
to 9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all
are valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
3. Keywords cannot be used as identifiers.

Python Comments
In Python, the hash (#) symbol to start writing a comment. It extends up to the newline character.
Comments are for programmers for better understanding of a program. Python Interpreter ignores
comment.

Multi-line comments
If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the
beginning of each line. For example: Another way of doing this is to use triple quotes, either ''' or
""".

Python Variables
A variable is a named location used to store data in the memory. It is helpful to think of variables as
a container that holds data which can be changed later throughout programming.
Rules and Naming Convention for Variables and constants
1. Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase
(A to Z) or digits (0 to 9) or an underscore (_). For example:
2. snake_case
3. MACRO_CASE
4. camelCase
5. CapWords

Type Conversion:
The process of converting the value of one data type (integer, string, float, etc.) to another data type
is called type conversion. Python has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion

Implicit Type Conversion:


In Implicit type conversion, Python automatically converts one data type to another data type. This
process doesn't need any user involvement.

Explicit Type Conversion:


In Explicit Type Conversion, users convert the data type of an object to required data type.
Functions like int(), float(), str(), etc to perform explicit type conversion.This type conversion
is also called typecasting because the user casts (change) the data type of the objects.
Syntax :
(required_datatype)(expression)

Python Input, Output


Python provides numerous built-in functions that are readily available at the Python prompt.Some of
the functions like input() and print() are widely used for standard input and output operations
respectively.
the print() function to output data to the standard output device (screen).
To allow flexibility to take the input from the user. In Python, we have the input() function

Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value
that the operator operates on is called the operand.

Arithmetic operators

Arithmetic operators in Python


Operator Meaning Example
x+y
+ Add two operands or unary plus
+2
x-y
- Subtract right operand from the left or unary minus
-2
* Multiply two operands x*y
/ Divide left operand by the right one (always results into float) x/y
x % y (remainder of
% Modulus - remainder of the division of left operand by the right
x/y)
Floor division - division that results into whole number adjusted to
// x // y
the left in the number line
x**y (x to the
** Exponent - left operand raised to the power of right
power y)

Logical operators

Logical operators in Python


Operator Meaning Example
And True if both the operands are true x and y
Or True if either of the operands is true x or y
Not True if operand is false (complements the operand) not x
Operator Meaning Example
> Greater than - True if left operand is greater than the right x>y
Comparis n Operators
< Less than - True if left operand is less than the right x<y
== Equal to - True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
Greater than or equal to - True if left operand is greater than
>= x >= y
or equal to the right
Less than or equal to - True if left operand is less than or
<= x <= y
equal to the right
Membership operators

Operator Meaning Example


In True if value/variable is found in the sequence 5 in x
not in True if value/variable is not found in the sequence 5 not in x
PROGRAMS

a) Write a python program to input student details and display welcome message to newly added
student record.

INPUT:

print("NEW STUDENT REGISTRATION !\n")


name = input("Please Enter your name : ")
course = input("Please Enter your course : ")
class_div = input("Please Enter your class : ")
rollno = input("Please Enter your roll no : ")

print("\nWELCOME,",name+"!!")

print("\nYour Details are :\n")


print("Name :",name)
print("Course :",course)
print("Class :",class_div)
print("Roll No :",rollno)

OUTPUT:

b) Write a python Program to perform arithmetic operations on any given inputs


INPUT:

print("Arithemetic Operations !\n")


num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

add = num1 + num2


sub = num1 - num2
mul = num1 *
num2 div = num1 /
num2

print("\nAddition of First Number and Second Number is ",add)


print("\nSubstraction of First Number and Second Number is ",sub)
print("\nMultiplication of First Number and Second Number is ",mul)
print("\nDivsion of First Number and Second Number is ",div)

OUTPUT:

c) Write a python Program for Password Length Checker.


INPUT:

print("Password

Length Checker

!!") user_input =

input("Enter a

password : ")

if (len(user_input)<8 or len(user_input)>12):
print("Not valid ! Total characters should
be between 8 and 12 !") else:
print("Password is valid !")
print("Length of Password :",len(user_input))

OUTPUT:

CONCLUSION: Thus, studied basic input/output methods and operators in


python.

R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10 Marks)

You might also like