



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Variables are used to store data that can be used and manipulated in a program.
A variable is created when you assign a value to it using the = operator.
Example:
Variable names can contain letters, numbers, and underscores. Variable names must start with a letter or underscore. Variable names are case-sensitive. Avoid using Python keywords as variable names (e.g., print , if , else ).
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 ).
name = "Alice" age = 25 height = 5.
Python supports several built-in data types:
Integers ( int ): Whole numbers (e.g., 10 , -5 ). Floats ( float ): Decimal numbers (e.g., 3.14 , -0.001 ). Strings ( str ): Text data enclosed in quotes (e.g., "Hello" , 'Python' ). Booleans ( bool ): Represents True or False. Lists: Ordered, mutable collections (e.g., [1, 2, 3] ). Tuples: Ordered, immutable collections (e.g., (1, 2, 3) ). Sets: Unordered collections of unique elements (e.g., {1, 2, 3} ). Dictionaries: Key-value pairs (e.g., {"name": "Alice", "age": 25} ).
Use the type() function to check the data type of a variable.
Typecasting in Python
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.
print(type(10)) # Output: print(type("Hello")) # Output:
Comments, Escape Sequences & Print Statement
Comments are used to explain code and are ignored by the Python interpreter.
Single-line comments start with #.
Multi-line comments are enclosed in ''' or """.
Escape sequences are used to include special characters in strings.
Common escape sequences:
\n : Newline \t : Tab \ : Backslash " : Double quote ' : Single quote
Example:
The print() function is used to display output.
''' This is a multi-line comment '''
print("Hello\nWorld!") print("This is a tab\tcharacter.")
You can use sep and end parameters to customize the output.
Operators in Python
Arithmetic Operators:
Example:
Comparison Operators:
== (Equal), != (Not Equal), > (Greater Than), < (Less Than), >= (Greater Than or Equal), <= (Less Than or Equal).
Example:
Logical Operators:
and , or , not.
Example:
print("Hello", "World", sep=", ", end="!\n")
print(10 + 5) # Output: 15 print(10 ** 2) # Output: 100
print(10 > 5) # Output: True print(10 == 5) # Output: False
print(True and False) # Output: False print(True or False) # Output: True print(not True) # Output: False