The document provides an overview of key Python concepts including variables, data types, operators, formatting numbers, and taking user input. It explains that variables store values in memory and have naming rules. The main data types are int, float, and string, and numbers can be formatted using specifiers like .2f. Operators allow mathematical operations and comparisons, while input() reads user keyboard input as a string.
3. msbacademy.org
Output Statement in Python
print( ) : it display the output on the screen. print() function is
built in function of python.
ex: print(“hello world”)
out put: hello world
4. msbacademy.org
String and String Literals
String:
a sequence of characters that is used as data is called a string.
String Literals
When a string appears in the actual code of a program
it is called a string literal. In Python code, string literals must be
enclosed in quote marks.
6. msbacademy.org
Comments
Comments in Python start with the hash character, # , and
extend to the end of the physical line.
Example
# This program displays welcome to python programming
Print(“welcome to python programming”)
7. msbacademy.org
Variable
Variables are nothing but reserved memory locations to store
values. This means that when you create a variable you reserve
some space in memory.
Syntax : Variable=expression
salary=12000
.
8. msbacademy.org
Variable
Note:
You cannot use a variable until you have assigned a value to it
an error will occur if you try to perform an operation on a variable,
such as printing it, before it has been assigned a value.
9. msbacademy.org
Variable Naming Rules
Rules:
1. You cannot use one of Python’s key words as a variable
name.
2. A variable name cannot contain spaces.
3. The first character must be one of the letters a through z, A
through Z, or an underscore character (_).
4. After the first character you may use the letters a through z or
A through Z, the digitsb0 through 9, or underscores.
5. Uppercase and lowercase characters are distinct
11. msbacademy.org
Variable Reassignment
Variables are called “variable” because they can reference
different values while a program is running. When you assign a
value to a variable, the variable will reference that value until you
assign it a different value.
13. msbacademy.org
Numeric Data Types and Literals
Numeric Data types
1. Int (When an integer is stored in memory, it is classified as
an int )
2. Float (When a real is stored in memory, it is classified as
a float )
Literals
3.Str (When string literal is stored in memory, it is classified as
a string)
14. msbacademy.org
Reading Input from the Keyboard
Input(): Python’s built-in input function to read input from the
keyboard.
Syntax:
Variable=input()
Note
it returns string values
15. msbacademy.org
Reading Numbers with the input Function
Variable=int(input())
Ex: a=int(“enter a value”)
Variable=float(input())
Ex : avg=float(input())
24. msbacademy.org
Membership Operators
1. These operators test for membership in a sequence such as
lists, strings or tuples. There are two membership operators
that are used in Python. (in, not in). It gives the result based
on the variable present in specified sequence or string
25. msbacademy.org
Identity Operators
1. To compare the memory location of two objects, Identity
Operators are used. The two identify operators used in Python
are (is, is not).
26. msbacademy.org
Operators Precedence
Operators (Decreasing order
of precedence)
Meaning
** Exponent
*, /, //, % Multiplication, Division, Floor division,
Modulus
+, - Addition, Subtraction
<= < > >= Comparison operators
= %= /= //= -= += *= **= Assignment Operators
is ,is not Identity operators
in not in Membership operators
not or and Logical operators
27. msbacademy.org
Mixed-Type Expressions and Data Type Conversion
1. When an operation is performed on two int values, the result
will be an int.
2. When an operation is performed on two float values, the result
will be a float
3. When an operation is performed on an int and a float, the int
value will be temporarily converted to a float and the result of
the operation will be a float.
28. msbacademy.org
Escape Characters
Escape Character Effect
n
t
'
’’
Causes output to be advanced to the next
line
Causes output to skip over to the next
horizontal tab position.
Causes a single quote mark to be printed.
Causes a double quote mark to be printed.
Causes a backslash character to be printed.
29. msbacademy.org
Formatting Numbers
1. When a floating-point number is displayed by the print
statement, it can appear with up to 12 significant digits.
Ex: toat_marks = 100.0
avg_marks = toat_marks / 3.0
print('Average marks is',avg_marks)
Output: Average marks is 33.3333333333
31. msbacademy.org
Formatting in Scientific Notation
If you want to display floating-point numbers in scientific
notation, you can use the letter e or the letter E instead of f.
print(format(33.333333333333,’.1e’)
Output
3.3e+1
32. msbacademy.org
Inserting Comma Separators
1. If you want the number to be formatted with comma
separators, you can insert a comma into the format specifier.
print(format(33333333.3333,’,.2f)
output
33,333,333.33
33. msbacademy.org
Specifying a Minimum Field Width
1. The format specifier can also include a minimum field width,
which is the minimum number of spaces that should be used
to display the value.
Ex:
print('The number is', format(14586.6889, '12.2f'))
output
The number is 14586.68
34. msbacademy.org
Formatting Integers
1. You use ‘d’ as the type designator.
2. You cannot specify precision.
Ex :
Print('The number is', format(125465,’,d’))
output
The number is 1,25,465