Python 1
Python 1
A variable is a quantity that can store varying data values during the
execution of the program. A variable is represented by a symbolic name,
which may be short(like x,y) or descriptive (like name, age,grade , d_o_b )
x = "John"
# is the same as
x = 'John'
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)
Examples
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Many Values to Multiple Variables
Example
x, y, z = "Orange", "Banana", "Cherry“ a, b,c = 5, 6, 10
print(x) a, b, c = 30,15, “ Abhay”
print(y)
print(z)
Integer
Numeric
Floating point
String
x = 1 # int
y = 2.8 # float
Strings in python are surrounded by either single quotation marks, or double quotation
marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
a = "Hello"
print(a)
Operators are symbols that perform some calculations or manipulations on
data.
The quantities (values and variables ) that operators manipulate are known as
the operands
c=a =+ b a and b are operands and + is an operator.
Arithmetic operators
Assignment operators
Comparison operators /Relational
Logical operators
5 + 10
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Comparison Operators /Relational
Operators
Comparison operators are used to compare two values:
== Equal x == y False
!= Not equal x != y
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
Python Logical Operators
Logical operators are used to combine
conditional statements:
i = -15;
# condition 1
if i != 0:
# condition 2
if i > 0:
print("Positive")
# condition 3
if i < 0:
print("Negative")
i = 0;
# if condition 1
if i != 0:
# condition 1
if i > 0:
print("Positive")
# condition 2
if i < 0:
print("Negative")
else:
print("Zero")
Type Conversion in Python
The act of changing an object’s data
type is known as type conversion.
The Python interpreter automatically
performs Implicit Type Conversion.
Python prevents Implicit Type
Conversion from losing data.
The user converts the data types of
objects using specified functions in
explicit type conversion, sometimes
referred to as type casting.
Implicit Type Conversion in Python
In Implicit type conversion of data types in
Python, the Python interpreter automatically
converts one data type to another without any
user involvement.
x = 10
print("x is of type:",type(x))
y = 10.6
print("y is of type:",type(y))
z=x+y
print(z)
print("z is of type:",type(z))
Output
# initializing string
s = "10010"