0% found this document useful (0 votes)
11 views20 pages

Python Basics

Python is a high-level, interpreted programming language known for its easy-to-learn syntax and versatility across various fields such as AI and web development. It features a large standard library, supports multiple data types, and includes various operators for performing operations. The document also covers Python's character set, tokens, identifiers, literals, data types, type conversion, and control structures like conditional statements and loops.

Uploaded by

Dhanusri
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)
11 views20 pages

Python Basics

Python is a high-level, interpreted programming language known for its easy-to-learn syntax and versatility across various fields such as AI and web development. It features a large standard library, supports multiple data types, and includes various operators for performing operations. The document also covers Python's character set, tokens, identifiers, literals, data types, type conversion, and control structures like conditional statements and loops.

Uploaded by

Dhanusri
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

Python is a high-level, interpreted programming language created by

Guido van Rossum in 1991.

Main Features
Easy to learn – Simple and readable syntax
Interpreted language – Code runs line by line
Platform independent – Runs on Windows, Linux, Mac
Large standard library – Many built-in modules available
Free and open source – Anyone can use and modify it
Used in many fields – AI, Data Science, Web Development, Automation
Syntax simplicity of three programming languages to print “Hello world!”
Python Character Set

A character set is a set of valid characters that can be used in a programming language.

Letters A–Z a–z Example name, total, sum

Digits 0–9 Example10, 25, 100

Special Symbols + - * / = % ( ) { } [ ] # @ !

Whitespace Characters Space Tab New line


Python Tokens the smallest individual units of a Python program.

Eg: a = 10 + 5

Keywords: reserved words in Python that have a special meaning. They cannot be used as variable names.
if else for print
Identifiers: the names given to variables, functions, or objects.

Rules for Identifiers


Must start with letter or underscore (_)
Can contain letters, digits, underscore
Cannot start with a number
Cannot be a keyword
Literals: Values used Numerical literal a=10 String literal a=‘Hi’ Boolean Literal True

Operators: used to perform operations. + - * / //

Punctuators: symbols used to organize the program structure. () [] {} \ ; :

Variables: A variable is a name given to a memory location used to store data. A=apple
Identifier Valid / Invalid Reason
total_marks Valid Uses letters and underscore
1value Invalid Cannot start with a number
_count Valid Identifier can start with underscore
class Invalid Keyword in Python
student1 Valid Letters and numbers allowed
total marks Invalid Space not allowed
marks$ Invalid $ not allowed
True Invalid Boolean keyword
Letters, numbers and underscore
data_2024 Valid
allowed
for Invalid Keyword
Try this yourself

_sum __name__

sum_ name_1

2sum global

sum2 avg-score

sum-total studentName

Sum

sumTotal

sum total
Answers

_sum Valid __name__ Valid

sum_ Valid name_1 Valid

2sum Invalid global Invalid

sum2 Valid avg-score Invalid

sum-total Invalid studentName Valid

Sum Valid

sumTotal Valid

sum total Invalid


Data Type: A data type tells what kind of value a variable stores.

Number: Stores numeric values. None: Represents no value or empty value.


•Integer: Whole numbers (e.g., 10, -5) Example: None
•Float: Decimal numbers (e.g., 3.14, 5.0)
•Complex: Numbers with real and imaginary parts (e.g., 3+4j)

Set: A set is a collection of unique elements


Boolean: Stores only True or False values. written inside { }.
Example: Example:A = {1, 2, 3, 4}
True
False

Sequence: Stores multiple values in order. Mapping


•String: Collection of characters (e.g., "Hello") Dictionary: Stores data in key–value pairs.
•List: Ordered collection of items (e.g., [1,2,3]) Example:
•Tuple: Ordered collection that cannot be changed (e.g., (1,2,3)) {"name":"John", "age":18}
MUTABLE: VALUES CAN BE CHANGED LATER list set dictionary

IMMUTABLE: VALUES CANNOT BE CHANGED AFTER CREATED

int float bool complex string tuple


Refer XI Computer Science Book Page number from 100 - 104

Operator Definition Operators Example

Used to perform
Arithmetic Operators + - * / % // ** a+b
mathematical calculations.

Relational (Comparison) Used to compare two


== != > < >= <= a>b
Operators values.

Used to assign values to


Assignment Operators = += -= *= /= %= a = 10
variables.

Used to combine
Logical Operators and or not a>5 and b<10
conditions.

Used to check if a value


Membership Operators in not in "a" in "apple"
exists in a sequence.
a=1
Used to check whether two
b=a
Identity Operators variables refer to the same is is not
object.
a is b
Type Conversion: The process of changing one data type into another data type.

Type Definition Example

Python automatically converts one


Implicit Conversion 5 + 3.2 → result 8.2
data type to another.

The programmer manually


Explicit Conversion (Type
converts the data type using int(), float(), str()
Casting)
functions.
Try this yourself find the output

print(int(5.8))

print(int("20") + 5)

print(float(5))

print(int("5") + int("6"))

print(str(5) + str(6))

print(int("5") + float("2.5"))

print(int("5.5"))

print(str(10) + str(5) + str(2*2))


Answers

print(int(5.8)) 5

print(int("20") + 5) 25

print(float(5)) 5.0

print(int("5") + int("6")) 11

print(str(5) + str(6)) 56

print(int("5") + float("2.5")) 7.5

print(int("5.5")) ValueError

print(str(10) + str(5) + str(2*2)) 1054


Try this yourself find the output

print(type([1,2,3])) Print(type({}))

print(type((1,2,3))) print(type([1,2]+[3,4]))

print(type({1,2,3})) print(type((1,2)+(3,4)))

print(type({"a":1,"b":2})) print(type([1,2,3][0]))

print(type((5))) print(type((1,2,3)[1]))

print(type((5,)))

Print(type(3>1))
Conditional Statements in Python
Statement Definition Example
Executes a block of code only if the condition is if x>10:
if
true. print(“Hi”)
if x>10:
Executes one block if the condition is true, otherwise print(“Hi”)
if–else
another block. else:
print(“Bye”)
if x==0:
print(“No”)
elif x==1:
if–elif–
Used to check multiple conditions one by one. print(“Yes”)
else
else:
print(“Close”)

if x==0:
print(“No”)
if x==0:
Nested if An if statement inside another if statement.
print(“Yes”)
else:
print(“Close”)
Try this yourself find the output

x = 10 x = 5

if x > 5: if x > 10:

print("A") print("A")

if x > 8: else:

print("B") print("B")
Try this yourself find the output

x = 7 x = 8

if x > 10: if x > 5:

print("A") if x > 7:

elif x > 5: print("A")

print("B") else:

else: print("B")

print("C")
Try this yourself find the output

x = 5
x = 3
y = 7
y = 6

z = 2
if x > 10:

print("A")
if x > 5 or y > 4 and z > 3:
elif y > 5:
print("A")
print("B")
else:
elif x > 2:
print("B")
print("C")
for Loop: used to repeat a block of code for each item in a sequence (like list, string, or range).

Syntax
for variable in sequence:
statement
Output

0
for i in range(5): 1
print(i) 2
3
4

name = "PYTHON" P
for i in name: Y
print(i) T
H
O
N

You might also like