Fundamentals Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

(Assignment)

“Fundamentals_of_Programming”
1. Variable
In programming, a variable is a symbolic name or identifier that represents a value or a
storage location in the computer's memory. Variables are used to store and manipulate data in a
program. They serve as placeholders for values that can change during the execution of the
program. Variables are containers for storing data values.
Here's an example in Python that demonstrates the use of a variable:

In this example, "name" is a variable that holds a string value, "John." The program uses
the variable to display a personalized greeting.
Variables are fundamental in programming as they allow developers to work with and
manipulate data dynamically. They enable the storage of information, calculation of values, and
interaction with user inputs and external data sources, making them a crucial concept in virtually
every programming language.

2. Data Types in Python


Python supports a variety of data types that are used for different purposes. Here are
some common data types in Python and explanations of what each data type is used for:

 Integer (int):
Purpose: Integers are used to represent whole numbers without decimal points. They can be
positive, negative, or zero.
Example: 42, -10, 0
 Floating-Point (float):

Purpose: Floating-point numbers are used to represent real numbers with decimal points. They
can also represent very large or very small numbers using scientific notation.
Example: 3.14, -0.005, 2.5e-5 (scientific notation)
 String (str):
Purpose: Strings are sequences of characters and are used to represent text. They can contain
letters, numbers, symbols, and spaces.
Example: "Hello, World!", 'Python', "123"
 Boolean (bool):

Purpose: Booleans represent binary values - either True or False. They are used for making
logical decisions and comparisons.
Example: True, False
 List:
Purpose: Lists are ordered collections of items. They can store multiple values of different data
types and are mutable (i.e., you can change their contents).
Example: [1, 2, 3], ['apple', 'banana', 'cherry']
 Tuple:

Purpose: Tuples are similar to lists but are immutable (cannot be changed after creation). They
are often used to represent fixed collections of items.
Example: (1, 2, 3), ('red', 'green', 'blue')
 Dictionary (dict):
Purpose: Dictionaries are unordered collections of key-value pairs. They are used to store and
retrieve data using a unique key.
Example: {'name': 'Alice', 'age': 30, 'city': 'New York'}
 Set:
Purpose: Sets are unordered collections of unique elements. They are used for tasks like
removing duplicates from a list or testing membership.
Example: {1, 2, 3}, {'apple', 'banana', 'cherry'}
 NoneType (None):
Purpose: None represents the absence of a value or the lack of a specific data type. It is often
used to initialize variables or indicate the absence of a return value in functions.
 Complex (complex):

Purpose: Complex numbers are used to represent numbers with both real and imaginary parts.
They are commonly used in scientific and engineering calculations.
Example: 3 + 2j, -1 - 4j

These are some of the most common data types in Python, and they form the building
blocks for representing and manipulating data in Python programs. Understanding and using the
appropriate data types is essential for effective programming in Python.

3. Input and output command/syntax in Python


Input and output (I/O) operations are fundamental in programming as they allow you to
interact with users, read external data, and display results. In Python, you can perform I/O using
various functions and methods. Here's an overview of input and output in Python:
Input:

 Taking User Input:

To receive input from the user, you can use the ‘input()’ function. It takes a single argument,
which is a string that serves as a prompt to the user. The user's input is read as a string.

 Converting Input to Other Data Types:

Since ‘input()’ returns a string, you may need to convert it to other data types (e.g., integers or
floats) if you're expecting numerical input.

Output:

 Displaying Output:

You can use the ‘print()’ function to display output in Python. You can pass one or more values to
‘print()’, and it will display them as strings.

 Formatting Output:

Python provides several methods for formatting output. Here are a few examples:
Using the ‘format()’ method:
Using f-strings (formatted string literals) in Python 3.6 and later:

 Writing to Files:

Python allows you to write data to files using the ‘open()’ function and various file-writing
methods. For example, you can use the ‘write()’ method to write data to a text file.

 Reading from Files:

You can read data from files using the ‘open()’ function and file-reading methods. The ‘read()’
method reads the entire content of a file.

 Standard Input and Output:

Python interacts with the standard input (usually the keyboard) and standard output (usually the
terminal/console) by default. You can use ‘input()’ to read from the standard input and ‘print()’ to
write to the standard output.
These are the basic input and output operations in Python. They allow you to create
interactive programs, read and write data to files, and format output to suit your needs. Python's
simplicity and flexibility make it easy to work with I/O operations for various tasks.

4. Operators in Python
Python supports a variety of operators that allow you to perform different types of
operations on data. Here's a list of common operators in Python:

 Arithmetic Operators:

o ‘+’ (Addition): Adds two numbers.


o ‘-' (Subtraction): Subtracts the right operand from the left operand.
o ‘*’ (Multiplication): Multiplies two numbers.
o ‘/’ (Division): Divides the left operand by the right operand.
o ‘%’ (Modulus): Returns the remainder of the division.
o ‘**’ (Exponentiation): Raises the left operand to the power of the right operand.
o ‘//’ (Floor Division): Returns the integer result of division, discarding the
remainder.

 Comparison Operators:

o ‘==’ (Equal): Checks if two values are equal.


o ‘!=’ (Not Equal): Checks if two values are not equal.
o ‘<’ (Less Than): Checks if the left operand is less than the right operand.
o ‘>’ (Greater Than): Checks if the left operand is greater than the right operand.
o ‘<=’ (Less Than or Equal To): Checks if the left operand is less than or equal to
the right operand.
o ‘>=’ (Greater Than or Equal To): Checks if the left operand is greater than or
equal to the right operand.

 Logical Operators:

o ‘and’ (Logical AND): Returns ‘True’ if both operands are ‘True’.


o ‘or’ (Logical OR): Returns ‘True’ if at least one of the operands is ‘True’.
o ‘not’ (Logical NOT): Returns the opposite boolean value of the operand.

 Assignment Operators:
o ‘=’ (Assignment): Assigns a value to a variable.
o ‘+=’ (Add and Assign): Adds the right operand to the variable and assigns the
result to the variable.
o ‘-=’ (Subtract and Assign): Subtracts the right operand from the variable and
assigns the result to the variable.
o ‘*=’ (Multiply and Assign): Multiplies the variable by the right operand and
assigns the result to the variable.
o ‘/=’ (Divide and Assign): Divides the variable by the right operand and assigns the
result to the variable.
o ‘%=’ (Modulus and Assign): Calculates the modulus of the variable and assigns
the result to the variable.
o ‘**=’ (Exponentiate and Assign): Raises the variable to the power of the right
operand and assigns the result to the variable.
o ‘//=’ (Floor Divide and Assign): Performs floor division on the variable and
assigns the integer result to the variable.

 Identity Operators:

o ‘is’ : Returns ‘True’ if both operands refer to the same object.


o ‘is not’ : Returns ‘True’ if both operands do not refer to the same object.

 Membership Operators:

o ‘in’ : Returns ‘True’ if the left operand is found in the right operand (e.g., used for
checking membership in lists, tuples, strings, etc.).
o ‘not in’ : Returns ‘True’ if the left operand is not found in the right operand.

 Bitwise Operators:

o ‘&’ (Bitwise AND)


o ‘|’ (Bitwise OR)
o ‘^’ (Bitwise XOR)
o ‘~’ (Bitwise NOT)
o ‘<<’ (Left Shift)
o ‘>>’ (Right Shift)

These operators are fundamental for performing mathematical calculations, making


comparisons, controlling program flow, and manipulating data in Python programs. Depending
on the context and the data types involved, different operators are used to achieve specific tasks.

You might also like