Style

Python Lesson 1

Lesson 1 

What is Programming?
Programming is a way to communicate with your PC. We write the code in many languages and then the compiler/Interpreter Translates the code in a language that a PC can understand.

What is Python?
  • Python is a simple & easy language
  • It is Free and Open Source
  • It is a high-level level language
  • Developed by Guido van Rossum
first Programme
print("Hello World")

Output
Hello World

Python Character Set
  • Letters – A to Z, a to z
  • Digits – 0 to 9
  • Special Symbols - + - * / etc.
  • Whitespaces – Blank Space, tab, carriage return, newline, formfeed
  • Other characters – Python can process all ASCII and Unicode characters as part of data or literal

Variables
A variable is a name given to a memory location in a program
var_1 = "Hello World"

Here we give the "Hello World" value to the Variable var_1
We can change the value of the variable as often as we want

Rules for Identifiers
  • Identifiers can combine uppercase and lowercase letters, digits, or an underscore(_). So my Variable, variable_1, variable_for_print all are valid python identifiers.
  • An Identifier can not start with a digit. So while the variable is valid, Ivariable is not valid.
  • We can't use special symbols like !,#,@,%,$, etc in our Identifier.
  •  Identifiers can be of any length.
Data Types
There can be various types of data that can be stored in a variable some examples are
var_1 = "Hello World" #This is a String
var_2 = 223           #This is a integer
var_3 = 45.67         #This is a float
var_4 = True          #This is a boolean

We can know their type by using the type() function
print(type(var_1)) #'str'
print(type(var_2)) #'int'
print(type(var_3)) #'float'
print(type(var_4)) #'bool'


Keywords
Keywords are reserved words in Python. In Python Keywords are case-sensitive We cannot write True as true it will give us an error
Some examples are- True, False, or, and, not, it, etc.

Comments in Python
There are two types of comments in Python
#This is a single line comment

"""
This is a 
Multi Line
Comment
"""


Types of Operators
An operator is a symbol that performs a certain operation between operands.

1) Arithmetic Operators
num = 30 + 40  #This will give addition
num = 30 - 40  #This will give Subtraction
num = 30 / 40  #This will give Division
num = 30 * 40  #This will give Multiplication
num = 30 ** 40 #This will give Exponential
num = 30 % 40  #This will give Reminder


2) Relational / Comparison Operators
They are used to comparing one number with another. It gives output as True or False
a, b = 30, 40

print(a <= b)  #True
print(a == b)  #False
print(a >= b)  #False
print(a > b)   #False
print(a < b)   #True
print(a != b)  #True


3) Assignment Operators
They do the same thing as Arithmetic Operators but they shorten the code. They can also be said as self assigned variables. 
num = 30
num += 10
num -= 10
num *= 10
num **= 10
num /= 10
num %= 10


4) Logical Operators
var_1 = True
var_2 = False

print(not var_1)           #True
print(var_1 and var_2)     #False
print(var_1 or var_2)      #True


Type Conversion
The type Conversion is automatically done by the program we do not have to write anything extra for it for example if we are adding two values one is Int and another is Float then Int will automatically get converted into Int
var_1 = 3
var_2 = 45.45
print(var_1 + var_2) #It will give answer in a floting value


Type Casting
In this, we have to tell the computer that we want to convert a certain data type to another data type
Some examples are

var = 30.59
var_1 = int(var) #It will convert the float value to Int
var_2 = str(var) #It will convert the float value to String
var_3 = bool(var) #It will give a True as answer

If we convert 0 to a boolean it will give a False value and in another case, it will give a True
The same thing applies to String if we convert an empty string to a boolean it will give a False value and in another case, it will give a True
var = 0
var_1 = ""
print(bool(var))   #False
print(bool(var_1)) #False

Input in Python
input( ) statement is used to accept values (using the keyboard) from the user
var = input("Write Your Name")       #result for input( ) is always a str
var = int(input("Write Your Age"))   #We can conver it in other type as we want

Comments

Script