0% found this document useful (0 votes)
49 views17 pages

2.fundamentals of Python

Python uses character sets like ASCII and Unicode to define valid characters that can be used in programs. Identifiers are names given to variables, functions, and classes that must start with a letter and can contain letters, numbers, and underscores. Variables reference values in memory and are assigned using the = operator. Python supports different data types like integers, floats, strings, and more. Expressions involve values, variables, and operators to evaluate to a value. Input and output statements are used to provide data to programs and display results.

Uploaded by

Rohith Bellapu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
49 views17 pages

2.fundamentals of Python

Python uses character sets like ASCII and Unicode to define valid characters that can be used in programs. Identifiers are names given to variables, functions, and classes that must start with a letter and can contain letters, numbers, and underscores. Variables reference values in memory and are assigned using the = operator. Python supports different data types like integers, floats, strings, and more. Expressions involve values, variables, and operators to evaluate to a value. Input and output statements are used to provide data to programs and display results.

Uploaded by

Rohith Bellapu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

FUNDAMENTAL

CONCEPTS
IN PYTHON
•Character set defines the valid characters that can be used in a
source program .Python uses the character set as the building block
to form the basic program elements such as variables, identifiers
,constants, expressions etc.
•Python uses the traditional ASCII character set. The latest version also
CHARACTER SET recognizes the Unicode character set.
•The ASCII character set is a subset of the Unicode character set. The
main limitation of ASCII character is its inability to represent more
than 256(=28).
•Unicode character set, a set that use 2 bytes (16 bits) per character..
❑Identifier is the name given to entities like class, functions, variables
etc. in Python. It helps differentiating one entity from another.
❑Rules for writing identifiers:
▪Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore (_).
▪ Names like my Class, var_1 and print_this_to_screen, all are
valid example.

IDENTIFIERS ▪An identifier must start with a letter or an underscore.


▪ It cannot start with a digit.
▪variable and _variable are valid identifiers but 1variable is
invalid.
▪An identifier cannot be a keyword .Keywords, also called reserved
words, have special meanings in Python. For example, import is a
keyword, which tells the Python interpreter to import a module to the
program.
▪An identifier can be of any length.
KEYWORDS False class finally is return

Keywords are the reserved words None continue for lambda try
in Python. Keywords cannot be
used as variable name, function True def from nonlocal while
name or any other identifier. They
are used to define the syntax and
structure of the Python language. In and del global not with
Python, keywords are case
sensitive. as elif if or yield
There are 33 keywords in Python.
assert else import pass
All the keywords except True, False
and None are in lowercase and
they must be written as it is. break except in raise
VARIABLES • We need not to declare explicitly variable in Python as
we do in c, c++, and java.
Variables are used to reference
values that may be changed in the int a, b;
program. float c, d;
When we assign any value to the variable that variable is
Variable is a name of the memory declared automatically. The statement for assigning a
location where data is stored. Once value to a variable is called an assignment statement. In
a variable is stored that means a Python, the equal sign (=) is used as the assignment
space is allocated in memory.
operator. The syntax for assignment statements is as
follows:

Variable = expression
DATA TYPE There are various data types in Python. Some of the important
types are listed below.
A data type is a set of values, and Numbers
a set of operators that may be Integers, floating point numbers and complex numbers falls
applied to those values. under Python numbers category. They are defined as int, float
For example, the integer data and complex class in Python.
type consists of the set of integers, a) Integer (signed) -Numbers (can be both positive and
and operators for addition, negative) with no fractional part.e.g. 100
subtraction, multiplication, and b) Float pointing - Real numbers with both integer and
division, among others. Integers, fractional part e.g. 100.90
floats, complex numbers and strings c) Complex - In the form of a+bj where a forms the real part
are part of a set of predefined and b forms the imaginary part of complex number. e.g. 3+4j
data types in Python called the
built-in types. Strings
String is sequence of Unicode characters. We can use single
quotes or double quotes to represent strings.
s = "This is a string"
EXPRESSION y=1 # Assign 1 to variable y

Radius = 1.0 # Assign 1.0 to variable Radius


