0% found this document useful (0 votes)
3 views31 pages

Advance Python Programming Until Operators

The document provides an introduction to Python programming, covering its basics such as comments, character sets, tokens, keywords, identifiers, variables, user input, data types, literals, punctuators, and operators. It includes examples of how to declare variables, assign values, and perform operations, as well as activities for practice. Overall, it serves as a foundational guide for beginners to understand Python programming concepts.

Uploaded by

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

Advance Python Programming Until Operators

The document provides an introduction to Python programming, covering its basics such as comments, character sets, tokens, keywords, identifiers, variables, user input, data types, literals, punctuators, and operators. It includes examples of how to declare variables, assign values, and perform operations, as well as activities for practice. Overall, it serves as a foundational guide for beginners to understand Python programming concepts.

Uploaded by

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

Introduction to Python

Python is a programming language which was created by Guido Van Rossum . It can be used to
follow both procedural approach and object-oriented approach of programming.
PYTHON BASICS  Comments : Comments are the statements which are incorporated
in the code to give a better understanding of code statements to the
user. There are two types of comments in python.

1. Single Line

2. Multi Line .

 Single Line comment A single-line comment is used to add some


explanatory text in the program for better understanding of the next
line.

 Multiline comments The multiline comments are written in python


using triple quotes. You can write number lines starting with triple
quotes and end with triple quotes.
•CHARACTER SET:
Character set is a group of letters or signs which are
valid in a language. For example English has 26, Greek
has 24 characters, Hindi has 44 etc. And Python
Character set includes letters, sign, numbers and
symbols.
Introduction • Letters: A-Z, a-z
• Digits:0-9
• Special Symbols:_,+,-,*,/,(,),{,},[,]...etc
• White spaces: blank space, tab, carriage return,
newline etc.
TOKENS
•Token is the
smallest unit of a
program in any
programming
language. It is also
known as Lexical
Unit.
1. Keywords

•Keywords are those reserved


words for specific functioning which
provides a special meaning to
interpreter/compiler.
•The keywords can’t be used for
any other purpose than the originally
defined. Some of the keywords
displayed below:
2. Identifiers
Example on Identifiers

● Identifiers are the name given to variables, classes, methods(functions), etc. For example,

Here, language is a variable (an identifier) which holds the value 'Python'.
We cannot use keywords as variable names as they are reserved names that are
built-in to Python. For example,

The above code is wrong because we have used continue as a variable name.
2.1. Valid Identifiers
2.2. Invalid identifiers
Activity: W.A.P to declare four different type of values Example:
Value 1="Your Name"
Value 2="Age"
Value 3="Grade in Roman Number" Use print statement "sep"
and "end", to add more data.
Variables

● In Python, a variable is a container for holding values, akin to a box.


● These containers can accommodate a wide range of data types, making them versatile storage units.
● Like a physical box, you can open the variable to inspect its contents or add new data.
● Assigning a name to a variable is comparable to labelling a container, providing a means of
identification.
● These named variables can store various values, such as numbers and strings, in Python.
There are certain rules that need to be followed while naming variables in Python.
Activity: identify if these variable names are valid or not.
Assigning a value to a variable
Create a variable and assign any numerical value. Use the print()function to display the value stored in the variable.

num = 100 # create and assign a value to the variable


print(num) # display the value of the variable
Output:
100

num =100 # create and assign a value to the variable


print(num) # display the value of the variable
num =120.5 # reassign a new value to the variable
print(num) # display the variable after re-assigning
Output:
100
120.5
Assigning a value to a variable
We can assign a value of a variable to another variable. Create another variable, num2, and assign the
variable num2 to num1.

num1 = 100 # assign a value to the variable num1


num2 = num1 # assign the value of num1 to the variable
num2
print("num2 :", num2) # display the variable

Output:
num2 :100
User input in Python
● The input() function is used to get input from the user.
User input in Python
● Get a string as user input and assign it to the new variable name.

name = input("Enter your name: ")

● Write a code to greet the user.

print("Welcome ", name)

● Click Run and enter any name.


Output:
Enter your name: Arthur
Welcome Arthur
Data types
Data types represent the type of data that any variable holds in its memory. We will
use the following data types in this module.
Data types
Let’s write a program to find the sum of 10 and any other number entered by a user.

# program to find the sum of two numbers


number1 = input("Enter the number: ")
number2 = 10
# display the sum
print("The sum is ", number1 + number2)

Output:
Feedback:
Incompatible types
You used an addition operation with a string and a number on line 4.
But you can't do that with that operator. Make sure both sides of the
operator are the right type.
Data types
Typecasting
Even if you enter an integer value, the input() function converts it into a string. You
need to convert it into an integer in your code using typecasting.

Convert the input to integer data type using int() function.


# program to find the sum of two numbers
number1 = int(input("Enter the number: "))
number2 = 10
# display the sum
print("The sum is ", number1 + number2)

Output:
Enter the first number: 2
The sum is 12
Literals are the values
which are saved in
variables/identifiers,
and they cannot be
changed.
Punctuators

Used to implement the grammatical structure of Syntax.


Python Operators
• Operators are special symbols or keywords used to perform operations on variables and
values.
• Think of them like mathematical signs or logical tools.

Example:
a = 10
b=5
print(a + b)
Activity
Arithmetic Operators Comparison Operators

a = 10 a = 10
b=3 b=3
print("Arithmetic Operators:")
print("Comparison Operators:")
print("a + b =", a + b)
print("a == b:", a == b)
print("a - b =", a - b)
print("a * b =", a * b) print("a != b:", a != b)
print("a / b =", a / b) print("a > b:", a > b)
print("a % b =", a % b) print("a < b:", a < b)
print("a ** b =", a ** b)
print("a >= b:", a >= b)
print("a // b =", a // b)
print("a <= b:", a <= b)
Activity
Assignment Operators Logical Operators

a = 10
a = 10
b=3
print("Assignment Operators:")
b=3
x=5 print(" Logical Operators:")
print("Initial x =", x)
print("a > 5 and b < 5:", a > 5 and b < 5)
x += 3
print("x += 3 →", x) print("a < 5 or b < 5:", a < 5 or b < 5)
x *= 2
print("not(a > b):", not(a > b))
print("x *= 2 →", x)
x -= 4
print("x -= 4 →", x)
x /= 2
print("x /= 2 →", x)
THANK YOU

You might also like