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

Python Variables: Naming and Assignment Guide

Uploaded by

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

Python Variables: Naming and Assignment Guide

Uploaded by

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

Python

Variables
Python Variables
 In Python, 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 many other programming languages, Python variables do not
require explicit declaration of type.
 The type of the variable is inferred based on the value assigned.

Output
Rules for Naming Variables
To use variables effectively, we must follow Python’s
naming rules:
•Variable names can only contain letters, digits and underscores (_).
•A variable name cannot start with a digit.
•Variable names are case-sensitive (myVar and myvar are different).
•Avoid using Python keywords (e.g., if, else, for) as variable names.
Assigning Values to Variables
 Basic Assignment
Variables in Python are assigned values using the = operator.

 Dynamic Typing
Python variables are dynamically typed, meaning the same variable can hold
different types of values during execution.
Multiple Assignments
 Python allows multiple variables to be assigned values in a single line.

Assigning the Same Value


Python allows assigning the same value to multiple variables in a single
line, which can be useful for initializing variables with the same value.

Assigning Different Values


We can assign different values to multiple variables simultaneously,
making the code concise and easier to read.
Type Casting a Variable
 Type casting refers to the process of converting the value of one data
type into another.
 Python provides several built-in functions to facilitate casting, including
int(), float() and str() among others.

 Basic Casting Functions


 int() - Converts compatible values to an integer.
 float() - Transforms values into floating-point numbers.
 str() - Converts any data type into a string.
Type Casting Example :-
Getting the Type of Variable
 In Python, we can determine the type of a variable using the type()
function.
 This built-in function returns the type of the object passed to it.
Practical Examples
1. Swapping Two Variables

2. Counting Characters in a String

You might also like