0% found this document useful (0 votes)
24 views3 pages

Python Comments and Variable Basics

Comments help programmers understand a program's intent and functionality but are ignored by the Python interpreter. There are two types of comments in Python: single-line comments which start with # and end at the end of the line, and multi-line comments which use triple quotes (''' or """) and can span multiple lines. Variables in Python are named locations that refer to values and can be used and processed during a program's execution. Variables do not have fixed locations and their values can change. Variables are created when a value is first assigned to them and can change type after being set.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Python Comments and Variable Basics

Comments help programmers understand a program's intent and functionality but are ignored by the Python interpreter. There are two types of comments in Python: single-line comments which start with # and end at the end of the line, and multi-line comments which use triple quotes (''' or """) and can span multiple lines. Variables in Python are named locations that refer to values and can be used and processed during a program's execution. Variables do not have fixed locations and their values can change. Variables are created when a value is first assigned to them and can change type after being set.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

USE OF COMMENTS

Comments are descriptions that help programmers better understand the intent
and functionality of the program. They are completely ignored by the Python
interpreter. Comments in python begins with a # symbol.

There are two types of comments in python:


i. Single line comment
ii. Multi-line comment

[Link] line comment: This type of comments starts in a line and when a line
ends, it is automatically ends. Single line comment starts with # symbol.

Example: if a>b: # Relational operator compares two values

ii. Multi-Line comment: Multiline comments can be written in more than one
lines. Triple quoted ’’’ or ” ” ”) multi-line comments may be used in python. It
is also known as docstring.

Example:
’’’This program will calculate the average of 10 values.
First find the sum of 10 values
and divide the sum by number of values ’’’

VARIABLES

Named location that refers to a value and whose value can be used and
processed during program execution.
Variables in python do not have fixed locations. The location they refer to
changes every time their values change.

Creating a variable:
A variable is created the moment you first assign a value to it.
Example:
x=5
y = “hello”

Variables do not need to be declared with any particular type and can even
change type after they have been set. It is known as dynamic Typing.

x = 4 # x is of type int
x = "python" # x is now of type str
print(x)
Rules for Python variables:
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscore
(A-z, 0-9, _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables)

Python allows assign a single value to multiple variables.


Example: x = y = z = 5

You can also assign multiple values to multiple variables. For example −
x , y , z = 4, 5, “python”

4 is assigned to x, 5 is assigned to y and string “python” assigned to variable z


respectively.

x=12
y=14
x,y=y,x
print(x,y)

Now the result will be


14 12

Concept of l-value and r-value


r-value: expression that come on the rhs(right hand side) of an assignment.
l-value: expression that come on the lhs(left hand side) of an assignment.
Example
a=20 ( so a is l-value and 20 is r-value)
But 20=a is not a valid expression.

KNOWLEDGE OF DATA TYPES


Python has two data types -
1. Primitive Data Type (Numbers, String)
2. Collection Data Type (List, Tuple, Set, Dictionary)
1. Primitive Data Types:
a. Numbers: Number data types store numeric values.
There are three numeric types in Python:
• int example w=1
• float example y=2.8
• complex example z=1j

b. String: Sequence of characters represented in the quotation marks.


• Python allows for either pairs of single or double quotes. Example: 'hello'
is the same as "hello".
• Python does not have a character data type, a single character is simply a
string with a length of 1.
• The python string store Unicode characters.
• Each character in a string has its own index.
• String is immutable data type means it can never change its value in place.

2. Collection Data Type:


• List
• Tuple
• Set
• Dictionary

Mutable and Immutable data types


The python data types are broadly classified as Mutable and Immutable data
types. Difference between both is explained below:
S. Mutable data type Immutable data type
No.
1 Data types that can change their Data types that cannot change
value in place (in same memory their value in place (in same
address) memory address)
2 Examples: List, Dictionary, set Ex: Integers, float, Booleans,
strings, tuples
3 list1=[0,1,2,3] tuple1 = (0, 1, 2, 3)
list1[0]=4 #allows to change tuple1[0] = 4 # result into an
value error
print(list1) #output will be print(tuple1)
[4,1,2,3]

