Data Types and Variables¶
Syntax¶
Syntax refers to the guidelines that determine the structure of a language.
If the syntax of a language is not followed, the code will not be understood by a compiler or interpreter.
Key Features of Python Syntax:¶
Comments:
- Single-line comment:
#
- Multi-line comment:
""" """
- Single-line comment:
Case Sensitivity:
- Python is case-sensitive.
Statements:
- Python executes statements line by line, i.e., one line at a time.
Input/Output:
input()
is used to take user input.print()
is used to show the output.
Indentation:
- Python uses indentation to define code blocks instead of braces
{}
.
- Python uses indentation to define code blocks instead of braces
In [1]:
Copied!
# this is a comment
'''
this is a
multi line
comment
'''
# this is a comment
'''
this is a
multi line
comment
'''
Out[1]:
'\nthis is a\nmulti line \ncomment\n'
In [2]:
Copied!
"""
This is an example of
multi line comment.
Below shows the example of the CASE SENSTIVE in Python
"""
temperature = 20
TEMPerature = 30
print(temperature)
print(TEMPerature)
"""
This is an example of
multi line comment.
Below shows the example of the CASE SENSTIVE in Python
"""
temperature = 20
TEMPerature = 30
print(temperature)
print(TEMPerature)
20 30
In [3]:
Copied!
print(TEMPERATURE)
print(TEMPERATURE)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 print(TEMPERATURE) NameError: name 'TEMPERATURE' is not defined
In [5]:
Copied!
## input your name
name = input('Enter your name : ')
print(name)
## input your name
name = input('Enter your name : ')
print(name)
Nirajan
Variables¶
Variables are containers for storing data values.
Rules for Creating Variable Names¶
- Must start with a letter or the underscore (
_
) character - Cannot start with a number
- Can only contain alphanumeric characters and underscores (
A-z
,0-9
, and_
) - Case-sensitive —
mobile
,Mobile
, andMOBILE
are three different variables - Cannot be any of the Python keywords
Tip: Use meaningful variable names to make your code easier to understand.
In [6]:
Copied!
PI = 3.14
PI = 3.14
In [7]:
Copied!
TEMPERATURE = 20
TEMPERATURE = 20
In [9]:
Copied!
abc = 10
abc = 10
In [9]:
Copied!
temperature = 2
exhaust_temperature = 2
temperature = 2
exhaust_temperature = 2
In [ ]:
Copied!
## special characters lik $, -, % cannot be used in the variable name.
asdf$sdf = 10
## special characters lik $, -, % cannot be used in the variable name.
asdf$sdf = 10
Cell In[1], line 1 asdf$sdf = 10 ^ SyntaxError: invalid syntax
In [8]:
Copied!
# python keywords cannot be used as variable name
if = 10
# python keywords cannot be used as variable name
if = 10
Cell In[8], line 2 if = 10 ^ SyntaxError: invalid syntax
Data Types¶
Category | Data Types | Description |
---|---|---|
Text Type | str |
Stores text data (e.g., "Hello" ) |
Numeric Types | int , float , complex |
int : Integers (e.g., 5) float : Decimals (e.g., 3.14) complex : Complex numbers (e.g., 2 + 3j) |
Sequence Types | list , tuple , range |
Ordered collections list and tuple store multiple items range represents a sequence of numbers |
Mapping Type | dict |
Key-value pairs (e.g., {"name": "Alice"} ) |
Set Types | set , frozenset |
Unordered collections of unique items |
Boolean Type | bool |
Represents truth values: True or False |
In [10]:
Copied!
a = 2
b = 5
print(a + b)
print(a - b)
a = 2
b = 5
print(a + b)
print(a - b)
7 -3
In [11]:
Copied!
a = 2+5j
print(a)
print(type(a))
a = 2+5j
print(a)
print(type(a))
(2+5j) <class 'complex'>
In [ ]:
Copied!
In [25]:
Copied!
x = 5 + 2j
y = 5 - 2j
print(x)
print(y)
print(x + y)
print(x - y)
print(x / y)
print(x * y)
print(x ** y)
x = 5 + 2j
y = 5 - 2j
print(x)
print(y)
print(x + y)
print(x - y)
print(x / y)
print(x * y)
print(x ** y)
(5+2j) (5-2j) (10+0j) 4j (0.7241379310344828+0.6896551724137931j) (29+0j) (1025.9425992055105-9639.459628933018j)
Type Casting¶
- Type casting, also known as type conversion, is the process of changing the data type of a variable from one type to another.
In [12]:
Copied!
x_string = '10asdfa'
x_int = int(x_string)
print(x_string)
print(x_int)
x_string = '10asdfa'
x_int = int(x_string)
print(x_string)
print(x_int)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[12], line 2 1 x_string = '10asdfa' ----> 2 x_int = int(x_string) 4 print(x_string) 5 print(x_int) ValueError: invalid literal for int() with base 10: '10asdfa'
In [27]:
Copied!
print(type(x_string))
print(type(x_int))
print(type(x_string))
print(type(x_int))
<class 'str'> <class 'int'>
Q. Take two numbers as an input from user and add them
In [28]:
Copied!
a = 10
b = str(a)
print(a)
print(b)
print(type(a), type(b))
a = 10
b = str(a)
print(a)
print(b)
print(type(a), type(b))
10 10 <class 'int'> <class 'str'>
In [ ]:
Copied!
a = 10
print(a)
print(type(a))
b = float(a)
print(b)
print(type(b))
a = 10
print(a)
print(type(a))
b = float(a)
print(b)
print(type(b))
10 <class 'int'> 10.0 <class 'float'>
In [3]:
Copied!
a = 10.123
print(a)
print(type(a))
b = int(a)
print(b)
print(type(b))
a = 10.123
print(a)
print(type(a))
b = int(a)
print(b)
print(type(b))
10.123 <class 'float'> 10 <class 'int'>
In [6]:
Copied!
a = '10'
b = int(a)
a = '10'
b = int(a)
Dynamic Typing in Python¶
In statically typed languages like C
int a = 10
The data type is explicitly declared at compile time, meaning a is known to be an integer before the program runs.
In [10]:
Copied!
"""
In python, data type is specified during the runtime.
"""
a = 10
"""
In python, data type is specified during the runtime.
"""
a = 10
Exercise: Write a program to take two numbers from user and add them¶
In [ ]:
Copied!