0% found this document useful (0 votes)
8 views15 pages

Python Coding For Beginners 1

The document provides a comprehensive introduction to Python programming for beginners, covering fundamental concepts such as loops, conditionals, and basic arithmetic operations. It includes examples of using for and while loops, input handling, and if statements, along with explanations of their functionality. Additionally, it discusses the range function and its applications in generating sequences of numbers.
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)
8 views15 pages

Python Coding For Beginners 1

The document provides a comprehensive introduction to Python programming for beginners, covering fundamental concepts such as loops, conditionals, and basic arithmetic operations. It includes examples of using for and while loops, input handling, and if statements, along with explanations of their functionality. Additionally, it discusses the range function and its applications in generating sequences of numbers.
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 for beginners‬

‭ ame:‬ ‭__________________________________‬ ‭Roll.‬ ‭No_______‬ ‭Date:‬ ‭______‬


N
‭_________________________________________________________________________‬
‭ ession_I‬
S
‭1. Print a Greeting Message‬
print("Hello, welcome to Python programming!")‬

‭2. For Loop Examples in Python‬


‭ for loop in Range function‬
#
for i in range(0,21,5)‬

print (i)‬

‭ for loop using range function‬


#
for i in range(2,10):‬

print(i)‬

‭ for loop example in Range‬


#
for i in range(6):‬

print(i)‬

‭ for loop to print each character in String‬


#
for i in “COMPUTER”:‬

print(i)‬

‭3. Write a python code to print the sum of numbers from 1 to 100‬
‭ for loop‬
#
sum =0‬

for i in range (1,101):‬

sum = sum + i‬

print(“ The sum of first 100 natural numbers is :“, sum)‬

4‭ . Write a python code input the age of 5 friends and find how many less than‬
‭18 years of age are‬
‭ input age five friends age‬
#
Minor=0‬

for i in range (0,5)‬

age = eval(input(“enter age:”))‬

if(age<18):‬

minor=minor+1‬

print(“Out of 5 friends”, minor, “ are under 18 years.”)‬

‭5. Write a python code to print List Item using for loop‬
‭ for loop in List‬
#
computer=[“Printer”,”Moniter”,”keyboard”,“Mouse”,”Software”]‬

for i in computer:‬

print(i)‬

‭6. Write a python code to print List Item using for loop‬
‭ for loop in List‬
#
for i in “COMPUTER”:‬

print(i)‬

7‭ . Write a python code to print the numbers from 0 to 20 with a step value of 5‬
‭using while loop‬
‭ while loop‬
#
i=0‬

while i<=20:‬

print(i)‬

i=i+5‬

8‭ . Write a python code to print the sum of numbers from 1 to 100 using while‬
‭loop‬
‭ while loop code‬
#
i=0‬

sum=0‬

while(i<=20):‬

sum=sum+i‬

i=i+1‬

print(“the sum of first 100 natural numbers is :”sum)‬

9‭ . Write a python code to enter numbers and stop asking for input as soon as a‬
‭number greater than 100is entered‬
‭ while loop code‬
#
i=0‬

found=True‬

while (Found==True):‬

number=eval(input(“input a number:”))‬

i=i+i‬

if(number>100):‬

found=false‬

print(“totalnumbers entered are: ”,i)‬

‭Session-II‬

‭1. Adding Two Numbers‬


‭ Input two numbers‬
#
num1 = int(input("Enter first number: "))‬

num2 = int(input("Enter second number: "))‬

‭ Add the numbers‬


#
sum = num1 + num2‬

‭ Print the result‬


#
print("The sum of", num1, "and", num2, "is", sum)‬

‭ xplanation:‬‭This program takes two numbers as input from the user, adds them,‬
E
‭and displays the result.‬
‭3. Check if a Number is Even or Odd‬
‭ Input a number‬
#
num = int(input("Enter a number: "))‬

‭ Check if the number is even or odd‬


