0% found this document useful (0 votes)
34 views6 pages

Python Ka Chilla: Basics with Ammar

The document provides an introduction to Python programming basics through a series of code examples in Jupyter Notebooks. It covers topics like printing output, operators, variables, data types, input functions, conditional logic, loops, functions, importing libraries and troubleshooting. The examples demonstrate how to write simple Python code, work with data types, use basic programming constructs and import commonly used libraries.

Uploaded by

Ziao Rahman
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)
34 views6 pages

Python Ka Chilla: Basics with Ammar

The document provides an introduction to Python programming basics through a series of code examples in Jupyter Notebooks. It covers topics like printing output, operators, variables, data types, input functions, conditional logic, loops, functions, importing libraries and troubleshooting. The examples demonstrate how to write simple Python code, work with data types, use basic programming constructs and import commonly used libraries.

Uploaded by

Ziao Rahman
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 ka chilla with baba Aammar

# how to use jupyter Note book


Basics of Python
1- My first program

01- My first program


In [1]: print("python_with_Ammar")
print("we are learning python with Ammar")

print(45*25)

python_with_Ammar

we are learning python with Ammar

1125

02_operators
In [2]: print(2+2)

print(3-2)

print(6/2)

print(2*3)

print(13%2)

print(6//2)

print(2**3)

print(2**2/2*3/3+6-4)

#PEAMDAS

# parenthasis Exponents Multiply Divide Addition Subdtraction

# Left to sequence for M D & A S

3.0

4.0

03-Strings
In [3]: print("Hello world")

print("We are learning with Ammar")

Hello world

We are learning with Ammar

05- Variables
In [4]: # Variables: objects containing specific values

x = 5

print(x) # numeric or integer variables

y = "we are learning python with Ammar" #String variables

print(y)

x = x+10

print(x)

#types of variables

type(x)

print(type(y))

# Rules to assign a variable

# 1- The variable Should contain letters, numbers or Underscores

# 2- Do not start with numbers

# 3- Spaces are not Allowed

# 4- Do not use Keywords used in functions (break, mean, media, test etc)

# 5- short and descriptive

# 6- Case sensitivity (Lowercase, uppercaseletters, lowercase letters is recommended)

fruitbasket= "Mangos"

print(fruitbasket)

we are learning python with Ammar

15

<class 'str'>

Mangos

06-Input_Variables
In [ ]: #fruit_basket ="Mangos"

#print(fruit_basket)

#input function simple

# fruit_basket = input("Which is your favourite fruit?")

# print(fruit_basket)

#input function of 2nd stage

# name =input("what is your name? ")

# greetings = "Hello!"

# print(greetings, name)

#Another way of stage 2 input function

# input function of 2nd stage

# name =input("what is your name? ")

# print("Hello!", name)

#3rd stage input function

name= input("what is your name?")


age = input("how old are you?")

greetings = "Hello!"

print(greetings, name, ", You are still young")

07-Conditional_logics
In [ ]: ### Logical Operators mentioned below

### logical operators are either "true or false" or "yes or No" or "0 or 1"

### Equal to ==

### not equal to !=

### great than <

### less than and equal to <=

### greater than and equal to >=

### is equal to?

### print(4==4)

### application of logical operators

### hamad_age = 4

### age_at_school = 5

### print(hamad_age==age_at_school)

### input funcion and logical operator

hamad_age = 4

age_at_school = 5

hamad_age = input("how old is hamad? ") #input function

hamad_age=int(hamad_age)

print(type(hamad_age))

print(hamad_age==age_at_school) #logical operator

08-Type_conversion
In [ ]: # from unicodedata import name

# x = 10 # integer

# y = 10.2 # float (with disassemble)

# z = "Hello" # string

# implicit type conversion

# x = x+y

# print(x,"type of x is:", type(x))

# exlicit type conversion

# age = input("What is your age? ")

# age = int(age)

# print(age, type(float(age)))

# name

from zipapp import ZipAppError

name = input("what is your name? ")

print(name,type(str(name)))

09-If_else_elif
In [ ]: requird_age_at_school = 5

hammad_age =2

# question : can hammad go to school

if hammad_age == requird_age_at_school:

print("hammad can join the school.")

elif hammad_age > requird_age_at_school:

print("Hammad should join high school.")

print("hammad can not go to school.")

elif hammad_age <= 5:

print("you should take care of hammad, he is still a baby!")

else:

print("hammad can not go to school.")

10-Functions
In [ ]: # definig a function

# def print_codanics():

# print("We are learning with Ammar.")

# print("We are learning with Ammar.")

# print("We are learning with Ammar.")

# print_codanics()

# 2

# def print_codanics():

# text = "we are learning with Aammar in codanics youtube channel"

# print(text)

# print(text)

# print(text)

# print_codanics()

# 3

# def print_codanics(text):

# print(text)

# print(text)

# print(text)

# print_codanics("We are learning python with Ammar in codanics youtube channel")

# defining a function with if elif else statements

# def school_calculator(age, text):

# if age == 5:

# print("Hammad can join the school")

# elif age > 5:

# print("Hammad should go to high school")

# else:

# print("Hammad is stil a baby")

# school_calculator(1,"Hammad")

# dfining a function of future

def fg(age):

new_age = age + 20

return new_age

print(new_age)

future_predicted_age= fg(18)

print(future_predicted_age)

11-Loops
In [ ]: # while loops and For loops

# while loops

# x = 0

# while (x<5):

# print(x)

# x = x + 1

# for loop

# for x in range(5,10):

# print(x)

# array

days = ["mon", "tue","wed", "thu", "Fri", "sat", "sun"]

for d in days:

if (d=="Fri"):break #loop stops

# if (d=="Fri"): continue #skips d

print(d)

12-Import_libraries
In [ ]: #### if you want to print the value of pi

import math

print("The value of pi is ", [Link])

import statistics

x = [150,250,350,450]

print([Link](x))

#### important libraries: numpy, pandas

13-Trouble_shouting
In [ ]: # if you want to print the value of pi

import math

print("The value of pi is ", [Link])

import statistics

x = [150,250,350,450]

print([Link](x))

# important libraries: numpy, pandas

Common questions

Powered by AI

Type conversion in Python involves transforming a variable from one type to another. It can occur implicitly (e.g., adding an integer and a float results in a float) or explicitly where functions like 'int()', 'float()', or 'str()' are used. This is crucial when working with inputs that need to be processed or compared in a specific type, such as converting a string input of age to an integer for comparisons .

Type safety in Python can be enhanced by explicitly converting and checking types using functions like 'type()', 'isinstance()', and adhering to consistent type usage throughout code. Proper validation of inputs, usage of type hints, and employing testing frameworks also help maintain type safety by ensuring that variables contain the expected types and errors are caught early during execution .

Conditional logic in Python uses 'if-elif-else' statements to direct code execution based on evaluated conditions. It allows a program to make decisions, such as determining user eligibility for actions based on input or state. For example, a child's eligibility for school is assessed with conditions that check if the child is the right age and continues to appropriate alternate actions based on age comparison results .

The input function in Python can operate at various stages – initially, it captures user input as a string. In the second stage, it may incorporate additional user interaction, for example, greeting the user with entered information ('print("Hello!", name)'). The third stage involves integrating conditional logic or post-processing of input, such as prompting for age and converting it to an integer for logical comparison .

Effective debugging strategies in Python involve thinking critically about where errors may occur and systematically testing small portions of code. Utilizing print statements or integrated development environments (IDEs) with debugging tools can identify logical errors and runtime issues. Understanding and employing proper exception handling, unit testing, and code reviews can greatly reduce bugs and improve code robustness .

Loops in Python, such as 'for' and 'while', automate repetitive tasks by iterating over sequences or until conditions are met. 'For' loops run a block of code over a sequence such as a list, while 'while' loops continue until a specified condition is false. For instance, iterating over days of the week can be automated with a 'for' loop to print each day unless a break condition like 'if (d=="Fri")' is fulfilled .

Logical operators in Python are used to evaluate conditions as either true or false, with common operators including '==' for equal to, '!=' for not equal to, and '>=' for greater than or equal to. For instance, if 'hamad_age == age_at_school' evaluates as true, it confirms that the age is equal to the required age for school, otherwise, it results in false .

Defining functions in Python involves using the 'def' keyword followed by the function name and parameters. Inside the function body, logic is implemented. For example, a school admission function calculates eligibility based on age using 'if', 'elif', and 'else' statements. Functions provide modularity, making code reusable and easier to manage, thus supporting DRY (Don't Repeat Yourself) principles .

When naming a variable in Python, the variable name should contain letters, numbers, or underscores and should not start with numbers. Spaces are not allowed, and it is important not to use keywords that are reserved in functions (e.g., 'break', 'mean'). Variables should be short and descriptive, and Python is case-sensitive, so it's recommended to use lowercase letters consistently .

Libraries in Python are crucial for extending functionality without having to reinvent the wheel. They provide pre-built functions and tools for data manipulation, statistical analysis, and more. Important libraries include 'math' for mathematical functions, 'statistics' for statistical operations, 'numpy' for numerical calculations, and 'pandas' for data manipulation and analysis. Utilizing these libraries accelerates development and enhances code efficiency .

You might also like