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

Nic Python Basics

This document is a beginner's guide to the Python programming language, covering fundamental concepts such as variables, data types, operators, control flow, loops, functions, and exception handling. It provides examples and explanations for each topic to facilitate understanding for new programmers. The guide also includes best practices and tips for beginners.

Uploaded by

arjunbs316
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 views11 pages

Nic Python Basics

This document is a beginner's guide to the Python programming language, covering fundamental concepts such as variables, data types, operators, control flow, loops, functions, and exception handling. It provides examples and explanations for each topic to facilitate understanding for new programmers. The guide also includes best practices and tips for beginners.

Uploaded by

arjunbs316
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 Programming Language

Beginner Guide

Contents

1 What is Python? 3
1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Variables 3
2.1 What is a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.5 Rules for Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6 Correct Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.7 Wrong Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 Data Types 4
3.1 What are Data Types? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Main Data Types in Python . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.1 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.2 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.3 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.4 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4 Operators 5
4.1 What are Operators? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

5 Strings in Python 6
5.1 What is a String? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.3 Access Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.4 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.5 String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1
6 Control Flow 6
6.1 What is Control Flow? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.2 if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.3 if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.4 if-elif-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

7 Loops 7
7.1 for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.2 while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.3 break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.4 continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

8 Type Casting 8
8.1 What is Type Casting? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
8.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

9 Functions 8
9.1 What is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
9.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
9.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
9.4 Function with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 9
9.5 Function with Return Value . . . . . . . . . . . . . . . . . . . . . . . . . 9

10 Lists, Tuples, and Sets 9


10.1 List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
10.2 Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
10.3 Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

11 Exception Handling 10
11.1 What is an Exception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
11.2 try-except Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
11.3 Multiple Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
11.4 finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

12 Final Beginner Tips 11


12.1 Project Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

13 Simple Beginner Program 11

14 Conclusion 11

2
1 What is Python?
Python is a simple and powerful programming language used for:
• Web Development
• AI and Machine Learning
• Automation
• Data Science
• Cybersecurity
• Software Development
Python is beginner-friendly because its syntax is easy to read and understand.

1.1 Example
print ( " Hello World " )

2 Variables
2.1 What is a Variable?
A variable is used to store data in memory.
You can think of a variable like a container that stores information.

2.2 Syntax
variable_name = value

2.3 Example
name = " Arjun "
age = 20
marks = 95.5

print ( name )
print ( age )
print ( marks )

2.4 Output
Arjun
20
95.5

3
2.5 Rules for Variable Names
Can contain:

• Letters

• Numbers

• Underscore ( )

Cannot:

• Start with a number

• Use spaces

• Use special symbols

2.6 Correct Example

student_name = " Ram "


age1 = 21

2.7 Wrong Example

1 name = " Ram "


student name = " Ram "

3 Data Types
3.1 What are Data Types?
Data types define the type of value stored in a variable.

3.2 Main Data Types in Python

Data Type Description Example


int Integer numbers 10
float Decimal numbers 5.7
str Text/String ”Hello”
bool True or False True
list Multiple values [1,2,3]
tuple Ordered immutable data (1,2,3)
set Unordered unique values {1,2,3}
dict Key-value pairs {”name”:”Ram”}

4
3.3 Examples
3.3.1 Integer

age = 21
print ( type ( age ) )

3.3.2 Float

price = 99.99
print ( type ( price ) )

3.3.3 String

name = " Python "


print ( type ( name ) )

3.3.4 Boolean

is_passed = True
print ( type ( is_passed ) )

4 Operators
4.1 What are Operators?
Operators are symbols used to perform operations on variables and values.

4.2 Arithmetic Operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Power
// Floor Division

4.3 Example

a = 10
b = 3

print ( a + b )

5
print ( a - b)
print ( a * b)
print ( a / b)
print ( a % b)
print ( a ** b )
print ( a // b )

5 Strings in Python
5.1 What is a String?
A string is a collection of characters.

5.2 Example

name = " Python "

5.3 Access Characters

text = " Python "

print ( text [0])


print ( text [1])

5.4 String Concatenation

first = " Hello "


second = " World "

print ( first + " " + second )

5.5 String Length

text = " Python "

print ( len ( text ) )

6 Control Flow
6.1 What is Control Flow?
Control flow controls how a program runs step by step.

6
6.2 if Statement

age = 18

if age >= 18:


print ( " Eligible to vote " )

6.3 if-else Statement

number = 5

if number % 2 == 0:
print ( " Even " )
else :
print ( " Odd " )

6.4 if-elif-else Statement

marks = 85

if marks >= 90:


print ( " Grade A " )
elif marks >= 75:
print ( " Grade B " )
else :
print ( " Grade C " )

7 Loops
7.1 for Loop

for i in range (5) :


print ( i )

7.2 while Loop

count = 1

while count <= 5:


print ( count )
count += 1

7
7.3 break Statement

for i in range (10) :


if i == 5:
break

print ( i )

7.4 continue Statement

for i in range (5) :


if i == 2:
continue

print ( i )

8 Type Casting
8.1 What is Type Casting?
Type casting means converting one data type into another.

8.2 Example

num = " 10 "

converted = int ( num )

print ( converted )
print ( type ( converted ) )

9 Functions
9.1 What is a Function?
A function is a block of reusable code.

9.2 Syntax

def function_name () :
statement

8
9.3 Example

def greet () :
print ( " Welcome to Python " )

greet ()

9.4 Function with Parameters

def add (a , b ) :
print ( a + b )

add (5 , 3)

9.5 Function with Return Value

def square ( num ) :


return num * num

result = square (4)

print ( result )

10 Lists, Tuples, and Sets


10.1 List

fruits = [ " apple " , " banana " , " orange " ]

print ( fruits )
print ( fruits [0])

fruits . append ( " grape " )

print ( fruits )

10.2 Tuple

numbers = (1 , 2 , 3)

print ( numbers )
print ( numbers [1])

9
10.3 Set

data = {1 , 2 , 3 , 3 , 4}

print ( data )

11 Exception Handling
11.1 What is an Exception?
An exception is an error that occurs during program execution.

11.2 try-except Example

try :
num = int ( input ( " Enter number : " ) )
print ( num )

except :
print ( " Invalid Input " )

11.3 Multiple Exceptions

try :
a = 10
b = 0

print ( a / b )

except Ze roDivi sionEr ror :


print ( " Cannot divide by zero " )

except ValueError :
print ( " Invalid value " )

11.4 finally Block

try :
print ( " Hello " )

except :
print ( " Error " )

finally :
print ( " Program Ended " )

10
12 Final Beginner Tips
1. Practice Daily

2. Start Small

3. Read Errors Carefully

4. Build Small Projects

12.1 Project Examples


• Calculator

• Number Guessing Game

• Student Result System

13 Simple Beginner Program

name = input ( " Enter your name : " )


age = int ( input ( " Enter your age : " ) )

if age >= 18:


print ( name , " is eligible to vote " )
else :
print ( name , " is not eligible to vote " )

14 Conclusion
In this guide you learned:

• Variables

• Data Types

• Operators

• Strings

• Control Flow

• Type Casting

• Functions

• Lists, Tuples, and Sets

• Exception Handling

11

You might also like