0% found this document useful (0 votes)
55 views5 pages

Python Programming Fundamentals Guide

The document provides an overview of Python fundamentals, including the character set, input function, and the importance of indentation. It explains tokens, keywords, identifiers, literals, operators, and the structure of a Python program, including variables and their types. Additionally, it covers functions like print(), type(), id(), chr(), and ord(), along with concepts of variable scope and dynamic typing.

Uploaded by

Aditya Ak
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)
55 views5 pages

Python Programming Fundamentals Guide

The document provides an overview of Python fundamentals, including the character set, input function, and the importance of indentation. It explains tokens, keywords, identifiers, literals, operators, and the structure of a Python program, including variables and their types. Additionally, it covers functions like print(), type(), id(), chr(), and ord(), along with concepts of variable scope and dynamic typing.

Uploaded by

Aditya Ak
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

Python Fundamentals

Python Character Set :A set of valid characters recognized by python.


input() Function In Python allows a user to give input to a program from a
keyboard but returns the value accordingly.
e.g.
age = int(input(‘enter your age’))
C = age+2 #will not produce any error
NOTE : input() function always enter string value in python [Link] on need
int(),float() function can be used for data conversion
Indentation refers to the spaces applied at the beginning of a code line. In other
programming languages the indentation in code is for readability only, where as
the indentation in Python is very important. Python uses indentation to indicate
a block of code or used in block of codes.
E.g.1
if 3 > 2:
print(“Three is greater than two!") //syntax error due to not indented
E.g.2
if 3 > 2:
print(“Three is greater than two!") //indented so no error
Token
Smallest individual unit in a program is known as token/lexical unit
1. Keywords
2. Identifiers-
3. Literals
4. Operators
5. Punctuators/Delimiters
Keywords:Reserve word of the compiler/interpreter which can’t be used as
identifier.
Identifiers:A Python identifier is a name used to identify a variable, function,
class, module or other object.
* An identifier starts with a letter A to Z or a to z or an underscore (_) followed
by zero or more letters, underscores and digits (0 to 9).
* An identifier can contain digits but cannot begin with a digit
* Python does not allow special characters except underscore
* Identifier must not be a keyword of Python.
* Python is a case sensitive programming language.
Thus, Rollnumber and rollnumber are two different identifiers in Python.
Some valid identifiers : Mybook, file123, z2td, date_2, _no
Literals:In Python can be defined as number, text, or other data that represent
values to be stored in variables.
Example of String Literals in Python
name = ‘Johni’
fname =“johny”
Example of Integer Literals in Python(numeric literal)
age = 22
Example of Float Literals in Python(numeric literal)
height = 6.2
Example of Special Literals in Python
name = None
Operators:can be defined as symbols that are used to perform operations on
operands.
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
Punctuators/Delimiters:Used to implement the grammatical and structure of a
[Link] are the python punctuators.

Barebone of a python program


a. Expression : - which is evaluated and produce result. E.g. (20 + 4) / 4
b. Statement :- instruction that does something.
e.g
a = 20
print("Calling in proper sequence")
c. Comments : which is readable for programmer but ignored by python
interpreter
i. Single line comment: Which begins with # sign.
[Link] comment is a comment on the same line as a statement
iii. Multi line comment (docstring): either write multiple line beginning with #
sign or use triple quoted multiple line. E.g.
‘’’this is my first
python multiline comment‘’’
d. Function:a code that has some name and it can be reused.e.g. keyArgFunc in
above program
d. Block & indentation : group of statements is block/[Link] at same
level create a block
.e.g. all 3 statement of keyArgFunc function
Variable is a name given to a memory location. A variable can consider as a
container which holds value. Python is a type infer language that means you
don't need to specify the datatype of [Link] automatically get variable
datatype depending upon the value assigned to the variable.
Assigning Values To Variable
name = ‘python' # String Data Type
sum = None # a variable without value
a = 23 # Integer
b = 6.2 # Float
sum = a + b
print (sum)
Multiple Assignment: assign a single value to many variables
a = b = c = 1 # single value to multiple variable
a,b = 1,2 # multiple value to multiple variable
a,b = b,a # value of a and b is swaped
Variable Scope And Lifetime in Python Program
Concept of L Value and R Value in variable
Lvalue and Rvalue refer to the left and right side of the assignment operator.
The Lvalue (pronounced: L value) concept refers to the requirement that the
operand on the left side of the assignment operator is modifiable, usually a
variable. Rvalue concept fetches the value of the expression or operand on the
right side of the assignment operator.
example:
amount = 390
The value 390 is pulled or fetched (Rvalue) and stored into the variable named
amount (Lvalue); destroying the value previously stored in that variable.
Dynamic typing
Data type of a variable depend/change upon the value assigned to a variable on
each next statement.
X = 25 # integer type
X = “python” # x variable data type change to string on just next line
Now programmer should be aware that not to write like this:
Y = X / 5 # error !! String cannot be divided
print() Function In Python is used to print output on the screen.
Syntax of Print Function - print(expression/variable)
print(122) print('hello India')
Output :- Output :-
122 hello India

print(‘Computer',‘Science') x=10
print(‘Computer',‘Science',sep=' & ') y=20
print(‘Computer',‘Science',sep=' & z=x*y
',end='$') print(x,"*",y,"=",z)
Output :- Output :-
Computer Science 10 * 20 = 200
Computer & Science
Computer & Science$
type()- type(obj) is passed, it returns the type of the given object
x = 10
print(type(x))
<class 'int'>

id() function is used to return a unique identification value of the object stored
in the memory.
a = 10
print('ID of a =', id(a))
ID of a = 9781088

chr() function returns the character that represents the specified unicode.
chr(65) returns 'A'

ord() function in Python is used to convert a single Unicode character into its
integer representation, i.e., it takes a single string character as an input and
returns an integer (representing the Unicode equivalent of the character) as an
output.
ord('A')returns 65

Common questions

Powered by AI

In Python, indentation is crucial because it is used to define the structure and blocks of code, rather than just for readability . This is different from many other programming languages where indentation is often ignored by the compiler/interpreter. Improper indentation in Python will lead to syntax errors, whereas, in other languages, it may merely affect code readability without causing errors . Proper indentation ensures that statements that need to be grouped together are recognized as part of a block (or suite).

In Python, tokens are the smallest individual units in a program, serving as the basic building blocks for writing code . Tokens in Python are categorized into several types: Keywords, which are reserved words that cannot be used as identifiers; Identifiers, which are names given to variables, functions, etc.; Literals, representing fixed values; Operators, used to perform operations on operands; and Punctuators/Delimiters, which implement syntax structures . Each type of token has a specific syntactic role in Python programming.

The input() function in Python is used for taking input from the user. In Python 3, input() always returns the user input as a string, regardless of what is entered . This behavior means that if a numerical input is required, explicit conversion using functions like int() or float() is necessary to prevent type errors when performing numerical operations . This ensures that the program can handle user input appropriately, considering Python's dynamic typing and type conversion requirements.

In Python, Lvalue and Rvalue refer to the left and right sides of an assignment statement respectively. Lvalue denotes a modifiable location (typically a variable) where the Rvalue can be stored. Rvalue is the actual data value or the result of an evaluated expression to be assigned to the Lvalue . Understanding the difference is crucial in assignment operations, as only Lvalues can appear on the left side of an assignment, whereas Rvalues are evaluated and fetched for assignment . This distinction ensures that values are correctly stored and accessed during program execution.

Python is a dynamically typed language, which means that variable types are determined at runtime based on the value assigned to them, rather than being declared explicitly by the programmer . This allows for flexibility and quicker development since programmers do not need to specify data types. However, it also requires careful handling of variables during operations, as demonstrated by the potential error of performing arithmetic on values of incompatible types, e.g., attempting to divide a string by a number will result in an error .

The type() function in Python enhances debugging by allowing developers to ascertain the data type of a given object, which can help in understanding program behavior and debugging type-related errors . The id() function provides the unique memory address of an object, enabling developers to verify object identity and track how objects with the same value might be stored independently in memory. These functions aid in debugging by allowing insight into the structure and storage of data within a program .

Python manages variable scope and lifetime based on their position within the hierarchy of a program—global scope for globally declared variables and local scope for variables within function definitions . The lifetime of a variable is the time period during which the variable exists in memory. Global variables are accessible throughout the program, while local variables exist only during the execution of the function in which they are declared. This scoping mechanism impacts the program state by determining which variables are accessible at any given point, potentially affecting program functionality and memory usage .

The ord() function in Python is used to convert a single Unicode character into its integer representation, providing the Unicode code point for the given character . Conversely, the chr() function takes an integer (representing a Unicode code point) and returns the corresponding character . These functions are essential for tasks that require conversion between characters and their numerical representations, facilitating operations in contexts such as encryption, encoding, and character manipulations.

Basic operators in Python include arithmetic, relational, assignment, logical, bitwise, and membership operators—all functioning to perform operations on operands . Arithmetic operators execute basic mathematical operations, relational operators compare operands, and assignment operators assign values. Logical operators enable compound condition evaluation, bitwise operators manipulate data at the bit level, and membership operators verify element presence in collections. These operators are integral to computations, affecting operand interaction and the resulting values produced by expressions, ultimately shaping program logic and data processing .

Comments in Python are integral to enhancing code readability and maintainability, facilitating communication among developers about the intent and functionality of code segments. Python supports single-line comments and multi-line (docstring) comments . Although comments are ignored by the Python interpreter, they play a vital role in documenting code functionality and logic, making complex code easier to understand and modify. They do not affect code execution or functionality but significantly impact the maintainability of the codebase, particularly in collaborative development environments .

You might also like