#
if num % 2 == 0:‬

print(num, "is an even number.")‬

else:‬

print(num, "is an odd number.")‬

‭Explanation:‬‭This program checks whether a given number is even or odd.‬

‭4. Simple Calculator‬

‭●‬ O
‭ bjective:‬‭To perform basic arithmetic operations (addition, subtraction,‬
‭multiplication, division).‬

‭ython code‬
P
# Taking input from the user‬

num1 = float(input("Enter the first number: "))‬

num2 = float(input("Enter the second number: "))‬

‭ Performing arithmetic operations‬


#
print("Addition:", num1 + num2)‬

print("Subtraction:", num1 - num2)‬

print("Multiplication:", num1 * num2)‬

print("Division:", num1 / num2)‬

‭Python code‬
‭ he user is asked to enter the number, the input number is stored in a variable number‬
T
‭and then we have checked the number using if..elif..else statement.‬
# User enters the number‬

number = int(input("Enter number: "))‬

# checking the number‬

if number < 0:‬

print("The entered number is negative.")‬

elif number > 0:‬

print("The entered number is positive.")‬

elif number == 0:‬

print("Number is zero.")‬

else:‬

print("The input is not a number")‬

‭ he range() function in Python is used to generate a sequence of numbers. It’s‬


T
‭commonly used in loops like for loops.‬

‭ yntax:‬
S
range(start, stop, step)‬

‭‬ s
● ‭ tart (optional): the beginning of the sequence (default is 0)‬
‭●‬ ‭stop (required): the end of the sequence (not included)‬
‭●‬ ‭step (optional): the amount by which the value increases (default is 1)‬

‭Examples:‬

‭1.‬ ‭Basic usage:‬

for i in range(5):‬

print(i)‬

‭Output:‬

‭0‬

‭1‬

‭2‬

‭3‬

‭4‬

‭Starts from 0, goes up to 4 (not including 5)‬

‭2.‬ ‭Using start and stop:‬

for i in range(2, 6):‬



print(i)‬

‭Output:‬

‭2‬

‭3‬

‭4‬

‭5‬

‭ sing step:‬
U
for i in range(1, 10, 2):‬

print(i)‬

‭Output:‬

‭1‬

‭3‬

‭5‬

‭7‬

‭9‬
‭ ounting backwards:‬
C
for i in range(5, 0, -1):‬

print(i)‬

‭Output:‬

‭5‬

‭4‬

‭3‬

‭2‬

‭1‬

‭range() can be converted to a list for better understanding.‬

‭ ctually, in the range() function, the last number (the stop value) is not included — and‬
A
‭that's by design.‬

‭ xample:‬
E
for i in range(2, 6):‬

print(i)‬

‭2‬

‭3‬

‭4‬

‭5‬

I‭t starts at 2, and stops before 6. So 6 is not included.‬


‭ hy is the stop value excluded?‬
W
‭This is a common convention in Python and many programming languages because:‬

‭ ‬ I‭t makes it easier to work with zero-based indexing (like in lists).‬



‭●‬ ‭The length of the range is simply stop - start.‬
‭●‬ ‭It helps with slicing and looping over arrays more predictably.‬

‭Python Code‬
‭In this program, users are asked to enter a year. Program checks whether the‬
‭ntered year is a leap year or not.‬
e
# User enters the year‬

year = int(input("Enter Year: "))‬

# Leap Year Check‬

if year % 4 == 0 and year % 100 != 0:‬

print(year, "is a Leap Year")‬

elif year % 100 == 0:‬

‭rint(year,
p "is not a Leap Year")‬
elif year % 400
‭ ==0:‬
print(year,
‭ "is a Leap Year")‬
else:‬

print(year,
‭ "is not a Leap Year")‬
‭Example: Check If number is even or odd‬
‭ his program takes the input from user and checks whether the entered number‬
T
‭is even or odd. We are receiving the value into a variable‬‭
num‬‭and then we are‬
‭dividing the num by 2 to see whether it is an even number or odd number.‬

