5 -Python Fundamentals
5 -Python Fundamentals
• Example:
name = "Alice"
age = 25
height = 5.6
Best Practices
• Use descriptive names that reflect the purpose of the variable.
• Use lowercase letters for variable names.
• Separate words using underscores for readability (e.g., first_name ,
total_amount ).
Data Types in Python
Python supports several built-in data types:
Typecasting in Python
What is Typecasting?
• Typecasting is the process of converting one data type to another.
• Python provides built-in functions for typecasting:
• int() : Converts to integer.
• float() : Converts to float.
• str() : Converts to string.
• bool() : Converts to boolean.
Examples:
• The input() function allows you to take user input from the keyboard.
• By default, input() returns a string. You can convert it to other data types as
needed.
• Example:
Comments
• Comments are used to explain code and are ignored by the Python
interpreter.
Escape Sequences
• Escape sequences are used to include special characters in strings.
• \n : Newline
• \t : Tab
• \\ : Backslash
• \" : Double quote
• \' : Single quote
• Example:
print("Hello\nWorld!")
print("This is a tab\tcharacter.")
Print Statement
• The print() function is used to display output.
• You can use sep and end parameters to customize the output.
Operators in Python
Types of Operators
1. Arithmetic Operators:
2. Example:
print(10 + 5) # Output: 15
print(10 ** 2) # Output: 100
2. Comparison Operators:
1. == (Equal), != (Not Equal), > (Greater Than), < (Less Than), >=
(Greater Than or Equal), <= (Less Than or Equal).
2. Example:
3. Logical Operators:
1. and , or , not .
2. Example:
1. = , += , -= , *= , /= , %= , **= , //= .
2. Example:
x = 10
x += 5 # Equivalent to x = x + 5
print(x) # Output: 15
5. Membership Operators:
1. in , not in .
2. Example:
6. Identity Operators:
1. is , is not .
2. Example:
x = 10
y = 10
print(x is y) # Output: True
Summary