The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.
Colab link - Open colab
Your First Program - Printing statements and numbers.
We can use print function to display a string , integers, float, complex numbers.
Example:
print("Hello Friend")
print(30)
Section 2: Python Variables - Creating Variables
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Example:
x = 5
y = "Python"
print(x)
print(y)
Variables do not need to be declared with any particular type and can even change type after they have been set.
x = 4 # x is of type int
y = "python" # x is now of type str
print(x)
print(y)
Variable Names : A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:
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).
**NOTE:** Remember that variables are case-sensitive.
Python Numbers There are three numeric types in Python: int, float, complex
Int : Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
x = 1
y = 35656222554887711
z = -3255522
print(type(x)) # To verify the type of any object in Python, use the type() function
print(type(y))
print(type(z))
Float : Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Complex : Complex numbers are written with a "j" as the imaginary part.
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Python Casting - Specify a Variable Type : There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.
Casting in python is therefore done using constructor functions:
A literal is a notation for representing a fixed value in source code.
int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
Integers :
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Main advantage of type casting is you can print and integer and a string in the same line.
Example:
a = " kingdoms in Westeros"
b = str(7)
print (b + a)
Python Strings - String literals:
String literals in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. Square brackets can be used to access elements of the string.
a = "Hello, World!"
print(a[1]) # Gets the character at position 1 (remember that the first character has the position 0)
b = "Hello, World!"
print(b[2:5]) #Gets the characters from position 2 to position 5 (not included)
The strip() method: The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The len() method: The len() method returns the length of a string
a = "Hello, World!"
print(len(a))
The lower() method: The lower() method returns the string in lower case
a = "Hello, World!"
print(a.lower())
print(a) #Orignal value of a is not changed
a=a.lower() #Orignal value of a is changed
print(a)
The upper() method: The upper() method returns the string in upper case
a = "Hello, World!"
print(a.upper())
The replace() method : The replace() method replaces a string with another string.
a = "Hello, World!"
print(a.replace("H", "J"))
The split() method : The split() method splits the string into substrings if it finds instances of the separator
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']