‭um = int(input("Enter any number: "))‬


n
flag = num%2‬

if flag == 0:‬

print(num, "is an even number")‬

elif flag == 1:‬

print(num, "is an odd number")‬

else:‬

print("Error, Invalid input")‬

‭Python If Statement explained with examples‬


I‭f statements are control flow statements which helps us to run a particular code‬
‭only when a certain condition is satisfied. For example, you want to print a‬
‭message on the screen only when a condition is true then you can use if‬
‭statement to accomplish this in programming. In this guide, we will learn how to‬
‭use‬‭if statements in Python programming‬‭with the help of examples.‬

‭ here are other control flow statements available in Python such as if..else,‬
T
‭if..elif..else,‬
‭nested if etc. However in this guide, we will only cover the if statements, other‬
‭control statements are covered in separate tutorials.‬

‭Syntax of If statement in Python‬


‭The syntax of if statement in Python is pretty simple.‬

if condition:‬

block_of_code‬

‭If statement flow diagram‬

‭Python – If statement Example‬


‭lag = True‬
f
if flag==True:‬

print("Welcome")‬

print("To")‬

print("BeginnersBook.com")‬

‭Output:‬
‭elcome‬
W
To‬

BeginnersBook.com‬

I‭n the above example we are checking the value of flag variable and if the value is‬
‭True then we are executing few print statements. The important point to note here‬
‭is that even if we do not compare the value of flag with the ‘True’ and simply put‬
‭‘flag’ in place of condition, the code would run just fine so the better way to write‬
‭the above code would be:‬

‭lag = True‬
f
if flag:‬

print("Welcome")‬

print("To")‬

print("BeginnersBook.com")‬

‭ y seeing this we can understand how if statement works. The output of the‬
B
‭condition would either be true or false. If the outcome of condition is true then the‬
‭statements inside body of ‘if’ executes, however if the outcome of condition is‬
‭false then the statements inside ‘if’ are‬‭skipped‬‭. Lets take another example to‬
‭understand this:‬

‭lag = False‬
f
if flag:‬

print("You Guys")‬

print("are")‬

print("Awesome")‬

‭ he output of this code is none, it does not print anything because the outcome of‬
T
‭condition is ‘false’.‬

‭Python if example without boolean variables‬


I‭n the above examples, we have used the boolean variables in place of conditions.‬
‭However we can use any variables in our conditions. For example:‬

‭um = 100‬
n
if num < 200:‬

print("num is less than 200")‬

Output:‬

num is less than 200‬

‭Python If Statement explained with examples‬


I‭f statements are control flow statements which helps us to run a particular code‬
‭only when a certain condition is satisfied. For example, you want to print a‬
‭message on the screen only when a condition is true then you can use if‬
‭statement to accomplish this in programming. In this guide, we will learn how to‬
‭use‬‭if statements in Python programming‬‭with the help of examples.‬

‭ here are other control flow statements available in Python such as if..else,‬
T
‭if..elif..else,‬
‭nested if etc. However in this guide, we will only cover the if statements, other‬
‭control statements are covered in separate tutorials.‬
‭Syntax of If statement in Python‬
‭The syntax of if statement in Python is pretty simple.‬
if condition:‬

block_of_code‬

‭If statement flow diagram‬

‭Python – If statement Example‬


‭lag = True‬
f
if flag==True:‬

print("Welcome")‬

print("To")‬

print("BeginnersBook.com")‬

‭Output‬‭
:‬
‭elcome‬
W
To‬

BeginnersBook.com‬

I‭n the above example we are checking the value of flag variable and if the value is‬
‭True then we are executing few print statements. The important point to note here‬
‭is that even if we do not compare the value of flag with the ‘True’ and simply put‬
‭‘flag’ in place of condition, the code would run just fine so the better way to write‬
‭the above code would be:‬

