Phython 1-3
Phython 1-3
• Introduction
o Object oriented
o Hight level programming
o Interpreted
o Dynamic semantics
• Features
o Easy to learn
o Open source
o Broad standard library
o Cross platform
o Multi paradigm language
o Extendable language
o Expressive programming language
o GUI programming support
• Applications
o Network programming
o Data analysis
o Robotics
o Website and application development
o Desktop application
o Games development
o Web scraping
o Data visualization
o Scientific calculation
o Machine learning and artificial intelligence
o 3D application development
o Audio and video software development
• Download and install
o Python.org
• Python IDEs
o Default IDE (IDLE) – we can just run python command but we can not save our code.
o Pycharm
o Visual studio code
• Create first application
o File extension (.py)
o Comments: # sign used for it, python does not have multi line comments.
o Indentation: python works on indentation, it creates block or body of If or for etc.
o Print(“hello world”): works for output, we can concatenate two strings, but not string with
variables, if you wanna do so then use comma.
• Variables
o Just like java script it is dynamic data types. Same rules applied, at run time it will decide the
variable type base on its data. Just write variable name and assign value and there is no
termination sign.
a=10
b=10
print(a,b)
print(id(a)) it will print memory address of a
o string concatenation ( + sign is used for concatenation, if both side are string, if any one is
number then it will generate an error.)
o string can be defined either single or double quotes
o Define a variable
▪ A variable name must start with a letter or the underscore character
▪ A variable name cannot start with a number
▪ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
▪ Variable names are case-sensitive (age, Age and AGE are three different variables)
▪ A variable name cannot be any of the Python keywords.
o Variable Naming Case
▪ Camel Case
myVariableName = "Test"
▪ Pascal Case
MyVariableName = "Test"
▪ Snake Case
my_variable_name = "Test"
• User Input and type casting
o Input
a = input(“Enter the value:”)
b = input(“Enter second value:”)
print( a + b)
• Casting
X = str(3)
Y = int(3)
Z = float(3)
o Assignment
o Comparison
o Logical
o Identity
Is just like == and is not just like !=
o Membership
o Bitwise
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
▪ Python does not have character type so single character is string of length of 1.
▪ We can use [] brackets to access elements of string with index number.
▪ String Length: len() function returns the strings length
b = "Hello, World!"
t = len(b)
▪ String Functions
print(a.upper())
print(a.lower())
print(a.title())
print(a.captialize())
print(a.strip())
print(a.replace("H", "J"))
print(a.split(",")) # returns ['Hello', ' World!']
print(a.isalpha()) return true if all alphabets
print(a.isdigit()) return true if all numbers
print(a.isalnum()) return true if either number or
alphabets or alphanumeric but false if other than
this like special character.
print(a.find(‘e’,starting index)) #return index of
first occurrence.
print(a.index(‘e’,starting index)) only difference
between find and index function is that find
returns -1 and index returns error on not found.
• Conditional Statements
in python there is no body brackets so it works on indentation levels. These indentations create parent
and child relation. We can create this indentation levels with spaces or tabs.
IF ELSE BLOCK:
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Multiple Conditions
if a > b and c > a:
print("Both conditions are True")
NOT:
if not a > b:
print("a is NOT greater than b")
Nested:
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
SHORTHAND: