0% found this document useful (0 votes)
16 views8 pages

Python Unit-1 Part Ii-1

Total python ganesh notes easy way all topics coverd

Uploaded by

G N Sai Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Python Unit-1 Part Ii-1

Total python ganesh notes easy way all topics coverd

Uploaded by

G N Sai Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Comments in python:

 Including comments in programs makes code more readable for humans as it


Unit-1 Part- 2 Introduction to Python

provides some information or explanation about what each part of a program


is doing.
 Depending on the purpose of your program, comments can serve as notes to
yourself or reminders, or they can be written with the intention of other
programmers being able to understand what your code is doing.
 In general, it is a good idea to write comments while you are writing or
updating a program as it is easy to forget your thought process later on, and
comments written later may be less useful in the long term.
 Comments in Python begin with a hash mark (#) and whitespace character and
continue to the end of the line.

# This is a comment

 In a “Hello, World!” program, a comment may look like this:

# Print “Hello, World!” to console


print("Hello, World!")

 Inline comments occur on the same line of a statement, following the code
itself. Like other comments, they begin with a hash mark and a single
whitespace character.
 Generally, inline comments look like this:

[code] # Inline comment about the code

 Example:
z = 2.5 + 3j # Create a complex number

 Multiline comments in python can be placed in between triple single quotes ‘’’ or triple
double quotes “””
 Example:

2
Unit-1 Part- 2 Introduction to Python

Data types in python:


Python has following standard Data Types:
Data
Description Nature Example
type
i=25

Integer, is a whole j=234456778800


number, positive or type(i)#<class ‘int’>
Immutable
negative, without
type(j)#<class ‘int’>
decimals, of unlimited (un-
int
length. changeable) print(i) #25
print(j) # 234456778800

“Floating point
f=23.456
number" is a number,
positive or negative, type(f) #<class ‘float’>
float containing one or Immutable
print(f)
more decimals.
c1=20+50.25j
Complex numbers
type(c1) #<class ‘complex’>
Form: x+yj
print(c1) #20+50.25j
x: real part
complex Immutable print(c1.real)# 20
y: imaginary part print(c1.imag) # 50.25

Strings: combination of
characters. s1=’hello’
Each character in string s2=”HELLO”
can be accessed by
s3=’’’hello python’’’
index. Immutable
type(s1)# <class ‘str’>
Positive index starts
str from ‘0’(left to right). print(s2) # HELLO
print(s3[2])# l
Negative index starts
from’-1’(right to left).
b1=True
b2=False
bool Boolean: True or False Immutable
type(b1)#<class ‘bool’>
print(b2)# False
List is a sequence of lst=[1,2,3,4,5]
ordered homogeneous type(lst)# <class ‘list’>
elements. Symbol: [ ]
list Mutable print(lst)# [1,2,3,4,5]
Supports indexing print(lst[2])# 3
(positive, negative). print(lst[-2])# 4

3
Unit-1 Part- 2 Introduction to Python

tuple is a sequence of
ordered heterogeneous tup=(10,’hello’,True)
objects.
type(tup)# <class ‘tuple’>
Symbol: ( ) Immutable
print(tup[0]) #10
tuple Supports indexing print(tup[-2]) #hello
(positive, negative).

Set is a collection of un- vowels={‘a’,’e’,’i’,’o’,’u’}


ordered elements. type(vowels)# <class ‘set’>
Mutable
set Symbol: { } print(vowels)
Discards the duplicates. # {‘a’,’e’,’i’,’o’,’u’}
d={1:’a’,2:’b’,3:’c’}
Dictionaries are used
for mapping (key,value) Mutable type(d) # <class ‘dict’>
dict pairs. print(d) #{1:’a’,2:’b’,3:’c’}

************************************************************

Type Conversion functions:


 Python defines type conversion functions to directly convert one data type to
another which is useful in day to day and competitive programming.
 Following are some conversion functions.
int(a, base) : This function converts any data type to integer. ‘Base’ specifies the base in
which string is if data type is string.
float() : This function is used to convert any data type to a floating point number

Program:
.
s = "10010"
c = int(s,2)
print ("After converting to integer base 2 : ",c)
e = float(s)
print ("After converting to float : ",e)

Output:
After converting to integer base 2 : 18
After converting to float : 10010.0

Integer convert into float


4
Unit-1 Part- 2 Introduction to Python

Program:
a=10
print(a)
print(type(a))
b=float(a)
print(b)
print(type(b))

output:
10
<class 'int'>
10.0
<class 'float'>

Float convert into integer

Program:
a=10.5
print(a)
print(type(a))
b=int(a)
print(b)
print(type(b))

output:
10.5
<class 'float'>
10
<class 'int'>

String convert into integer

Program:
a='11'
print(a)
print(type(a))
b=int(a)
print(b)
print(type(b))

output:
11
5
Unit-1 Part- 2 Introduction to Python

<class 'str'>
11
<class 'int'>

Simple Input and Output functions:


 input() and print() are widely used for standard input and output operations
respectively.
 We use the print() function to output data to the standard output device (screen).

 In the second print() statement, we can notice that a space was added between
the string and the value of variable a.This is by default, but we can change it.
 The actual syntax of the print() function is

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

 Here, objects is the value(s) to be printed.


 The sep separator is used between the values. It defaults into a space character.
 After all values are printed, end is printed. It defaults into a new line.
 The file is the object where the values are printed and its default value
is sys.stdout(screen). Here are an example to illustrate this.

 In Python, we have the input() function to allow this. The syntax for input() is

a=int(input("Enter the a value :"))


b=int(input("Enter the b value :"))
c=a+b
print(c)
output:
Enter the a value :2021
Enter the b value :1000
3021

6
Unit-1 Part- 2 Introduction to Python

7
Unit-1 Part- 2 Introduction to Python

You might also like