‭lag = True‬
f
if flag:‬

print("Welcome")‬

print("To")‬

print("BeginnersBook.com")‬

‭ y seeing this we can understand how if statement works. The output of the‬
B
‭condition would either be true or false. If the outcome of condition is true then the‬
‭statements inside body of ‘if’ executes, however if the outcome of condition is‬
‭false then the statements inside ‘if’ are‬‭skipped‬‭. Lets take another example to‬
‭understand this:‬
‭lag = False‬
f
if flag:‬

print("You Guys")‬

print("are")‬

print("Awesome")‬

‭ he output of this code is none, it does not print anything because the outcome of‬
T
‭condition is ‘false’.‬

‭Python if example without boolean variables‬


I‭n the above examples, we have used the boolean variables in place of conditions.‬
‭However we can use any variables in our conditions. For example:‬

‭um = 100‬
n
if num < 200:‬

print("num is less than 200")‬

Output:‬

num is less than 200‬

‭Python If elif else statement example‬


I‭n this tutorial, we will learn‬‭if elif else statement in Python‬‭. The if..elif..else‬
‭statement is used when we need to check multiple conditions.‬

‭Syntax of if elif else statement in Python‬


‭This way we are checking multiple conditions.‬
if condition:‬

block_of_code_1‬

elif condition_2:‬

block_of_code_2‬

elif condition_3:‬

block_of_code_3‬

..‬

..‬

..‬

else:‬

block_of_code_n‬

‭ otes:‬
N
‭1. There can be multiple ‘elif’ blocks, however there is only ‘else’ block is allowed.‬
‭2. Out of all these blocks only one block_of_code gets executed. If the condition is‬
‭true then the code inside ‘if’ gets executed, if condition is false then the next‬
‭condition(associated with elif) is evaluated and so on. If none of the conditions is‬
‭true then the code inside ‘else’ gets executed.‬
‭Python – if..elif..else statement example‬
‭In this example, we are‬‭checking multiple conditions‬‭using if..elif..else statement.‬

‭um = 1122‬
n
if 9 < num < 99:‬

print("Two digit number")‬

elif 99 < num < 999:‬

print("Three digit number")‬

elif 999 < num < 9999:‬

print("Four digit number")‬

else:‬

print("number is <= 9 or >= 9999")‬

# Program to check if a number is prime‬



