0% found this document useful (0 votes)
4 views17 pages

Python Input

The document provides an overview of Python variables, including their definition, naming rules, value assignment, and type casting. It explains implicit and explicit type conversion methods, along with examples of using built-in functions for type casting. Additionally, it covers Python's input/output functions, arithmetic, comparison, logical, and bitwise operators, control flow statements, loops, functions, and data structures like lists and arrays.

Uploaded by

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

Python Input

The document provides an overview of Python variables, including their definition, naming rules, value assignment, and type casting. It explains implicit and explicit type conversion methods, along with examples of using built-in functions for type casting. Additionally, it covers Python's input/output functions, arithmetic, comparison, logical, and bitwise operators, control flow statements, loops, functions, and data structures like lists and arrays.

Uploaded by

japneet kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Variables

Last Updated :22 May, 2026





Variables are used to store data that can be referenced and
manipulated during program execution. A variable is essentially a
name that is assigned to a value.
 Unlike Java and many other languages, Python variables do
not require explicit declaration of type.
 Type of the variable is inferred based on the value
assigned.

x = 5
name = "Alex"
print(x)
print(name)

Output
5
Alex

Rules for Naming Variables


To use variables correctly, the following naming rules should be
followed:
1. Names can contain letters, digits and underscores (_).
2. The first character cannot be a digit.
3. Names are case-sensitive, so myVar and myvar are treated
differently.
4. Keywords such as if, else and for cannot be used as
variable names.

Assigning Values to Variables


1. Basic Assignment: Variables are assigned values using the =
operator.
Page 1 of 17
x = 5
y = 3.14
z = "Hi"
2. Dynamic Typing: Python is dynamically typed, so the same
variable can store different data types during execution.
x = 10
x = "Now a string"
3. Assigning Same Value: same value can be assigned to
multiple variables in a single line.

a = b = c = 100
print(a, b, c)

Output
100 100 100
4. Assigning Different Values: Multiple variables can also be
assigned different values in a single line.

x, y, z = 1, 2.5, "Python"
print(x, y, z)

Output
1 2.5 Python

Type Casting in Python


Last Updated :29 Jul, 2026



Type Casting is the method to convert the Python variable datatype
into a certain data type in order to perform the required operation

Page 2 of 17
by users. We will see various techniques for typecasting. There can
be two types of Type Casting in Python:
Implicit Type Conversion
 Explicit Type Conversion
Example: The following code converts a numeric string into an
integer so it can be used in arithmetic operations.

age = "21"
age = int(age)
print(age + 5)
print(type(age))

Output
26
<class 'int'>

Implicit Type Conversion


Implicit type conversion is the automatic changing of a data value
from one type to another by the compiler or runtime environment
without any manual intervention from the programmer

# Python automatically converts 'a' to int


a = 7
print(type(a))

# Python automatically converts 'b' to float


b = 3.0
print(type(b))

# Python automatically converts 'c' to float as it is a float


addition
c = a + b
print(c)
print(type(c))

Page 3 of 17
# Python automatically converts 'd' to float as it is a float
multiplication
d = a * b
print(d)
print(type(d))

Output
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>
Explanation:
 a is an integer and b is a floating-point number.
 During a + b, Python automatically converts a to float, so c
becomes 10.0.
 During a * b, Python again converts a to float, so d
becomes 21.0.
 Since the operations involve both int and float, the results
are returned as float.
 This automatic conversion is known as implicit type
conversion.
Explicit Type Conversion
Explicit type conversion is when the programmer manually changes
a value’s data type using built-in type casting functions, usually
when automatic conversion is not possible or a specific type is
needed.

Examples of Type Casting

Commonly used type casting functions in Python are:


 Int() function take float or string as an argument and
returns int type object.
 float() function take int or string as an argument and return
float type object.

Page 4 of 17
 str() function takes float or int as an argument and returns
string type object.

Convert Int to Float

Converting Int to Float in with the float() function.

a = 5
n = float(a)

print(n)
print(type(n))

Output
5.0
<class 'float'>

Python Convert Float to Int

Converting Float to int datatype in Python with int() function.

a = 5.9
n = int(a)

print(n)
print(type(n))

Output
5
<class 'int'>

Page 5 of 17
Python Convert int to String

Converting int to String datatype in Python with str() function.

a = 5

# typecast to str
n = str(a)

print(n)
print(type(n))

Output
5
<class 'str'>

Python Convert String to float

Casting string data type into float data type with float() function.

a = "5.9"
n = float(a)

print(n)
print(type(n))

Output
5.9
<class 'float'>

Page 6 of 17
Python Convert string to int

Converting string to int datatype in Python with int() function. If the


given string is not number, then it will throw an error.
a = "5"
b = 't'
n = int(a)

print(n)
print(type(n))

print(int(b))
print(type(b))
Output
5
<class 'int'>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/[Link] in <cell line: 0>()
9 print(type(n))
10
---> 11 print(int(b))
12 print(type(b))
ValueError: invalid literal for int() with base 10: 't'
The string "5" is successfully converted into an integer 5. Since 't'
is not a number, Python cannot convert it into an integer.

Adding String and Integer Without Conversion

If we try to directly add an integer and a string, Python will throw


an error because they are different data types.
a = 5
b = 't'

n = a+b

