Here you will learn variables, data types, and indentation in Python.
7. What is indentation in Python?
Indentation is important while writing code in Python. If you do not provide proper indentation for a block of statements, the system will throw an error during execution. Indentation refers to the use of tabs or whitespaces to define a group of statements. See the example below.
Below is the correct code:
a = 5
b = 6
if a > b:
print("It compares the result")
print("a is greater than b")
else:
print("b is greater than a")
In the above code, spaces after the if condition are crucial and are marked for easy understanding. The two print statements below the if block belong to it. In Python, curly braces { } are not used like in other languages (e.g., Java). Instead, indentation determines the block of code.
Below is error code 1:
a = 5
b = 6
if a > b:
print("It compares the result")
print("a is greater than b")
else:
print("b is greater than a")
Below is error code 2:
a = 5
b = 6
if a > b:
print("It compares the result")
print("a is greater than b")
else:
print("b is greater than a")
In error code 2, the second print statement under the if block follows the indentation rule, but the first print statement does not.
8. Single-line and multi-line comments
Comments are used to provide clarification or explanation of code. Comments are not executed by Python.
Single-line comments start with #:
# a, b values defined
a = 5
b = 6
# This code finds the greater value
if a > b:
print("a is greater than b")
Multi-line comments are enclosed within triple single quotes ''':
'''
a, b values defined
This code finds the greater value
'''
if a > b:
print("a is greater than b")
9. Variables in Python
Variables are named memory locations used to store values in a program. In Python, variables can be declared and initialized as needed.
Use = to assign a value to a variable:
a = 1
name = "janu"
Rules to define variables:
- Variable names should start with a lowercase letter (optional).
- Internal words should start with an uppercase letter.
- Variable names should not start with a dollar sign ($).
- Variable names can start with an underscore (_).
- String values must be enclosed in single or double quotes (" ").
- Variable names are case-sensitive.
Examples of valid variable names:
name = "ram"
myName = "ram"
_name = "ram"
name1 = "ram"
my_name = "ram"
age = 30
Examples of invalid variable names:
1name = "ram"
$name = "ram"
my name = "ram"
my-name = "ram"
10. Various types of data types in Python
The basic data types in Python are:
- Numbers
- Boolean
- String
- Dictionary
- List
- Tuple
- Set
1. Numbers
Numbers are classified into integers and floating-point numbers.
Integer:
# Integer number
a = 30
print(a)
Floating:
# Floating-point number
a = 30.14
print(a)
2. Boolean
Boolean data contains either True or False.
# Boolean values
exp = True
print(exp)
exp = False
print(exp)
3. String
Strings store text values and can be defined with single or double quotes.
# Strings
name = "My name is Obula"
print(name)
# Or
name = 'My name is Obula'
print(name)
4. Dictionary
Dictionaries store key-value pairs.
# Dictionary
dict = {1: 'Obula', 2: 'Ram'}
print(dict)
Output: {1: 'Obula', 2: 'Ram'}
5. List
Lists store multiple values, and the elements can be of different types.
# List
list = [1, "Obula", 3.5]
print(list)
6. Tuple
Tuples are like lists but are immutable. They are defined using parentheses ( ).
# Tuple
tuple = (1, "Obula", 3.5)
print(tuple)
7. Set
Sets store multiple values and do not allow duplicates.
# Set
set = {"banana", "orange", "berry"}
print(set)
Click below to read in-depth.
Comments
Post a Comment