An expression represents a
computation involving values, x = 5 * (3 / 2) + 3 * 2 # Assign the value of the
variables, and operators that, expression to x
taken together, evaluate to a value.
You can use a variable in an x=y+1 # Assign the addition of y and
expression. A variable can also be 1 to x
used in both sides of the =
operator. area = radius * radius * 3.14159 # Compute
For example, consider the area
following code:
SIMULTANEOUS Consider two variables: x and y. How do you write the
ASSIGNMENT code to swap their values? A common approach is to
introduce a temporary variable as follows:
Python also supports simultaneous x=1
assignment in syntax like this: y=2
temp = x # Save x in a temp variable
var1, var2, var3 = exp1, exp2, exp3 x=y # Assign the value in y to x
It tells Python to evaluate all the y = temp # Assign the value in temp to y
expressions on the right and assign them
to the corresponding variable on the left But you can simplify the task using the following statement
simultaneously. Swapping variable values to swap the values of x and y.
is a common operation in programming
and simultaneous assignment is very x, y = y, x # Swap x with y
useful to perform this operation.
MIXED TYPE Coercion vs. Type Conversion

EXPRESSION Coercion is the implicit (automatic) conversion of operands to a


common type. Coercion is automatically performed on mixed-type
A mixed-type expression is an expression expressions only if the operands can be safely converted, that is, if
containing operands of different type. no loss of information will result. The conversion of integer 2 to
The CPU can only perform operations on floating-point 2.0 below is a safe conversion—the conversion of
values with the same internal 4.5 to integer 4 is not, since the decimal digit would be lost,
representation scheme, and thus only on a=2
operands of the same type. b=4.5
c=a + b
Operands of mixed-type expressions
therefore must be converted to a common 2 + 4.5 ➝ 2.0 + 4.5 ➝ 6.5
type. Values can be converted in one of (automatic conversion of int to float)
two ways—by implicit (automatic)
conversion, called coercion, or by explicit Type conversion is the explicit conversion of operands to a
type conversion specific type. Type conversion can be applied even if loss of
information results.
2 + int (4.5) ➝ 2 + 4 ➝ 6
COMMENTS
Python, comments are preceded
by a pound sign (#) on a line,
called a line comment, or enclosed
between three consecutive single # This program displays Welcome to Python
quotation marks (''') on one or
several lines, called a paragraph ''' This program displays Welcome to Python and
comment. Python is fun
When the Python interpreter sees '''
#, it ignores all text after # on the
same line. When it sees ''', it scans
for the next ''' and ignores any text
between the triple quotation marks.
INDENTATION
Indentation matters in Python. Note
that the statements are entered from
the first column in the new line. The
Python interpreter will report an
error if the program is typed as
follows:
# Display two messages
print("Welcome to Python")
print("Python is fun")
INPUT AND OUTPUT
The purpose of a computer is to process
data and return results. It means that first
of all, we should provide data to the
computer.
The data given to the computer is called Processing the
input. The results returned by the computer INPUT input with some OUTPUT
are called output. logic
So, we can say that a computer takes
input, processes that input and produces the
output.
To provide input to a computer, Python
provides some statements which are called
Input statements. Similarly, to display the
output, there are Output statements
available in Python. We should use some
logic to convert the input into output.
print('Hello')
Hello
OUTPUT STATEMENT
print("This is the \n first line")
This is the
first line
To display output or results,
Python provides the print()
function. a, b = 2, 4
print(a)
2
print(a, b)
24
INPUT STATEMENT str = input()
print(str )
#this will wait till we enter a string

Raj kumar #enter this string

To accept input from keyboard, Python Raj kumar # output


provides the input() function.
This function takes a value from the
keyboard and returns it as a string.
str = input('Enter your name:')
print(str)

Enter your name: Raj kumar # input


Raj kumar # output
PRACTICE QUESTION 1
(a)14
What will be the output of the
code? (b)”12” + 2
(c) “12” + “2”
x= input("enter the value") (d) Error
# Let x= 12

y=x+2
print(y)
INPUT STATEMENT

str = input('Enter a number:') x = int(input('Enter a number:'))

x = int(str) #str is converted into int print(x)

print(x) Enter a number: 125

Enter a number: 125 125

125
PRACTICE QUESTION 2 a) Keywords are reserved words that are used to
construct instructions.
b) Keywords are used to calculate mathematical
What are keywords operations.
c) Keywords are used to print messages like "Hello
in Python? World!" to the screen.
d) Keywords are the words that we need to memorize to
program in Python.

You might also like