print(n)
print(type(n))
Output
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Page 7 of 17
We should first convert the string into an integer and then add:

a = 5
b = '7'

# explicit conversion of string to int


n = a + int(b)
print(n)
print(type(n))

Output
12
<class 'int'>

Page 8 of 17
Python Input/Output
Python provides simple built-in functions to take input from the
user and display output on the screen.
1. Input: The input() function is used to take input from the user.
By default, the value entered by the user is stored as a string.
val = input("Enter your value: ")
print("You entered:", val)

2. Output: The print() function is used to display values or


messages.

print "Hello, Geek!"

1. Arithmetic Operators: These are used to perform basic


mathematical operations like addition, subtraction, multiplication,
and division. Types of arithmetic operators: +, -, *, /, //, %, **
. Precedence of Arithmetic Operators are as follows:

Page 9 of 17
1. P - Parentheses
2. E - Exponentiation
3. M - Multiplication (Multiplication and division have the same
precedence)
4. D - Division
5. A - Addition (Addition and subtraction have the same
precedence)
6. S - Subtraction

a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
mod = a % b
exp = a ** b

print(add)
print(sub)
print(mul)
print(mod)
print(exp)

2. Comparison Operators : These are used to compare two values.


They return a Boolean value either True or False depending on
whether the comparison is correct.

a = 10
b = 20

print(a == b) # False, because 10 is not equal to 20


print(a != b) # True, because 10 is not equal to 20
print(a > b) # False, 10 is not greater than 20
print(a < b) # True, 10 is less than 20
print(a >= b) # False, 10 is not greater than or equal to 20
print(a <= b) # True, 10 is less than or equal to 20

Page 10 of 17
3. Logical Operators: It perform Logical AND, Logical OR and Logical
NOT operations. It is used to combine conditional statements.
Types of logical operators are: AND, OR, NOT.

a = True
b = False
print(a and b)
print(a or b)
print(not a)
4. Bitwise Operators: This act on bits and perform bit-by-bit
operations. These are used to operate on binary numbers. Types of
bitwise operators are: &, |, ^, ~, <<, >>

a = 10
b = 4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)

Output
0
14
-11
14
2
40

Python If Else

Page 11 of 17
In Python, the if statement runs a block of code when a condition is
True. If the condition is False, the else block runs. This helps
programs make decisions based on conditions.
Example 1: This example checks whether the value of i is less
than 15. If the condition is true, the if block runs; otherwise, the
else block runs.

i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
Example 2: This example checks multiple conditions for the value
of i. Python evaluates each condition in order and executes the
block where the condition becomes true.

i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")

Output
i is 20
Explanation:
 Python checks conditions from top to bottom.
 Once a condition is True, it executes that block and skips
the rest.
 If none of the conditions match, it executes the else block.

Page 12 of 17
Python Loops
1. For Loop: It is used to iterate over a sequence such as
a list, string, or a range of numbers. It runs a block of code once for
each item in the sequence. Below example uses range() to
generate numbers from 0 to 9 with a step of 2 and prints each
value.

for i in range(0, 10, 2):


print(i)

Output
0
2
4

6
8

2. While Loop: It continues to execute as long as a condition is


True. In below example, the condition for while will be True as long
as the counter variable (count) is less than 3.

count = 0
while (count < 3):
count = count + 1
print("Hello Geek")

Output
Hello Geek
Hello Geek

Page 13 of 17
Hello Geek
Explanation:
 The loop runs 3 times because count goes from 0 - 1 - 2.
 Once count becomes 3, the condition count < 3 becomes
False and the loop stops.

Python Functions
Python Function is a block of reusable code that performs a specific
task. Functions help make your code modular, readable, and easier
to debug. There are two main types of functions in Python:
1. Built-in functions like: print(), len(), type()
2. User-defined functions created using the def keyword

Example: This example shows a simple user-defined function that


checks whether a number is even or odd using a conditional
statement.

Example: This example shows a simple user-defined function that


checks whether a number is even or odd using a conditional
statement.

Page 14 of 17
def evenOdd(x):
if x % 2 == 0:
print("even")
else:
print("odd")

evenOdd(2)
evenOdd(3)

Page 15 of 17
Python Arrays
Last Updated :9 Jun, 2026



Python provides multiple ways to work with linear data structures,
which store elements sequentially. Although Python does not have
a built-in array type like some other languages, similar functionality
can be achieved using:

Lists
Lists are a flexible and commonly used data structure for storing
elements in sequence. Unlike arrays in other languages, list:
 Can store mixed data types in one list
 Elements can be added or removed easily
 Provide functions like append(), remove(), sort(), etc.

Page 16 of 17
a = [1, "Hello", [3.14, "world"]]
[Link](2) # Add an integer to the end
print(a)

Output
[1, 'Hello', [3.14, 'world'], 2]

Arrays
Array is a collection of elements stored at contiguous memory
locations, used to hold multiple values of the same data type.
Unlike Lists, which can store mixed types, arrays are homogeneous
and require a typecode during initialization to define the data type.

import array as arr


a = [Link]('i', [1, 2, 3])

# accessing first array


print(a[0])

# adding element to array


[Link](5)
print(a)

Output
1
array('i', [1, 2, 3, 5])

Page 17 of 17

You might also like