Python
variables:
A variable is the name given to a memory location in a
program.
->Variables are containers for storing data values.
->Python has no command for
declaring a variable.
->When
we create a variable first we have to assign a value to it.
->for
example:
x = 5
y = "bhavana"
print(x)
print(y)
Output:
5
bhavana
Get the type:
If you want get a data type of a variable we use
‘type()’ as a function.
->for
example:
x = 5
y = "bhavana"
print(type(x))
print(type(y))
Output:
<class ‘int’>
<class ‘str’>
Python Datatypes:
Built in Datatypes:
In
programming, data type is an important concept.
Variables
can store data of different types, and different types can do different things.
Python
has the following data types built-in by default, in these categories:
Text Type: |
|
Numeric Types: |
|
Sequence Types: |
|
Mapping Type: |
|
Set Types: |
|
Boolean Type: |
|
Binary Types: |
|
None Type: |
|
Ex:
x = 78
print(type(x))
Output:
<class ‘int’>
Python
Numbers:
There are three
numeric types in Python:
int
float
complex
Variables
of numeric types are created when you assign a value to them:
Ex:
x = 12
y = 9.8
z = 8j
print(type(x))
print(type(y))
print(type(z))
Output:
<class ‘int’>
<class ‘float’>
<class ‘complex’>
Python typecasting:
There
may be times when you want to specify a type on to a variable. This can be done
with casting. Python is an object-orientated language, and as such it uses
classes to define data types, including its primitive types.
Casting
in python is therefore done using constructor functions:
- int() - constructs an integer number from an
integer literal, a float literal (by removing all decimals), or a string
literal (providing the string represents a whole number)
- float() - constructs a float number from an
integer literal, a float literal or a string literal (providing the string
represents a float or an integer)
- str() - constructs a string from a wide variety of data
types, including strings, integer literals and float literals
Ex:
x = int(10)
y =
float(2.8)
z =
float("3")
w =
str(“shiva”)
print(x)
print(y)
print(z)
print(w)
Output:
10
2.8
3
Shiva
Python
Strings:
->string
is a data type in python.
->string
is sequence of characters enclosed in quotes.
Ex:
print("Hello")
print('Bhavana')
Output:
Hello
Bhavana
Assign string to a
variable:
Assigning
a string to a variable is done with the variable name followed by an equal sign
and the string:
Ex:
a = "Avniet"
print(a)
Output:
Avniet
Python
Boolean values:
When
you compare two values, the expression is evaluated and Python returns true or
false.
Ex:1)
print(13> 9)
print(18 == 9)
print(17 < 9)
Output:
True
False
False
2)
a = 280
b = 63
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output:
b is not greater than a
0 Comments
Got questions? Feel free to ask!