‭um = int(input("Enter a number:
n "))‬
if num <= 1:‬

print(f"{num} is not a prime
‭ number.")‬
else:‬

for i in range(2, int(num **
‭ 0.5) + 1):‬
if num % i == 0:‬

print(f"{num} is not
‭ a prime number.")‬
break‬

else:‬

print(f"{num} is a prime
‭ number.")‬

‭ he expression range(2, int(num ** 0.5) + 1) is used to efficiently check if a‬


T
‭number is prime. Let's break it down:‬

‭Full Meaning:‬

‭ ‬ ‭num ** 0.5 means square root of num.‬



‭●‬ ‭int(num ** 0.5) converts the square root to an integer (i.e., removes the‬
‭decimal part).‬
‭‬ +
● ‭ 1 ensures the range includes the square root itself.‬
‭●‬ ‭range(2, ...) generates numbers starting from 2 up to (but not including) the‬
‭integer just after the square root of num.‬

‭Why square root?‬

I‭f a number has a factor greater than its square root, then it must also have a‬
‭factor smaller than its square root. So, we only need to check up to √num to know‬
‭whether it’s prime.‬

‭Example:‬
Let’s say num = 29‬

Then:‬

num ** 0.5 → 5.385...‬

int(num ** 0.5) → 5‬

So range(2, 6) gives: 2, 3, 4, 5‬

‭ e check if 29 is divisible by any of those numbers. Since it's not, it's a prime‬
W
‭number.‬

‭Would you like a version of this code that lists all primes in a given range?‬

‭Python for Loop explained with examples‬


‭ loop is a used for iterating over a set of statements repeatedly. In Python we‬
A
‭have three types of loops‬‭for‬‭,‬‭while‬‭and‬‭do-while‬‭. In this guide, we will learn‬‭for‬
‭loop‬

‭Syntax of For loop in Python‬


for <variable> in <sequence>:‬

# body_of_loop that has set of statements‬

# which requires repeated execution‬

‭ ere <variable> is a variable that is used for iterating over a <sequence>. On every‬
H
‭iteration it takes the‬‭next value‬‭from <sequence>‬ ‭until the end of sequence is‬
‭reached.‬

‭Lets take few examples of for loop to understand the usage.‬

‭Python – For loop example‬


‭ he following example shows the use of for loop to iterate over a list of numbers.‬
T
‭In the body of for loop we are calculating the square of each number present in‬
‭list and displaying the same.‬

# Program to print squares of all numbers present in a list‬


‭ List of integer numbers‬


#
numbers = [1, 2, 4, 6, 11, 20]‬

‭ variable to store the square of each num temporary‬


#
sq = 0‬

‭ iterating over the given list‬


#
for val in numbers:‬

# calculating square of each number‬

‭q = val * val‬
s
# displaying the squares‬

print(sq)‬

‭Output:‬

1‬

4‬

16‬

36‬

121‬

400‬

‭Function range()‬
I‭n the above example, we have iterated over a list using for loop. However we can‬
‭also use a range() function in for loop to iterate over numbers defined by range().‬

r‭ ange(n)‬‭: generates a set of whole numbers starting from 0 to (n-1).‬


‭For example:‬
‭range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]‬

‭range(start, stop)‬‭: generates a set of whole numbers starting from‬‭


start‬‭to‬
stop-1‬
‭ ‭.‬
‭For example:‬
‭range(5, 9) is equivalent to [5, 6, 7, 8]‬

r‭ ange(start, stop, step_size)‬‭: The default step_size is 1 which is why when we‬
‭didn’t specify the step_size, the numbers generated are having difference of 1.‬
‭However by specifying step_size we can generate numbers having the difference‬
‭of step_size.‬
‭For example:‬
‭range(1, 10, 2) is equivalent to [1, 3, 5, 7, 9]‬

‭Lets use the‬‭range() function‬‭in for loop:‬

‭Python for loop example using range() function‬


‭ ere we are using‬‭range() function‬‭to calculate and‬‭display the sum of first 5‬
H
‭natural numbers.‬

‭ Program to print the sum of first 5 natural numbers‬


#
# variable to store the sum‬

sum = 0‬

‭ iterating over natural numbers using range()‬
#
for val in range(1, 6):‬

# calculating sum‬

sum = sum + val‬

‭ displaying sum of first 5 natural numbers‬


#
print(sum)‬

‭utput:‬
O
15‬

‭For loop with else block‬
‭ nlike‬‭Java‬‭, In Python we can have an optional ‘else’ block associated with the‬
U
‭loop. The ‘else’ block executes only when the loop has completed all the‬
‭iterations. Lets take an example:‬

for val in range(5):‬



print(val)‬

else:‬

print("The loop has completed execution")‬

Output:‬

0‬

1‬

2‬

3‬

4‬

The loop has completed execution‬

‭Note:‬‭The else block only executes when the loop is finished.‬

‭Nested For loop in Python‬


‭ hen a for loop is present inside another for loop then it is called a nested for‬
W
‭loop. Lets take an example of nested for loop.‬

for num1 in range(3):‬



for num2 in range(10, 14):‬

print(num1, ",", num2)‬

Output:‬

0 , 10‬

0 , 11‬

0 , 12‬

0 , 13‬

1 , 10‬

1 , 11‬

1 , 12‬


1 , 13‬
2
‭ , 10‬
2
‭ , 11‬
2
‭ , 12‬
2
‭ , 13‬

You might also like