Common questions

Powered by AI

Primitive data types in Python include numbers and strings, characterized by storing single values and being immutable. Examples include integers, floats, and strings . Collection data types, like lists, tuples, sets, and dictionaries, can store multiple values or entities and could either be mutable or immutable. Lists and dictionaries, for instance, are mutable; tuples are immutable . This classification is important as it influences how data is stored, modified, and accessed in a program, aiding developers in choosing the appropriate data type based on their data manipulation needs.

The immutability of strings in Python means once a string is created, its content cannot be changed, which ensures more predictable behavior sharing references to the same string . This feature enhances performance by reducing the need for reallocation when using repeated strings and ensuring thread safety in concurrent executions because immutable objects don't change state. However, it also implies that transformations create new strings, potentially increasing memory usage and computational overhead during frequent modifications.

Python's variable naming rules, which include starting a variable name with a letter or underscore and disallowing certain symbols or starting with a number, ensure clarity and avoid parsing errors . These rules enhance code transparency by making variable intentions clearer and promoting consistent coding standards. For example, using meaningful names with proper naming conventions helps others understand the code better and reduces the likelihood of syntax errors. These rules also help in avoiding namespace clashes and unintended variable shadowing, contributing to error avoidance and better maintainability.

Mutable data types in Python are those that allow modification of their content without changing their identity. Examples include lists, dictionaries, and sets. For instance, modifying an element in a list using 'list1[0]=4' does not create a new list but changes the content in place . Immutable data types, such as integers, floats, booleans, strings, and tuples, do not allow modification of their content once created. Any change results in a new object altogether, and attempts to modify an existing object's content, like in 'tuple1[0] = 4', will result in an error .

In Python, variables can be reassigned to different types, illustrating dynamic typing. For instance, initially assigning 'x = 10' makes 'x' an integer. Reassigning 'x = "hello"' changes its type to a string . This reassignment does not require special declarations or type specifications . The impact is that variables can be reused for different data types without overhead, but it also requires developers to pay attention to potential type mismatch issues that could arise during runtime.

In Python, complex numbers are represented with a real part and an imaginary part, denoted by 'j'. For instance, 'z = 1j' represents a complex number where the real part is zero, and the imaginary part is one . Complex numbers are useful in fields such as engineering and scientific calculations where real and imaginary components are necessary, like in electrical engineering to simulate AC circuits, or in various algorithms involving Fourier transforms and quantum mechanics.

In Python, l-values refer to the expressions that appear on the left-hand side of an assignment statement, representing storage locations or variables that can receive a value. R-values are expressions found on the right-hand side, which are evaluated to produce values to be assigned to l-values . For example, in 'a = 20', 'a' is the l-value representing a location to store '20', and '20' is the r-value that provides the data . An expression like '20 = a' is invalid because a literal cannot serve as an l-value .

Comments in Python enhance understanding by explaining the intent and functionality of the code to other programmers or to remind oneself of specific details . Python supports two types of comments: single-line comments, which start with a '#' symbol, and multi-line comments, also known as docstrings, which are enclosed in triple quotes (''' or """). These comments are completely ignored by the interpreter and serve purely as annotations for developers.

Python is considered dynamically typed because variables do not have a fixed data type and their data type can change at runtime. This means that a variable's type is determined at the time of assignment and can change if the variable is assigned a new value of a different type . For example, a variable 'x' can initially hold an integer value and later be assigned a string, as shown in assignments like 'x = 4' and then 'x = "python"' . This allows for flexibility but requires careful handling to prevent type-related errors.

Python supports multiple assignments, allowing a single value to be assigned to multiple variables or multiple values to be assigned to multiple variables simultaneously. For example, 'x = y = z = 5' assigns the value 5 to all variables x, y, and z . Moreover, it can also handle the assignment of multiple values in one line, such as 'x, y, z = 4, 5, "python"', where x becomes 4, y becomes 5, and z becomes the string "python" . This flexibility facilitates more concise code, reduces redundancy, and allows easy value swapping or initialization of multiple variables.

You might also like