Py Unit1
Py Unit1
Example
# Taking user input
name = input("Enter your name: ")
print("Hello, " + name + "!")
# Converting input to integer
age = int(input("Enter your age: "))
print("You will be", age + 1, "years old next
year.")
Single-Line Comments:
An Integrated Development Environment (IDE) is a software Created with #.
suite that streamlines the software development process. Used for short explanations on the same line.
Syntax :
# Single-line comment
Multi-Line (Block) Comments:
Achieved with triple-quoted strings.
Used for longer explanations spanning multiple lines.
Syntax :
'''
Multi-line comment
'''
Best Practices:
Provide clarity and context.
Keep comments concise and up-to-date.
Avoid redundancy with self-explanatory code.
Example :
result = calculate_total(price, quantity) # Calculate total cost
Examples:
Reserved keywords in a programming language are words # Implicit conversion
that have special meanings and cannot be used as identifiers result = 10 + 3.14 # int implicitly converted
(names for variables, functions, etc.)
to float for addition
because they are reserved for specific purposes in the
language. # Explicit conversion
num_float = 5.5
num_int = int(num_float) # Converts float to
integer
str_num = str(42)
data types represent the type of data that a variable can hold.
Here are some common data types in Python: A combination of symbols that yields a value.
Integer (int): Comprises variables, operators, values, sub-expressions, and
Represents whole numbers without decimal points.
reserved keywords.
Example: x = 5
When entered in the command line, expressions are
Float (float): evaluated by the interpreter, producing a result.
Represents numbers with decimal points or in exponential
arithmetic expressions are evaluating to a numeric type are
form. termed arithmetic expressions.
Example: y = 3.14
Sub-expressions are parts of larger expressions, enclosed in
String (str): parentheses.
Represents text or sequences of characters.
Example: 4 + (3 * k)
Example: name = "John"
Individual Expressions: A single literal or variable can also be
Boolean (bool): considered an expression (e.g., 4, 3, k).
Represents either True or False.
Hierarchy of Sub-Expressions: Expressions may have
Example: is_valid = True
multiple levels of sub-expressions, forming a hierarchy.
List (list): Example: a + (b * (c - d)) has sub-expressions 3 and k.
Represents an ordered collection of elements.
Example: numbers = [1, 2, 3]
Tuple (tuple): Used for creating, assigning values to, and modifying
Similar to a list but immutable (cannot be changed after variables.
creation). Syntax:
Example: coordinates = (4, 5) Variable = Expression represents the assignment statement.
Set (set):
Represents an unordered collection of unique elements.
Value-based expression on RHS.
Example: unique_numbers = {1, 2, 3}
x = 5 + 3 # Assign the result of the expression (5 + 3) to the
Dictionary (dict):
variable x
Represents a collection of key-value pairs.
Current variable on RHS.
Example: person = {'name': 'Alice', 'age': 25}
y=2
NoneType (None):
y = y + 1 # Increment the value of the variable y by 1
Represents the absence of a value or a null value.
Operation on RHS.
Example: result = None
a = 10
Complex (complex):
b=2
Represents complex numbers with a real and an imaginary
c = a / b # Assign the result of the division operation (a / b)
part.
to the variable c
Example: z = 3 + 4j
9. Write a Python program that takes user input for their name,
age, and favorite color. Print a formatted output using this
information.