0% found this document useful (0 votes)
19 views

4, Python Basics

Uploaded by

Sunil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

4, Python Basics

Uploaded by

Sunil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Python Basics

Dr Sunil Kumar P V
Assistant Professor, SCOPE
Python – Why?
• Simple syntax

• Programs are clear and easy to read

• Has powerful programming features

• Companies and organizations that use Python include


YouTube, Google, Yahoo, and NASA.

• Python is well supported and freely available at


www.python.org.
Python – Evolution
• Guido van Rossum – Creator

• Released in the early 1990s.

• Its name comes from a 1970s British comedy


sketch television show called Monty Python’s
Flying Circus.
Python….Interactive mode
In interactive mode, you type Python programs and the
interpreter displays the result:
>>> 1 + 1
2

The shell prompt, >>>, is the prompt the interpreter uses to


indicate that it is ready. If you type 1 + 1, the interpreter
replies 2.
Python …. Shell
Python…. Script Mode
• Can also store code in a file and use the interpreter to
execute the contents of the file, which is called a script.

• Python scripts have names that end with .py.

• Interactive mode is convenient for testing small pieces of


code because you can type and execute them
immediately.

• But for anything more than a few lines, should save your
code as a script so you can modify and execute it in
future.
Python….Script Mode …
File name : first.py
print(4+3)
print(4-3)
print(4>3)
print("hello World")
Keywords
• Reserved words with predefined meaning
• Should be used to get that meaning only

• >>> and =10 will give error


Identifiers
• Names given to variables to store data needed for processing
• Naming rules:-
• Sequence of alphabets, digits or underscore ( _ ) can be used
• Must start with a character
• No space allowed
• Case sensitive, SUM is different from Sum and sum
• Examples
• Employee1, student1_mark, totalSales, profit2020
Identifier Naming
Numeric constants
• To represent numbers
• Examples
• 2020, 3.14, 10000, -6.51, -800, 0.4561
• Commas are not allowed
• 12,000 and 5,500 are wrong
• No decimal point Integers
• No decimal point Floating point numbers
String Constants
• Strings are sequence of characters, digits, symbols put in quotes
• Quotes can be single, double or triple (3 single quotes)
• “Andrew S. Tanenbaum”
• ‘Python is fun’
• ‘ ‘ ‘ COVID-19 is a pandemic’ ’ ’
• To print single quote use double quote and vice-versa
• “Let’s go!”
• ‘He told me “I am happy” and continued his job’
• Use triple quote for multiline strings/ to print single and double
quotes in same string
• ‘ ‘ ‘ John’s mother told me “he is not well” ‘ ‘ ‘
• ‘ ‘ ‘ I am fine
Thank you ‘ ‘ ‘
Assigning Constants to variables
• = is used for assignment
• A = 10
• a = 100
•a=a+1
• profit2020 = 4500
• an_integer = 143
• a_string = ‘143’
• captain_name = ‘Virat Kohli’
• fav_novel = “3 states”
Data Types
• Python's data types are built in the core of the language
• They are easy to use and straightforward.
• Data types supported by Python
• Boolean values
• None
• Numbers
• Strings
• Tuples
• Lists
• Sets
Boolean values
• Primitive data type having any one of two values: True or
False
• 12 > 14 → False
• a=5,
• b=22
• c=100
• a+b-c < 0 → True
• As = is assignment operator = = is used for equality
checking
• a + b == 27 → True
• 80 + 10 == c → False
None

• Special data type - None

• Basically, the data type means non existent, not known or empty

• Can be used to check for emptiness


Numbers
Types of numbers supported by Python:
• Integers
• floating point numbers
• complex numbers
• Fractional numbers
Integers
• Integers have no fractional part in the number
• a = 10
•b=a
• type(a)
• type(b)
Floating Point Numbers
• Number with fractional part
• a = 3.1415
• p = 6.283
• type(a)
• type(b)
Complex Numbers
• A complex number consists of an ordered pair of real
floating point numbers denoted by a + bj, where a is
the real part and b is the imaginary part of the complex
number.
• complex(x) to convert x to a complex number with real
part x and imaginary part zero
• complex(x, y) to convert x and y to a complex number
with real part x and imaginary part y.
• x and y are numeric expressions
Complex Numbers
• 1j * 1J → (-1+0j)
• 2 + 1j * 3 → (2+3j)
• (2 + 1j) * 3 → (6+3j)
• A = 1+2j
• B=3+2j
• C = A+B
• print(C) → ?
Complex Numbers

• A = 7 + 5j
# prints real part of the number
• print(A.real)
# prints imaginary part of the number
• print(A.imag)
# Can do operations with part of complex number
• print(A.imag+3)
Python is a Dynamic Type language
• Same variable can be associated with values of
different type during program execution, as indicated
below.
• It's also very dynamic as it rarely uses what it knows
to limit variable usage
Input and output function
Input function : input
Basic_pay = input('Enter the Basic Pay: ')

Output function : print


print('Hello world!')
print(‘Net Salary', salary)
Printing values of variables
• Numbers
• a = 10
• b = 20
• print(“the sum of”,a “and”,b, “is”,a+b)
• Strings
• str1=‘Jamal’
• str2=‘Priya’
• print(str1+’is a friend of’+str2)
By Default…
• Input function reads all values as strings, to convert then to integers
and float, use the function int() and float()
• p=input(“Enter principal”)
• p = int(p)
• n= int(input(“Enter number of years”))
• r=float(input(‘Enter interest rate’))
• i=p*n*r/100
• print(‘the interest is, i)
Now Let’s move to VPROPEL
Thank you !!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy