[go: up one dir, main page]

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

Sunu 5

Uploaded by

esraayaksiz
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

Sunu 5

Uploaded by

esraayaksiz
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/ 15

INTRODUCTION

TO PYTHON
1) PYTHON BASICS

CONTENTS
2) CONTROL STRUCTURES
3) BASIC DATA STRUCTURES
PYTHON BASIC
PYTHON SYNTAX
 Indentation refers to the spaces at the beginning of a code line.
 Where in other programming languages the indentation in code is for readability only,
the indentation in Python is very important.
 Python uses indentation to indicate a block of code.

Syntax Error:
if 5 > 2:
print("Five is greater if 5 > 2:
than two!") print("Five is greater than two!")
PYTHON BASIC
◦ PYTHON SYNTAX
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
For Python: For C:
#This is a comment //This is a comment
print("Hello, World!") printf("Hello
World!");
"""
This is a comment /* The code below will print the words Hello World!
written in to the screen, and it is amazing */
more than just one line printf("Hello World!");
"""
print("Hello, World!")
PYTHON VARIABLES AND DATA TYPES
◦ A variable name must start with a letter or the underscore character myvar = "John"
my_var = "John"
◦ A variable name cannot start with a number
_my_var = "John"
◦ A variable name can only contain alpha-numeric characters and underscores.
myVar = "John"
◦ Variable names are case-sensitive. MYVAR = "John"
◦ A variable name cannot be any of the Python keywords. myvar2 = "John"

Data types
int: represents integer x = str(3) # x will be '3'
y = int(3) # y will be 3
float: represents decimal numbers z = float(3) # z will be 3.0
str: represents strings of characters
INPUT/OUTPUT
INPUT FUNCTİON
The input() function allows you to receive data from the user.
This function takes an input from the user and returns this input as a string.
x = input('Enter your
name:')
print('Hello, ' + x)

PRINT FUNCTİON
The print() function is used to print output to the screen.
print("Hello World")

Multiple values can be printed on the same line by separating them with commas.
x =
("apple", "banana", "cherry")
print(x)
CONTROL STRUCTURES(IF, ELIF, ELSE)
•If, elif and else statements are used for decision-making
in Python. USING C LANGUAGE

•if: Executes a block of code if the condition is true.


•elif: Checks additional conditions if the previous ones
are false.
•else: Executes a fallback block if no conditions are
true. USING PYTHON
CONTROL STRUCTURE (WHILE)
USING C LANGUAGE
The while loop in Python runs as long as the
condition is true.

In C, it uses parentheses for the condition and


curly braces {} for the block.

USING PHYTON
CONTROL STRUCTURE (FOR)
A for loop is used for iterating over a sequence. RANGE(PHYTON THEN C LANGUAGE)

STRING

LIST
CONTROL STRUCTURE (BREAK AND CONTINUE)
BREAK:With the break statement we can stop the loop before it has looped through all the items

CONTINUE:With the continue statement we can stop the current iteration of the loop, and continue with the
next:

BREAK CONTINUE
BASIC DATA STRUCTURES
String slicing feature in Python is not directly
STRING SLICING FOR supported in C language but we can perform the
same operation with the help of a loop
PYTHON
#include <stdio.h>

int main() {
b = "Hello, World! char b[ ] = "Hello,World!"; // String definition
01234 printf("Substring: %.3s\n", &b[2]); //Print 3 characters starting from the
2nd character
return 0;
print(b[2:5]) }
&b[2]:
It gives a pointer starting from the 2nd index of the string.
2 included in 5 is not included in This points to the rest of the string.
character count the character count %.3s:

OUTPUT: This format specifier prints at most 3 characters from the string.
llo So it prints the part starting from the 2nd index to the 5th index (llo).
BASIC DATA STRUCTURES
STRING FORMAT For C
# include <stdio.h>
For Python
age = 36 int main() {
txt = "My name is John, I am " + str(age) int age = 36;
print(txt)
// Direct screen printing
printf("My name is John, I am %d\n” , age);
*Python has a dynamic type system. This means that you return 0;
don't need to declare the variable age as int. Python
automatically determines the type of the variable. }

To concatenate strings in Python, you need the + operator or a method *C has a static type system, meaning you must
like f-string. specify the type of each variable (e.g. int, char)
in advance.
In C, concatenation is not done with printf; only the string and variables
are output directly using format specifiers.
ACTIVITIES
CALCULATING FACTORIAL WITH
CALCULATING FACTORIAL WITH C
PYTHON
RESOURCES
• Python Official Documentation
• FreeCodeCamp: Python Crash Course
• W3Schools Python Tutorial

You might also like